All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.io.GenericWritable Maven / Gradle / Ivy

There is a newer version: 3.2.0-9
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hadoop.io;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.ReflectionUtils;

/**
 * A wrapper for Writable instances.
 * 

* When two sequence files, which have same Key type but different Value * types, are mapped out to reduce, multiple Value types is not allowed. * In this case, this class can help you wrap instances with different types. *

* *

* Compared with ObjectWritable, this class is much more effective, * because ObjectWritable will append the class declaration as a String * into the output file in every Key-Value pair. *

* *

* Generic Writable implements {@link Configurable} interface, so that it will be * configured by the framework. The configuration is passed to the wrapped objects * implementing {@link Configurable} interface before deserialization. *

* * how to use it:
* 1. Write your own class, such as GenericObject, which extends GenericWritable.
* 2. Implements the abstract method getTypes(), defines * the classes which will be wrapped in GenericObject in application. * Attention: this classes defined in getTypes() method, must * implement Writable interface. *

* * The code looks like this: *
 * public class GenericObject extends GenericWritable {
 * 
 *   private static Class[] CLASSES = {
 *               ClassType1.class, 
 *               ClassType2.class,
 *               ClassType3.class,
 *               };
 *
 *   protected Class[] getTypes() {
 *       return CLASSES;
 *   }
 *
 * }
 * 
* * @since Nov 8, 2006 */ @InterfaceAudience.Public @InterfaceStability.Stable public abstract class GenericWritable implements Writable, Configurable { private static final byte NOT_SET = -1; private byte type = NOT_SET; private Writable instance; private Configuration conf = null; /** * Set the instance that is wrapped. * * @param obj */ public void set(Writable obj) { instance = obj; Class instanceClazz = instance.getClass(); Class[] clazzes = getTypes(); for (int i = 0; i < clazzes.length; i++) { Class clazz = clazzes[i]; if (clazz.equals(instanceClazz)) { type = (byte) i; return; } } throw new RuntimeException("The type of instance is: " + instance.getClass() + ", which is NOT registered."); } /** * Return the wrapped instance. */ public Writable get() { return instance; } @Override public String toString() { return "GW[" + (instance != null ? ("class=" + instance.getClass().getName() + ",value=" + instance.toString()) : "(null)") + "]"; } @Override public void readFields(DataInput in) throws IOException { type = in.readByte(); Class clazz = getTypes()[type & 0xff]; try { instance = ReflectionUtils.newInstance(clazz, conf); } catch (Exception e) { e.printStackTrace(); throw new IOException("Cannot initialize the class: " + clazz); } instance.readFields(in); } @Override public void write(DataOutput out) throws IOException { if (type == NOT_SET || instance == null) throw new IOException("The GenericWritable has NOT been set correctly. type=" + type + ", instance=" + instance); out.writeByte(type); instance.write(out); } /** * Return all classes that may be wrapped. Subclasses should implement this * to return a constant array of classes. */ abstract protected Class[] getTypes(); @Override public Configuration getConf() { return conf; } @Override public void setConf(Configuration conf) { this.conf = conf; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy