Example usage for org.apache.hadoop.io Text Text

List of usage examples for org.apache.hadoop.io Text Text

Introduction

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

Prototype

public Text(byte[] utf8) 

Source Link

Document

Construct from a byte array.

Usage

From source file:com.asakusafw.dag.runtime.internalio.InternalOutputAdapterTest.java

License:Apache License

/**
 * simple case./*w w w  .j a va  2  s  . com*/
 * @throws Exception if failed
 */
@Test
public void simple() throws Exception {
    File folder = temporary.newFolder();
    String pattern = folder.toURI().toString() + "/part-*";
    MockVertexProcessorContext vc = new MockVertexProcessorContext().withResource(StageInfo.class, STAGE)
            .withResource(Configuration.class, new Configuration());
    try (InternalOutputAdapter adapter = new InternalOutputAdapter(vc)) {
        adapter.bind("testing", pattern, Text.class);
        adapter.initialize();

        OutputHandler<? super TaskProcessorContext> handler = adapter.newHandler();
        Result<Text> sink = handler.getSink(Text.class, "testing");

        MockTaskProcessorContext tc = new MockTaskProcessorContext();
        try (Session session = handler.start(tc)) {
            sink.add(new Text("Hello, world!"));
        }
    }
    List<String> results = collect(folder);
    assertThat(results, containsInAnyOrder("Hello, world!"));
}

From source file:com.asakusafw.directio.hive.serde.ValueSerdeFactoryTest.java

License:Apache License

/**
 * decimal by string.//from ww  w. java 2  s . c  om
 */
@Test
public void decimal_by_string() {
    ValueSerde serde = StringValueSerdeFactory.DECIMAL;
    StringObjectInspector inspector = (StringObjectInspector) serde.getInspector();

    DecimalOption option = new DecimalOption(new BigDecimal("123.45"));
    String value = "123.45";

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new Text(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DecimalOption copy = new DecimalOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
}

From source file:com.asakusafw.directio.hive.serde.ValueSerdeFactoryTest.java

License:Apache License

/**
 * date by string./*w w  w.  j av a2  s . com*/
 */
@Test
public void date_by_string() {
    ValueSerde serde = StringValueSerdeFactory.DATE;
    StringObjectInspector inspector = (StringObjectInspector) serde.getInspector();

    DateOption option = new DateOption(new Date(2014, 7, 1));
    String value = "2014-07-01";

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new Text(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DateOption copy = new DateOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
}

From source file:com.asakusafw.directio.hive.serde.ValueSerdeFactoryTest.java

License:Apache License

/**
 * date-time by string./*from  www  .  j av  a2 s.  com*/
 */
@Test
public void datetime_by_string() {
    ValueSerde serde = StringValueSerdeFactory.DATETIME;
    StringObjectInspector inspector = (StringObjectInspector) serde.getInspector();

    DateTimeOption option = new DateTimeOption(new DateTime(2014, 7, 1, 12, 5, 59));
    String value = "2014-07-01 12:05:59";

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new Text(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DateTimeOption copy = new DateTimeOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
}

From source file:com.asakusafw.directio.hive.serde.ValueSerdeFactoryTest.java

License:Apache License

/**
 * date-time by string.//w w w.  j a va  2s.  c  o m
 */
@Test
public void datetime_by_string_w_zeros() {
    ValueSerde serde = StringValueSerdeFactory.DATETIME;
    StringObjectInspector inspector = (StringObjectInspector) serde.getInspector();

    DateTimeOption option = new DateTimeOption(new DateTime(1, 1, 1, 0, 0, 0));
    String value = "0001-01-01 00:00:00";

    assertThat(inspector.copyObject(option), is((Object) option));
    assertThat(inspector.copyObject(option), is(not(sameInstance((Object) option))));
    assertThat(inspector.copyObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveJavaObject(option), is(value));
    assertThat(inspector.getPrimitiveJavaObject(null), is(nullValue()));
    assertThat(inspector.getPrimitiveWritableObject(option), is(new Text(value)));
    assertThat(inspector.getPrimitiveWritableObject(null), is(nullValue()));

    ValueDriver driver = serde.getDriver(inspector);
    DateTimeOption copy = new DateTimeOption();

    driver.set(copy, option);
    assertThat(copy, is(option));
    driver.set(copy, null);
    assertThat(copy.isNull(), is(true));
}

From source file:com.asakusafw.dmdl.directio.csv.driver.CsvFormatEmitterTest.java

License:Apache License

/**
 * A simple case./*w w  w . j a  v  a  2s  . co  m*/
 * @throws Exception if failed
 */
@Test
public void simple() throws Exception {
    ModelLoader loaded = generateJava("simple");
    ModelWrapper model = loaded.newModel("Simple");
    BinaryStreamFormat<?> support = (BinaryStreamFormat<?>) loaded.newObject("csv", "SimpleCsvFormat");

    assertThat(support.getSupportedType(), is((Object) model.unwrap().getClass()));

    BinaryStreamFormat<Object> unsafe = unsafe(support);
    assertThat(unsafe, is(not(instanceOf(Configurable.class))));

    model.set("value", new Text("hello-world"));

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try (ModelOutput<Object> writer = unsafe.createOutput(model.unwrap().getClass(), "hello", output)) {
        writer.write(model.unwrap());
    }

    assertThat(scan(output.toByteArray()), is(Arrays.asList("hello-world")));

    Object buffer = loaded.newModel("Simple").unwrap();
    try (ModelInput<Object> reader = unsafe.createInput(model.unwrap().getClass(), "hello", in(output), 0,
            size(output))) {
        assertThat(reader.readTo(buffer), is(true));
        assertThat(buffer, is(model.unwrap()));
        assertThat(reader.readTo(buffer), is(false));
    }
}

From source file:com.asakusafw.dmdl.directio.csv.driver.CsvFormatEmitterTest.java

License:Apache License

/**
 * All types./*from   w ww . j  av a  2s. co  m*/
 * @throws Exception if failed
 */
@Test
public void types() throws Exception {
    ModelLoader loaded = generateJava("types");
    ModelWrapper model = loaded.newModel("Types");
    BinaryStreamFormat<?> support = (BinaryStreamFormat<?>) loaded.newObject("csv", "TypesCsvFormat");
    assertThat(support.getSupportedType(), is((Object) model.unwrap().getClass()));

    ModelWrapper empty = loaded.newModel("Types");

    ModelWrapper all = loaded.newModel("Types");
    all.set("c_int", 100);
    all.set("c_text", new Text("Hello, DMDL world!"));
    all.set("c_boolean", true);
    all.set("c_byte", (byte) 64);
    all.set("c_short", (short) 1023);
    all.set("c_long", 100000L);
    all.set("c_float", 1.5f);
    all.set("c_double", 2.5f);
    all.set("c_decimal", new BigDecimal("3.1415"));
    all.set("c_date", new Date(2011, 9, 1));
    all.set("c_datetime", new DateTime(2011, 12, 31, 23, 59, 59));

    BinaryStreamFormat<Object> unsafe = unsafe(support);

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try (ModelOutput<Object> writer = unsafe.createOutput(model.unwrap().getClass(), "hello", output)) {
        writer.write(empty.unwrap());
        writer.write(all.unwrap());
    }

    Object buffer = loaded.newModel("Types").unwrap();
    try (ModelInput<Object> reader = unsafe.createInput(model.unwrap().getClass(), "hello", in(output), 0,
            size(output))) {
        assertThat(reader.readTo(buffer), is(true));
        assertThat(buffer, is(empty.unwrap()));
        assertThat(reader.readTo(buffer), is(true));
        assertThat(buffer, is(all.unwrap()));
        assertThat(reader.readTo(buffer), is(false));
    }
}

From source file:com.asakusafw.dmdl.directio.csv.driver.CsvFormatEmitterTest.java

License:Apache License

/**
 * with attributes./*from www.  j  av a 2 s.c  om*/
 * @throws Exception if failed
 */
@Test
public void attributes() throws Exception {
    ModelLoader loaded = generateJava("attributes");
    ModelWrapper model = loaded.newModel("Model");
    model.set("text_value", new Text("\u3042\u3044\u3046\u3048\u304a"));
    model.set("true_value", true);
    model.set("false_value", false);
    model.set("date_value", new Date(2011, 10, 11));
    model.set("date_time_value", new DateTime(2011, 1, 2, 13, 14, 15));
    BinaryStreamFormat<Object> support = unsafe(loaded.newObject("csv", "ModelCsvFormat"));

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try (ModelOutput<Object> writer = support.createOutput(model.unwrap().getClass(), "hello", output)) {
        writer.write(model.unwrap());
    }

    String[][] results = parse(5, new String(
            dump(new GZIPInputStream(new ByteArrayInputStream(output.toByteArray()))), "ISO-2022-jp"));
    assertThat(results,
            is(new String[][] { { "text_value", "true_value", "false_value", "date_value", "date_time_value" },
                    { "\u3042\u3044\u3046\u3048\u304a", "T", "F", "2011/10/11", "2011/01/02+13:14:15" }, }));
}

From source file:com.asakusafw.dmdl.directio.csv.driver.CsvFormatEmitterTest.java

License:Apache License

/**
 * With compression.// w w  w . java2  s.co m
 * @throws Exception if failed
 */
@Test
public void compression() throws Exception {
    ModelLoader loaded = generateJava("compress");
    ModelWrapper model = loaded.newModel("Compress");
    BinaryStreamFormat<?> support = (BinaryStreamFormat<?>) loaded.newObject("csv", "CompressCsvFormat");

    assertThat(support.getSupportedType(), is((Object) model.unwrap().getClass()));

    BinaryStreamFormat<Object> unsafe = unsafe(support);
    assertThat(unsafe, is(instanceOf(Configurable.class)));

    model.set("value", new Text("hello"));

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try (ModelOutput<Object> writer = unsafe.createOutput(model.unwrap().getClass(), "hello", output)) {
        writer.write(model.unwrap());
    }

    assertThat(scan(dump(new GZIPInputStream(new ByteArrayInputStream(output.toByteArray())))),
            is(Arrays.asList("hello")));

    Object buffer = loaded.newModel("Compress").unwrap();
    try (ModelInput<Object> reader = unsafe.createInput(model.unwrap().getClass(), "hello", in(output), 0,
            size(output))) {
        assertThat(reader.readTo(buffer), is(true));
        assertThat(buffer, is(model.unwrap()));
        assertThat(reader.readTo(buffer), is(false));
    }
}

From source file:com.asakusafw.dmdl.directio.csv.driver.CsvFormatEmitterTest.java

License:Apache License

/**
 * with header./*from   w w w .j  a v a  2 s.c  om*/
 * @throws Exception if failed
 */
@Test
public void header() throws Exception {
    ModelLoader loaded = generateJava("field_name");
    ModelWrapper model = loaded.newModel("Model");
    BinaryStreamFormat<Object> support = unsafe(loaded.newObject("csv", "ModelCsvFormat"));

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try (ModelOutput<Object> writer = support.createOutput(model.unwrap().getClass(), "hello", output)) {
        model.set("value", new Text("Hello, world!"));
        writer.write(model.unwrap());
    }

    String[][] results = parse(1, new String(output.toByteArray(), "UTF-8"));
    assertThat(results, is(new String[][] { { "title" }, { "Hello, world!" }, }));
}