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

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

Introduction

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

Prototype

void write(DataOutput out) throws IOException;

Source Link

Document

Serialize the fields of this object to out.

Usage

From source file:org.apache.mahout.common.DummyRecordWriter.java

License:Apache License

private Writable cloneWritable(Writable original) throws IOException {

    Writable clone;/* www. j  a  va  2  s  .c  o m*/
    try {
        clone = original.getClass().asSubclass(Writable.class).newInstance();
    } catch (Exception e) {
        throw new RuntimeException("Unable to instantiate writable!", e);
    }
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    original.write(new DataOutputStream(bytes));
    clone.readFields(new DataInputStream(new ByteArrayInputStream(bytes.toByteArray())));

    return clone;
}

From source file:org.apache.mahout.df.DFUtils.java

License:Apache License

public static void storeWritable(Configuration conf, Path path, Writable writable) throws IOException {
    FileSystem fs = path.getFileSystem(conf);

    FSDataOutputStream out = fs.create(path);
    writable.write(out);
    out.close();//  www.ja v  a 2  s  .c o  m
}

From source file:org.apache.mahout.h2obindings.drm.H2OBCast.java

License:Apache License

/**
 * Internal method to serialize the object.
 *
 * @param w Either MatrixWritable or VectorWritable corresponding to
 *          either Matrix or Vector as the class is typed.
 * @return serialized sequence of bytes.
 *//*  w  w w.  j a  va2s.c  o m*/
private byte[] serialize(Writable w) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        w.write(oos);
        oos.close();
    } catch (java.io.IOException e) {
        return null;
    }
    return bos.toByteArray();
}

From source file:org.apache.mahout.math.MatrixWritableTest.java

License:Apache License

private static void writeAndRead(Writable toWrite, Writable toRead) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {//from ww w. jav a  2  s  .co  m
        toWrite.write(dos);
    } finally {
        Closeables.close(dos, false);
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream dis = new DataInputStream(bais);
    try {
        toRead.readFields(dis);
    } finally {
        Closeables.close(dis, true);
    }
}

From source file:org.apache.mahout.math.VectorTest.java

License:Apache License

private static void writeAndRead(Writable toWrite, Writable toRead) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutput dos = new DataOutputStream(baos);
    toWrite.write(dos);

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInput dis = new DataInputStream(bais);
    toRead.readFields(dis);/*  ww  w .j a v  a 2s.c  om*/
}

From source file:org.apache.mahout.math.VectorWritableTest.java

License:Apache License

private static void writeAndRead(Writable toWrite, Writable toRead) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {//from   w w w  .java2  s.  c o  m
        toWrite.write(dos);
    } finally {
        Closeables.close(dos, false);
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream dis = new DataInputStream(bais);
    try {
        toRead.readFields(dis);
    } finally {
        Closeables.close(dos, true);
    }
}

From source file:org.apache.nutch.crawl.TestMapWritable.java

License:Apache License

/** Utility method for testing writables, from hadoop code */
public void testWritable(Writable before) throws Exception {
    DataOutputBuffer dob = new DataOutputBuffer();
    before.write(dob);

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

    Writable after = (Writable) before.getClass().newInstance();
    after.readFields(dib);//  ww w  .j  a  va  2 s. com

    assertEquals(before, after);
}

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);

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

    Writable after = (Writable) before.getClass().newInstance();
    if (conf != null) {
        ((Configurable) after).setConf(conf);
    }//from  w  ww. j  a v a 2s  .c  o  m
    after.readFields(dib);
    return after;
}

From source file:org.apache.oozie.util.WritableUtils.java

License:Apache License

/**
 * Write a writable to a byte array.//w  w  w . j  a  v  a 2  s  . c om
 *
 * @param writable writable to write.
 * @return array containing the serialized writable.
 */
public static byte[] toByteArray(Writable writable) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream daos = new DataOutputStream(baos);
        writable.write(daos);
        daos.close();
        return baos.toByteArray();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

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);
    out.flush();/*  w  ww. j a va 2 s.  c om*/
    DataInputBuffer in = new DataInputBuffer();
    in.reset(out.getData(), out.getLength());
    destination.readFields(in);
}