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:crunch.MaxTemperature.java

License:Apache License

public static byte[] deserialize(Writable writable, byte[] bytes) throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        DataInputStream dataIn = new DataInputStream(in);
        writable.readFields(dataIn);
        dataIn.close();//w  w w .jav a2s  .c om
        return bytes;
    }

From source file:edu.mit.ll.graphulo.util.SerializationUtil.java

License:Apache License

public static void deserializeWritable(Writable writable, InputStream inputStream) {
    Preconditions.checkNotNull(writable);
    Preconditions.checkNotNull(inputStream);
    try (DataInputStream in = new DataInputStream(inputStream)) {
        writable.readFields(in);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }/* w  ww.  jav a2  s  .  co  m*/
}

From source file:edu.uci.ics.hyracks.imru.util.SerDeUtils.java

License:Apache License

public static void deserialize(Writable object, byte[] buffer) throws IOException {
    ByteArrayInputStream bbis = new ByteArrayInputStream(buffer);
    DataInput input = new DataInputStream(bbis);
    object.readFields(input);
}

From source file:edu.uci.ics.pregelix.dataflow.util.IterationUtils.java

License:Apache License

public static Writable readGlobalAggregateValue(Configuration conf, String jobId) throws HyracksDataException {
    try {//  w  w w  .  j  a  v a  2s  .c  o m
        FileSystem dfs = FileSystem.get(conf);
        String pathStr = IterationUtils.TMP_DIR + jobId + "agg";
        Path path = new Path(pathStr);
        FSDataInputStream input = dfs.open(path);
        Writable agg = BspUtils.createFinalAggregateValue(conf);
        agg.readFields(input);
        input.close();
        return agg;
    } catch (IOException e) {
        throw new HyracksDataException(e);
    }
}

From source file:edu.umd.cloud9.io.Tuple.java

License:Apache License

/**
 * Deserializes the Tuple./*from w  w  w .j  a  va2s.  com*/
 * 
 * @param in
 *            source for raw byte representation
 */
public void readFields(DataInput in) throws IOException {
    int numFields = in.readInt();

    mObjects = new Object[numFields];
    mSymbols = new String[numFields];
    mFields = new String[numFields];
    mTypes = new Class[numFields];

    for (int i = 0; i < numFields; i++) {
        mFields[i] = in.readUTF();
    }

    for (int i = 0; i < numFields; i++) {
        byte type = in.readByte();

        if (type == SYMBOL) {
            String className = in.readUTF();
            try {
                mTypes[i] = Class.forName(className);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mObjects[i] = null;
            mSymbols[i] = in.readUTF();
        } else if (type == INT) {
            mTypes[i] = Integer.class;
            mObjects[i] = in.readInt();
        } else if (type == BOOLEAN) {
            mTypes[i] = Boolean.class;
            mObjects[i] = in.readBoolean();
        } else if (type == LONG) {
            mTypes[i] = Long.class;
            mObjects[i] = in.readLong();
        } else if (type == FLOAT) {
            mTypes[i] = Float.class;
            mObjects[i] = in.readFloat();
        } else if (type == DOUBLE) {
            mTypes[i] = Double.class;
            mObjects[i] = in.readDouble();
        } else if (type == STRING) {
            mTypes[i] = String.class;
            mObjects[i] = in.readUTF();
        } else {
            try {
                String className = in.readUTF();
                mTypes[i] = Class.forName(className);

                int sz = in.readInt();
                byte[] bytes = new byte[sz];
                in.readFully(bytes);

                Writable obj = (Writable) mTypes[i].newInstance();
                obj.readFields(new DataInputStream(new ByteArrayInputStream(bytes)));
                mObjects[i] = obj;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:gaffer.utils.WritableToStringConverter.java

License:Apache License

public static Writable deserialiseFromString(String serialised) throws IOException {
    // Convert the base64 string to a byte array
    byte[] b = Base64.decodeBase64(serialised);

    // Deserialise the writable from the byte array
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    DataInput in = new DataInputStream(bais);
    String className = Text.readString(in);
    try {/*from  www  . j av  a  2s.c  o m*/
        Writable writable = (Writable) Class.forName(className).newInstance();
        writable.readFields(in);
        return writable;
    } catch (InstantiationException e) {
        throw new IOException("Exception deserialising writable: " + e);
    } catch (IllegalAccessException e) {
        throw new IOException("Exception deserialising writable: " + e);
    } catch (ClassNotFoundException e) {
        throw new IOException("Exception deserialising writable: " + e);
    } catch (ClassCastException e) {
        throw new IOException("Exception deserialising writable: " + e);
    }
}

From source file:gobblin.util.HadoopUtils.java

License:Apache License

/**
 * Deserialize a {@link Writable} object from a string.
 *
 * @param writableClass the {@link Writable} implementation class
 * @param serializedWritableStr the string containing a serialized {@link Writable} object
 * @param configuration a {@link Configuration} object containing Hadoop configuration properties
 * @return a {@link Writable} deserialized from the string
 * @throws IOException if there's something wrong with the deserialization
 *///from   w  ww  .  j  a v a 2s  .c o m
public static Writable deserializeFromString(Class<? extends Writable> writableClass,
        String serializedWritableStr, Configuration configuration) throws IOException {
    byte[] writableBytes = BaseEncoding.base64().decode(serializedWritableStr);

    try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(writableBytes);
            DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream)) {
        Writable writable = ReflectionUtils.newInstance(writableClass, configuration);
        writable.readFields(dataInputStream);
        return writable;
    }
}

From source file:io.aos.hdfs.WritableTestBase.java

License:Apache License

public static byte[] deserialize(Writable writable, byte[] bytes) throws IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    DataInputStream dataIn = new DataInputStream(in);
    writable.readFields(dataIn);
    dataIn.close();/*from  w w  w .  j a v a 2 s .  co m*/
    return bytes;
}

From source file:it.crs4.pydoop.mapreduce.pipes.CommonStub.java

License:Apache License

protected void readObject(Writable obj, DataInputStream inStream) throws IOException {
    int numBytes = WritableUtils.readVInt(inStream);
    byte[] buffer;
    // For BytesWritable and Text, use the specified length to set the length
    // this causes the "obvious" translations to work. So that if you emit
    // a string "abc" from C++, it shows up as "abc".
    if (obj instanceof BytesWritable) {
        buffer = new byte[numBytes];
        inStream.readFully(buffer);//from w ww  .  jav a  2  s  .  co m
        ((BytesWritable) obj).set(buffer, 0, numBytes);
    } else if (obj instanceof Text) {
        buffer = new byte[numBytes];
        inStream.readFully(buffer);
        ((Text) obj).set(buffer);
    } else {
        obj.readFields(inStream);
    }
}

From source file:ml.shifu.guagua.mapreduce.GuaguaInputSplitTest.java

License:Apache License

public Writable bytesToObject(byte[] data, String className) {
    if (data == null || className == null) {
        throw new NullPointerException(
                String.format("data and className should not be null. data:%s, className:%s",
                        data == null ? null : Arrays.toString(data), className));
    }//  w  ww .  j av a  2  s. com
    Writable result = (Writable) ReflectionUtils.newInstance(className);
    DataInputStream dataIn = null;
    try {
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        dataIn = new DataInputStream(in);
        result.readFields(dataIn);
    } catch (Exception e) {
        throw new GuaguaRuntimeException(e);
    } finally {
        if (dataIn != null) {
            try {
                dataIn.close();
            } catch (IOException e) {
                throw new GuaguaRuntimeException(e);
            }
        }
    }
    return result;
}