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:org.apache.nutch.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  ava 2s .  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:org.apache.nutch.util.WritableTestUtils.java

License:Apache License

/** Utility method for testing writables. */
public static Writable writeRead(Writable before, Configuration conf) throws Exception {

    DataOutputBuffer dob = new DataOutputBuffer();
    before.write(dob);//from www.  jav a  2  s.c  o m

    DataInputBuffer dib = new DataInputBuffer();
    dib.reset(dob.getData(), dob.getLength());

    Writable after = (Writable) before.getClass().newInstance();
    if (conf != null) {
        ((Configurable) after).setConf(conf);
    }
    after.readFields(dib);
    return after;
}

From source file:org.apache.orc.mapred.TestOrcList.java

License:Apache License

static void cloneWritable(Writable source, Writable destination) throws IOException {
    DataOutputBuffer out = new DataOutputBuffer(1024);
    source.write(out);//  w w w. jav  a  2s . c o m
    out.flush();
    DataInputBuffer in = new DataInputBuffer();
    in.reset(out.getData(), out.getLength());
    destination.readFields(in);
}

From source file:org.apache.pig.data.DataReaderWriter.java

License:Apache License

public static Writable bytesToWritable(DataInput in) throws IOException {
    String className = (String) readDatum(in);
    // create the writeable class . It needs to have a default constructor
    Class<?> objClass = null;
    try {/*from w ww .jav  a  2 s  .  com*/
        objClass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new IOException("Could not find class " + className + ", while attempting to de-serialize it ",
                e);
    }
    Writable writable = null;
    try {
        writable = (Writable) objClass.newInstance();
    } catch (Exception e) {
        String msg = "Could create instance of class " + className
                + ", while attempting to de-serialize it. (no default constructor ?)";
        throw new IOException(msg, e);
    }

    //read the fields of the object from DataInput
    writable.readFields(in);
    return writable;
}

From source file:org.apache.pig.piggybank.squeal.backend.storm.state.PigSerializer.java

License:Apache License

@Override
public Object deserialize(byte[] b) {

    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    DataInputStream dis = new DataInputStream(bais);

    try {/* ww w  .j  a  va 2s.com*/
        byte write_type = dis.readByte();
        Object ret = null;
        if (write_type == 0) {
            // Read the length.
            int values_size = dis.readInt();
            Values arr = new Values();

            for (int i = 0; i < values_size; i++) {
                // First read the type
                byte t = dis.readByte();
                // Get a new instance of the appropriate object.
                PigNullableWritable pnw = HDataType.getWritableComparableTypes(t).getClass().newInstance();
                pnw.readFields(dis);
                arr.add(pnw);
            }
            ret = arr;
        } else if (write_type == 1) {
            String cls = dis.readUTF();

            Writable c = (Writable) Class.forName(cls).newInstance();
            c.readFields(dis);
            ret = c;
        }

        return ret;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.sysml.runtime.util.LocalFileUtils.java

License:Apache License

/**
 * Reads an arbitrary writable from local file system, using a fused buffered reader
 * with special support for matrix blocks.
 * /*from w ww  .j av  a 2s  .c  om*/
 * @param fname file name to read
 * @param ret hadoop writable
 * @return hadoop writable
 * @throws IOException if IOException occurs
 */
public static Writable readWritableFromLocal(String fname, Writable ret) throws IOException {
    FileInputStream fis = new FileInputStream(fname);
    DataInput in = !(ret instanceof MatrixBlock)
            ? new DataInputStream(new BufferedInputStream(fis, BUFFER_SIZE))
            : new FastBufferedDataInputStream(fis, BUFFER_SIZE);
    try {
        ret.readFields(in);
    } finally {
        IOUtilFunctions.closeSilently((InputStream) in);
        IOUtilFunctions.closeSilently(fis);
    }

    return ret;
}

From source file:org.apache.sysml.runtime.util.LocalFileUtils.java

License:Apache License

/**
 * Reads an arbitrary writable from an input stream, using a fused buffered reader
 * with special support for matrix blocks.
 * //  www.  j a v  a  2 s . c  om
 * @param is input stream to read
 * @param ret hadoop writable
 * @return hadoop writable
 * @throws IOException if IOException occurs
 */
public static Writable readWritableFromStream(InputStream is, Writable ret) throws IOException {
    DataInput in = !(ret instanceof MatrixBlock) ? new DataInputStream(new BufferedInputStream(is, BUFFER_SIZE))
            : new FastBufferedDataInputStream(is, BUFFER_SIZE);
    try {
        ret.readFields(in);
    } finally {
        IOUtilFunctions.closeSilently((InputStream) in);
        IOUtilFunctions.closeSilently(is);
    }

    return ret;
}

From source file:org.commoncrawl.mapred.pipelineV3.crawllistgen.GenBundlesStep.java

License:Open Source License

private static void rawValueToWritable(RawRecordValue rawValue, DataInputBuffer inputBuffer, Writable typeOut)
        throws IOException {
    inputBuffer.reset(rawValue.data.getData(), rawValue.data.getLength());
    typeOut.readFields(inputBuffer);
}

From source file:org.elasticsearch.hadoop.mr.LinkedMapWritable.java

License:Apache License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override//from  ww w.j  a va 2s  .  c  om
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:org.goldenorb.zookeeper.ZookeeperUtils.java

License:Apache License

/**
 * /*  www .j a va 2  s .c o m*/
 * @param byteArray
 *          - byte[]
 * @param writableClass
 *          - Class <? extends Writable>
 * @param orbConf
 *          - OrbConfiguration
 * @returns Writable
 */
public static Writable byteArrayToWritable(byte[] byteArray, Class<? extends Writable> writableClass,
        OrbConfiguration orbConf) {
    ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
    DataInput in = new DataInputStream(bais);

    Writable w = (Writable) ReflectionUtils.newInstance(writableClass, orbConf);
    try {
        w.readFields(in);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return w;
}