Example usage for org.apache.hadoop.io DataInputBuffer reset

List of usage examples for org.apache.hadoop.io DataInputBuffer reset

Introduction

In this page you can find the example usage for org.apache.hadoop.io DataInputBuffer reset.

Prototype

public void reset(byte[] input, int length) 

Source Link

Document

Resets the data that the buffer reads.

Usage

From source file:TestIRecord.java

License:Open Source License

public void testPersistable() throws IOException {
    IRecord irc = new IRecord();
    TreeMap<Short, IFieldType> fieldtypes = new TreeMap<Short, IFieldType>();
    fieldtypes.put((short) 0, new IFieldType(ConstVar.FieldType_Double));
    fieldtypes.put((short) 1, new IFieldType(ConstVar.FieldType_Int));
    irc.setFieldTypes(fieldtypes);/*from w  w  w.  j a  va 2s .co  m*/
    irc.addFieldValue(new IFieldValue((double) 4, (short) 0));
    irc.addFieldValue(new IFieldValue(4, (short) 1));

    DataOutputBuffer dob = new DataOutputBuffer();
    irc.persistent(dob);
    byte[] data = dob.getData();

    DataInputBuffer dib = new DataInputBuffer();
    dib.reset(data, data.length);

    IRecord irc2 = new IRecord();
    irc2.setFieldTypes(fieldtypes);
    irc2.unpersistent(dib);

    assertTrue(irc.fieldValues().get((short) 0).compareTo(irc2.fieldValues().get((short) 0)) == 0);
    assertTrue(irc.fieldValues().get((short) 1).compareTo(irc2.fieldValues().get((short) 1)) == 0);

}

From source file:TestIHead.java

License:Open Source License

public void testPersistable() throws IOException {
    IHead ih = new IHead();
    ih.setCompress((byte) 1);
    ih.setCompressStyle((byte) 1);
    ih.setEncode((byte) 1);
    ih.setEncodeStyle((byte) 1);
    ih.setLineindex((byte) 1);
    ih.setVar((byte) 1);
    ih.setVer((short) 1);
    ih.setMagic(1);/*w  w w .ja va 2  s.c o  m*/
    ih.setPrimaryIndex((short) 1);

    IUserDefinedHeadInfo udi = new IUserDefinedHeadInfo();
    udi.addInfo(0, "aaa");
    udi.addInfo(1, "bbb");

    ih.setUserDefinedInfo(udi);
    IFieldMap fieldMap = new IFieldMap();
    fieldMap.addFieldType(new IFieldType.IFieldByteType());
    fieldMap.addFieldType(new IFieldType.IFieldShortType());
    fieldMap.addFieldType(new IFieldType.IFieldIntType());
    fieldMap.addFieldType(new IFieldType.IFieldLongType());
    fieldMap.addFieldType(new IFieldType.IFieldFloatType());
    fieldMap.addFieldType(new IFieldType.IFieldDoubleType());
    ih.setFieldMap(fieldMap);

    DataOutputBuffer dob = new DataOutputBuffer();
    ih.persistent(dob);
    byte[] data = dob.getData();
    DataInputBuffer dib = new DataInputBuffer();
    dib.reset(data, data.length);

    IHead ih2 = new IHead();
    ih2.unpersistent(dib);

    assertEquals(ih2.getMagic(), ih.getMagic());
    assertEquals(ih2.getCompress(), ih.getCompress());
    assertEquals(ih2.getCompressStyle(), ih.getCompressStyle());
    assertEquals(ih2.getEncode(), ih.getEncode());
    assertEquals(ih2.getEncodeStyle(), ih.getEncodeStyle());
    assertEquals(ih2.getPrimaryIndex(), ih.getPrimaryIndex());
    assertEquals(ih2.getVar(), ih.getVar());
    assertEquals(ih2.getVer(), ih.getVer());
    assertEquals(ih2.lineindex(), ih.lineindex());

}

From source file:TestIKeyIndex.java

License:Open Source License

public void testPersistable() throws IOException {
    IFieldValue beginkey = new IFieldValue(100);
    IFieldValue endkey = new IFieldValue(1000);
    IKeyIndex iki = new IKeyIndex(beginkey, endkey, 100);

    DataOutputBuffer dob = new DataOutputBuffer();
    iki.persistent(dob);/* www  .j ava2 s.co  m*/
    byte[] data = dob.getData();

    DataInputBuffer dib = new DataInputBuffer();
    dib.reset(data, data.length);

    IKeyIndex iki2 = new IKeyIndex(beginkey.fieldType());
    iki2.unpersistent(dib);

    assertTrue(iki.beginkey().compareTo(iki2.beginkey()) == 0);
    assertTrue(iki.endkey().compareTo(iki2.endkey()) == 0);
    assertEquals(iki.recnum(), iki2.recnum());
}

From source file:TestIUnitIndex.java

License:Open Source License

public void testIUnitIndexWrite() throws IOException {
    IFileInfo fileInfo = TestUtil.genfileinfo(false, 2);

    IUnitIndex iui = new IUnitIndex(fileInfo);

    iui.update(TestUtil.genunitinfo(fileInfo, 0, 0, 5));
    iui.update(TestUtil.genunitinfo(fileInfo, 0, 1, 5));
    iui.update(TestUtil.genunitinfo(fileInfo, 0, 2, 5));
    iui.update(TestUtil.genunitinfo(fileInfo, 0, 3, 5));
    iui.update(TestUtil.genunitinfo(fileInfo, 0, 4, 5));

    System.out.println(iui.getUnitnum());

    assertEquals(0, iui.getUnitid(0));/*from w w w  . j av a  2s . com*/
    assertEquals(0, iui.getUnitid(4));
    assertEquals(1, iui.getUnitid(5));
    assertEquals(1, iui.getUnitid(9));
    assertEquals(4, iui.getUnitid(24));
    assertEquals(5, iui.getUnitid(25));

    DataOutputBuffer dob = new DataOutputBuffer();
    iui.persistent(dob);
    byte[] data = dob.getData();
    DataInputBuffer dib = new DataInputBuffer();
    dib.reset(data, data.length);

    IUnitIndex iui2 = new IUnitIndex(fileInfo);
    iui2.unpersistent(dib);
    for (int j = 0; j < iui.getUnitnum(); j++) {
        assertEquals(iui.getUnitOffset(j), iui2.getUnitOffset(j));
        assertTrue(iui.getKeyIndex(j).compareTo(iui2.getKeyIndex(j)) == 0);
        assertTrue(iui.getLineIndex(j).compareTo(iui2.getLineIndex(j)) == 0);
    }

}

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

License:Apache License

/**
 * primitives./*from  w  w  w  . ja v  a2s.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.dmdl.thundergate.driver.ModelInputDriverTest.java

License:Apache License

/**
 * input/output simple records.//from ww w  . ja  v a  2  s  .com
 * @throws Exception if test was failed
 */
@SuppressWarnings("unchecked")
@Test
public void simple_record() throws Exception {
    ModelLoader loader = generateJava("simple_record");

    Class<?> type = loader.modelType("Simple");
    assertThat(type.isAnnotationPresent(ModelInputLocation.class), is(true));
    assertThat(type.isAnnotationPresent(ModelOutputLocation.class), is(true));

    ModelWrapper object = loader.newModel("Simple");
    DataOutputBuffer output = new DataOutputBuffer();
    try (ModelOutput<Object> modelOut = (ModelOutput<Object>) type.getAnnotation(ModelOutputLocation.class)
            .value().getDeclaredConstructor(RecordEmitter.class)
            .newInstance(new TsvEmitter(new OutputStreamWriter(output, "UTF-8")))) {
        object.set("sid", 1L);
        object.set("value", new Text("hello"));
        modelOut.write(object.unwrap());

        object.set("sid", 2L);
        object.set("value", new Text("world"));
        modelOut.write(object.unwrap());

        object.set("sid", 3L);
        object.set("value", null);
        modelOut.write(object.unwrap());
    }

    DataInputBuffer input = new DataInputBuffer();
    input.reset(output.getData(), output.getLength());
    try (ModelInput<Object> modelIn = (ModelInput<Object>) type.getAnnotation(ModelInputLocation.class).value()
            .getDeclaredConstructor(RecordParser.class)
            .newInstance(new TsvParser(new InputStreamReader(input, "UTF-8")))) {
        ModelWrapper copy = loader.newModel("Simple");

        modelIn.readTo(copy.unwrap());
        assertThat(copy.get("sid"), is((Object) 1L));
        assertThat(copy.get("value"), is((Object) new Text("hello")));

        modelIn.readTo(copy.unwrap());
        assertThat(copy.get("sid"), is((Object) 2L));
        assertThat(copy.get("value"), is((Object) new Text("world")));

        modelIn.readTo(copy.unwrap());
        assertThat(copy.get("sid"), is((Object) 3L));
        assertThat(copy.getOption("value").isNull(), is(true));

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

From source file:com.asakusafw.dmdl.thundergate.driver.ModelInputDriverTest.java

License:Apache License

/**
 * all primitive types.//from w  w w.  j  a v  a 2  s  . c o  m
 * @throws Exception if test was failed
 */
@SuppressWarnings("unchecked")
@Test
public void primitives() throws Exception {
    ModelLoader loader = generateJava("primitives");

    Class<?> type = loader.modelType("Primitives");
    assertThat(type.isAnnotationPresent(ModelInputLocation.class), is(true));
    assertThat(type.isAnnotationPresent(ModelOutputLocation.class), is(true));

    ModelWrapper object = loader.newModel("Primitives");

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

    DataOutputBuffer output = new DataOutputBuffer();
    try (ModelOutput<Object> modelOut = (ModelOutput<Object>) type.getAnnotation(ModelOutputLocation.class)
            .value().getDeclaredConstructor(RecordEmitter.class)
            .newInstance(new TsvEmitter(new OutputStreamWriter(output, "UTF-8")))) {
        modelOut.write(object.unwrap());
        modelOut.write(object.unwrap());
        modelOut.write(object.unwrap());
    }

    DataInputBuffer input = new DataInputBuffer();
    input.reset(output.getData(), output.getLength());
    try (ModelInput<Object> modelIn = (ModelInput<Object>) type.getAnnotation(ModelInputLocation.class).value()
            .getDeclaredConstructor(RecordParser.class)
            .newInstance(new TsvParser(new InputStreamReader(input, "UTF-8")))) {
        ModelWrapper copy = loader.newModel("Primitives");
        modelIn.readTo(copy.unwrap());
        assertThat(object.unwrap(), equalTo(copy.unwrap()));
        assertThat(input.read(), is(-1));
    }
}

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

License:Apache License

static <T extends Writable> T des(T writable, byte[] serialized) throws IOException {
    DataInputBuffer buf = new DataInputBuffer();
    buf.reset(serialized, serialized.length);
    writable.readFields(buf);/*from   www . j  av a  2 s .  co  m*/
    return writable;
}

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

License:Apache License

static <T extends Writable> T read(T writable, byte[] bytes) {
    DataInputBuffer buffer = new DataInputBuffer();
    buffer.reset(bytes, bytes.length);
    try {// w w  w . j  a va 2s. co m
        writable.readFields(buffer);
    } catch (IOException e) {
        throw new AssertionError(e);
    }
    return writable;
}

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

License:Apache License

static <T extends Writable> T read(T writable, byte[] bytes) {
    DataInputBuffer buffer = new DataInputBuffer();
    buffer.reset(bytes, bytes.length);
    try {/*  w  w  w  . j  a  v a2s .  c  o  m*/
        writable.readFields(buffer);
        assertThat("Enf of Stream", buffer.read(), is(-1));
    } catch (IOException e) {
        throw new AssertionError(e);
    }
    return writable;
}