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.asakusafw.dmdl.java.emitter.driver.WritableDriverTest.java

License:Apache License

/**
 * primitives./*  www .j  a  v  a  2  s.c  om*/
 * @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  ww  . ja  v a 2s  .  c om
 * @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.stage.collector.WritableSlot.java

License:Apache License

/**
 * Loads the contents in this object into the target object.
 * @param data the target object//  w w w  .  j  av  a 2s. c  o m
 * @throws IOException if failed to load the contents
 */
public void loadTo(Writable data) throws IOException {
    buffer.reset(0, buffer.getReadLimit());
    data.readFields(buffer);
}

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

License:Apache License

@SuppressWarnings("unchecked")
private <T extends Writable> T restoreWritable(T value) {
    try {/*ww w. ja v a  2s  . c  o  m*/
        ByteArrayInputStream read = new ByteArrayInputStream(toBytes(value));
        DataInputStream in = new DataInputStream(read);
        Writable copy = value.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.axiomine.largecollections.utilities.SerDeUtils.java

License:Apache License

public static Writable deserializeWritable(Writable writable, byte[] bytes) {
    try {//from   w  ww  .  j a va 2 s .  co  m
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        DataInputStream dataIn = new DataInputStream(in);
        writable.readFields(dataIn);
        dataIn.close();
        return writable;
    } catch (Exception ex) {
        throw Throwables.propagate(ex);
    }
}

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

License:Apache License

public static Writable deserializeWritable(Class<Writable> wc, byte[] bytes) {
    try {//from  w  w w .jav a  2s. c o m
        Writable writable = wc.newInstance();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        DataInputStream dataIn = new DataInputStream(in);
        writable.readFields(dataIn);
        dataIn.close();
        return writable;
    } catch (Exception ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:com.bah.culvert.test.Utils.java

License:Apache License

/**
 * Checks if the writable will successfully read and write using
 * {@link Writable}'s methods./*from   w w  w.j av a2s.  c  o m*/
 * @param writeable
 * @return The instance read in using
 *         {@link Writable#readFields(java.io.DataInput)}. Can be used for
 *         checking equality.
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws IOException
 */
public static Writable testReadWrite(Writable writeable)
        throws InstantiationException, IllegalAccessException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    writeable.write(dos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream dis = new DataInputStream(bais);
    Writable inst = writeable.getClass().newInstance();
    inst.readFields(dis);
    return inst;
}

From source file:com.cloudera.crunch.type.writable.GenericArrayWritable.java

License:Open Source License

public void readFields(DataInput in) throws IOException {
    values = new Writable[in.readInt()]; // construct values
    if (values.length > 0) {
        String valueType = Text.readString(in);
        setValueType(valueType);/* w  ww .ja  v  a 2 s .  co  m*/
        for (int i = 0; i < values.length; i++) {
            Writable value = WritableFactories.newInstance(valueClass);
            value.readFields(in); // read a value
            values[i] = value; // store it in values
        }
    }
}

From source file:com.cloudera.impala.security.PersistedDelegationTokenSecretManager.java

License:Apache License

public static void decodeWritable(Writable w, String idStr) throws IOException {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(Base64.decodeBase64(idStr)));
    w.readFields(in);
}

From source file:com.dasasian.chok.testutil.AbstractWritableTest.java

License:Apache License

protected Writable readWritable(DataOutputBuffer out, Writable writable) throws IOException {
    DataInputBuffer inputBuffer = new DataInputBuffer();
    inputBuffer.reset(out.getData(), out.getData().length);
    writable.readFields(inputBuffer);
    return writable;
}