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:com.aliyun.openservices.tablestore.hadoop.Utils.java

License:Apache License

static String serialize(Writable w) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(os);
    try {/*from   w  w  w .ja va2  s .c o m*/
        w.write(out);
        out.close();
    } catch (IOException ex) {
        // intend to ignore
    }
    byte[] buf = os.toByteArray();
    return Base64.encodeBase64String(buf);
}

From source file:com.asakusafw.dmdl.java.emitter.driver.WritableDriverTest.java

License:Apache License

/**
 * primitives.//from   w  w w.  j ava2  s .co  m
 * @throws Exception if test was failed
 */
@Test
public void primitives() throws Exception {
    ModelLoader loader = generate();
    ModelWrapper object = loader.newModel("Primitives");
    assertThat(object.unwrap(), instanceOf(Writable.class));

    object.set("type_boolean", true);
    object.set("type_byte", (byte) 64);
    object.set("type_short", (short) 256);
    object.set("type_int", 100);
    object.set("type_long", 200L);
    object.set("type_float", 300.f);
    object.set("type_double", 400.d);
    object.set("type_decimal", new BigDecimal("1234.567"));
    object.set("type_text", new Text("Hello, world!"));
    object.set("type_date", new Date(2011, 3, 31));
    object.set("type_datetime", new DateTime(2011, 3, 31, 23, 30, 1));

    Writable writable = (Writable) object.unwrap();

    DataOutputBuffer output = new DataOutputBuffer();
    writable.write(output);

    Writable copy = (Writable) loader.newModel("Primitives").unwrap();
    DataInputBuffer input = new DataInputBuffer();
    input.reset(output.getData(), output.getLength());
    copy.readFields(input);

    assertThat(input.read(), is(-1));
    assertThat(writable, equalTo(copy));
}

From source file:com.asakusafw.modelgen.emitter.EmitterTestRoot.java

License:Apache License

/**
 * Writable?????????//w  w  w .jav  a  2  s .c  o m
 * @param <T> ?
 * @param value ?
 * @return ??
 */
@SuppressWarnings("unchecked")
protected <T> T restore(T value) {
    assertThat(value, instanceOf(Writable.class));
    Writable writable = (Writable) value;
    try {
        ByteArrayOutputStream write = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(write);
        writable.write(out);
        out.flush();

        ByteArrayInputStream read = new ByteArrayInputStream(write.toByteArray());
        ObjectInputStream in = new ObjectInputStream(read);
        Writable copy = writable.getClass().newInstance();
        copy.readFields(in);
        assertThat(in.read(), is(-1));
        assertThat(copy, is((Writable) value));
        assertThat(copy.hashCode(), is(value.hashCode()));
        return (T) copy;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:com.asakusafw.runtime.flow.join.LookUpKey.java

License:Apache License

/**
 * Appends an element into the buffer.//w w w  .ja va2s  . c  o m
 * @param writable the target element
 * @throws IOException if error occurred while appending the element
 * @throws IllegalArgumentException if the parameter is {@code null}
 */
public void add(Writable writable) throws IOException {
    if (writable == null) {
        throw new IllegalArgumentException("writable must not be null"); //$NON-NLS-1$
    }
    writable.write(buffer);
}

From source file:com.asakusafw.runtime.io.util.WritableTestRoot.java

License:Apache License

static byte[] ser(Writable writable) throws IOException {
    DataOutputBuffer out = new DataOutputBuffer();
    writable.write(out);
    byte[] results = Arrays.copyOfRange(out.getData(), 0, out.getLength());
    return results;
}

From source file:com.asakusafw.runtime.stage.collector.SortableSlot.java

License:Apache License

/**
 * Adds an object.//from w  w  w . ja  va 2 s  .  c  om
 * @param data the object
 * @throws IOException if failed to add the value
 */
public void add(Writable data) throws IOException {
    data.write(buffer);
}

From source file:com.asakusafw.runtime.stage.collector.SortableSlotTest.java

License:Apache License

static byte[] write(Writable writable) {
    DataOutputBuffer buffer = new DataOutputBuffer();
    buffer.reset();//  w w w  .  j  a v  a2s .  co m
    try {
        writable.write(buffer);
    } catch (IOException e) {
        throw new AssertionError(e);
    }
    return Arrays.copyOf(buffer.getData(), buffer.getLength());
}

From source file:com.asakusafw.runtime.stage.collector.WritableSlot.java

License:Apache License

/**
 * Stores the copy of the target object.
 * @param data the target object/*from   w ww  . j  av  a2 s .c o  m*/
 * @throws IOException if failed to store the object
 */
public void store(Writable data) throws IOException {
    buffer.reset(0, 0);
    data.write(buffer);
}

From source file:com.asakusafw.runtime.value.ValueOptionTestRoot.java

License:Apache License

byte[] toBytes(Writable value) {
    ByteArrayOutputStream write = new ByteArrayOutputStream();
    try (DataOutputStream out = new DataOutputStream(write)) {
        value.write(out);
    } catch (IOException e) {
        throw new AssertionError(e);
    }//from   w w  w  . j  a  va 2 s .  c  om
    return write.toByteArray();
}

From source file:com.axiomine.largecollections.utilities.SerDeUtils.java

License:Apache License

public static byte[] serializeWritable(Writable writable) {
    byte[] outBA = null;
    ByteArrayOutputStream out = null;
    DataOutputStream dataOut = null;
    try {/*  w w  w.  j a  v  a 2s.c  o m*/
        out = new ByteArrayOutputStream();
        dataOut = new DataOutputStream(out);
        writable.write(dataOut);
        outBA = out.toByteArray();
    } catch (Exception ex) {
        Throwables.propagate(ex);
    } finally {
        if (outBA != null) {
            try {
                dataOut.close();
            } catch (Exception ex) {
                //Nothing to do
                ex.printStackTrace();
            }

        }
    }
    return outBA;
}