Example usage for org.apache.hadoop.io Writable readFields

List of usage examples for org.apache.hadoop.io Writable readFields

Introduction

In this page you can find the example usage for org.apache.hadoop.io Writable readFields.

Prototype

void readFields(DataInput in) throws IOException;

Source Link

Document

Deserialize the fields of this object from in.

Usage

From source file:com.datasalt.utils.commons.WritableUtils.java

License:Apache License

/**
* Deserialize Writables/*from  www  .j a v a2 s  . c  o  m*/
*/
public static Writable deserialize(Writable datum, byte[] bytes) throws IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    DataInputStream dis = new DataInputStream(bis);

    datum.readFields(dis);

    dis.close();
    return datum;
}

From source file:com.facebook.hiveio.common.Writables.java

License:Apache License

/**
 * Read fields from byteArray to a Writable object.
 *
 * @param bytes Byte array to find the fields in.
 * @param writableObject Object to fill in the fields.
 *///from  ww  w.j ava 2s  . c  o  m
public static void readFieldsFromByteArray(byte[] bytes, Writable writableObject) {
    DataInputStream is = new DataInputStream(new ByteArrayInputStream(bytes));
    try {
        writableObject.readFields(is);
    } catch (IOException e) {
        throw new IllegalStateException("readFieldsFromByteArray: IOException", e);
    }
}

From source file:com.google.appengine.tools.mapreduce.v2.impl.Writables.java

License:Apache License

/**
 * Initializes the writable via readFields using the data encoded in b.
 *
 * @param b byte array containing the fields to initialize w with
 * @param w writable to be initialized//  ww w  .jav a2s  .  c om
 */
public static void initializeWritableFromByteArray(byte[] b, Writable w) {
    try {
        w.readFields(new DataInputStream(new ByteArrayInputStream(b)));
    } catch (IOException ioe) {
        throw new RuntimeException("Got an impossible IOException from a ByteArrayInputStream.", ioe);
    }
}

From source file:com.google.mr4c.hadoop.HadoopTestUtils.java

License:Open Source License

/**
  * Copies writable using the readFields() and write() methods
*//*  ww  w .  ja v  a 2s.  c o m*/
public static void copyWritable(Writable src, Writable target) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    try {
        src.write(oos);
    } finally {
        oos.close();
    }
    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    try {
        target.readFields(ois);
    } finally {
        ois.close();
    }
}

From source file:com.hazelcast.jet.hadoop.impl.WritableSerializerHook.java

License:Open Source License

@Override
public Serializer createSerializer() {
    return new StreamSerializer<Writable>() {

        @Override/*  ww w .ja va 2s .c  om*/
        public int getTypeId() {
            return typeId;
        }

        @Override
        public void destroy() {
        }

        @Override
        public void write(ObjectDataOutput out, Writable writable) throws IOException {
            writable.write(out);
        }

        @Override
        public Writable read(ObjectDataInput in) throws IOException {
            T writable = constructor.get();
            writable.readFields(in);
            return writable;
        }
    };
}

From source file:com.ibm.jaql.io.serialization.binary.def.JsonJavaObjectSerializer.java

License:Apache License

@Override
public JsonJavaObject read(DataInput in, JsonValue target) throws IOException {
    JsonJavaObject t;//w  w  w .ja  v  a2 s.co  m
    if (target == null || !(target instanceof JsonJavaObject)) {
        t = new JsonJavaObject();
    } else {
        t = (JsonJavaObject) target;
    }

    String name = in.readUTF();
    Writable o = t.getInternalValue();
    if (o == null || !o.getClass().getName().equals(name)) {
        try {
            Class<?> clazz = Class.forName(name);
            o = (Writable) clazz.newInstance();
            t.set(o);
        } catch (Exception e) {
            throw new UndeclaredThrowableException(e);
        }
    }
    o.readFields(in);

    return t;
}

From source file:com.iflytek.spider.util.GenericWritableConfigurable.java

License:Apache License

@Override
public void readFields(DataInput in) throws IOException {
    byte type = in.readByte();
    Class clazz = getTypes()[type];
    try {//from  w w w  . j  a  va  2  s  .c  om
        set((Writable) clazz.newInstance());
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException("Cannot initialize the class: " + clazz);
    }
    Writable w = get();
    if (w instanceof Configurable)
        ((Configurable) w).setConf(conf);
    w.readFields(in);
}

From source file:com.jfolson.hive.serde.RTypedBytesWritableInput.java

License:Apache License

public Writable readWritable(Writable writable) throws IOException {
    DataInputStream dis = null;//  w  ww  .ja va 2 s. co  m
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(in.readBytes());
        dis = new DataInputStream(bais);
        String className = WritableUtils.readString(dis);
        if (writable == null) {
            try {
                Class<? extends Writable> cls = conf.getClassByName(className).asSubclass(Writable.class);
                writable = (Writable) ReflectionUtils.newInstance(cls, conf);
            } catch (ClassNotFoundException e) {
                throw new IOException(e);
            }
        } else if (!writable.getClass().getName().equals(className)) {
            throw new IOException("wrong Writable class given");
        }
        writable.readFields(dis);
        dis.close();
        dis = null;
        return writable;
    } finally {
        IOUtils.closeStream(dis);
    }
}

From source file:com.marklogic.mapreduce.LinkedMapWritable.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override/*from  w ww  .ja va2  s. c o  m*/
public void readFields(DataInput in) throws IOException {
    super.readFields(in);

    // First clear the map.  Otherwise we will just accumulate
    // entries every time this method is called.
    this.instance.clear();

    // Read the number of entries in the map
    int entries = in.readInt();

    // Then read each key/value pair
    for (int i = 0; i < entries; i++) {
        Writable key = (Writable) ReflectionUtils.newInstance(getClass(in.readByte()), getConf());

        key.readFields(in);

        Writable value = (Writable) ReflectionUtils.newInstance(getClass(in.readByte()), getConf());

        value.readFields(in);
        instance.put(key, value);
    }
}

From source file:com.nebulousnews.io.ObjectSerializableWritable.java

License:Apache License

/** Read a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding. *///from   w  w  w .  j a v a  2  s.c  om
@SuppressWarnings("unchecked")
public static Object readObject(DataInput in, ObjectSerializableWritable objectWritable, Configuration conf)
        throws IOException {
    String className = UTF8.readString(in);
    Class<?> declaredClass = PRIMITIVE_NAMES.get(className);
    if (declaredClass == null) {
        try {
            declaredClass = conf.getClassByName(className);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("readObject can't find class " + className, e);
        }
    }

    Object instance;

    if (declaredClass.isPrimitive()) { // primitive types

        if (declaredClass == Boolean.TYPE) { // boolean
            instance = Boolean.valueOf(in.readBoolean());
        } else if (declaredClass == Character.TYPE) { // char
            instance = Character.valueOf(in.readChar());
        } else if (declaredClass == Byte.TYPE) { // byte
            instance = Byte.valueOf(in.readByte());
        } else if (declaredClass == Short.TYPE) { // short
            instance = Short.valueOf(in.readShort());
        } else if (declaredClass == Integer.TYPE) { // int
            instance = Integer.valueOf(in.readInt());
        } else if (declaredClass == Long.TYPE) { // long
            instance = Long.valueOf(in.readLong());
        } else if (declaredClass == Float.TYPE) { // float
            instance = Float.valueOf(in.readFloat());
        } else if (declaredClass == Double.TYPE) { // double
            instance = Double.valueOf(in.readDouble());
        } else if (declaredClass == Void.TYPE) { // void
            instance = null;
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declaredClass);
        }

    } else if (declaredClass.isArray()) { // array
        int length = in.readInt();
        instance = Array.newInstance(declaredClass.getComponentType(), length);
        for (int i = 0; i < length; i++) {
            Array.set(instance, i, readObject(in, conf));
        }

    } else if (declaredClass == String.class) { // String
        instance = UTF8.readString(in);
    } else if (declaredClass.isEnum()) { // enum
        instance = Enum.valueOf((Class<? extends Enum>) declaredClass, UTF8.readString(in));
    } else if (Serializable.class.isAssignableFrom(declaredClass)) { //Serializable
        ObjectInputStream input = new ObjectInputStream((InputStream) in);
        try {
            instance = input.readObject();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            throw new IllegalArgumentException("ClassNotFound: " + declaredClass);
        }
    } else { // Writable (or fail, this is dangerous)
        Class instanceClass = null;
        String str = "";
        try {
            str = UTF8.readString(in);
            instanceClass = conf.getClassByName(str);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("readObject can't find class " + str, e);
        }

        Writable writable = WritableFactories.newInstance(instanceClass, conf);
        writable.readFields(in);
        instance = writable;

        if (instanceClass == NullInstance.class) { // null
            declaredClass = ((NullInstance) instance).declaredClass;
            instance = null;
        }
    }

    if (objectWritable != null) { // store values
        objectWritable.declaredClass = declaredClass;
        objectWritable.instance = instance;
    }

    return instance;

}