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.dmdl.windgate.jdbc.driver.JdbcSupportEmitterTest.java

License:Apache License

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

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

    assertThat(support.isSupported(list("VALUE")), is(true));
    assertThat(support.isSupported(list("VALUE", "VALUE")), is(false));
    assertThat(support.isSupported(JdbcSupportEmitterTest.<String>list()), is(false));
    assertThat(support.isSupported(list("INVALID")), is(false));
    assertThat(support.isSupported(list("VALUE", "INVALID")), is(false));

    DataModelJdbcSupport<Object> unsafe = unsafe(support);
    h2.executeFile("simple.sql");
    try (Connection conn = h2.open();
            PreparedStatement ps = conn.prepareStatement("INSERT INTO SIMPLE (VALUE) VALUES (?)")) {
        DataModelPreparedStatement<Object> p = unsafe.createPreparedStatementSupport(ps, list("VALUE"));
        model.set("value", new Text("Hello, world!"));
        p.setParameters(model.unwrap());
        ps.executeUpdate();
        ps.close();

        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery("SELECT VALUE FROM SIMPLE");
        DataModelResultSet<Object> r = unsafe.createResultSetSupport(rs, list("VALUE"));
        assertThat(r.next(model.unwrap()), is(true));
        assertThat(model.get("value"), is((Object) new Text("Hello, world!")));
        assertThat(r.next(model.unwrap()), is(false));
    }
}

From source file:com.asakusafw.dmdl.windgate.jdbc.driver.JdbcSupportEmitterTest.java

License:Apache License

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

    List<String> list = list(new String[] { "C_INT", "C_TEXT", "C_BOOLEAN", "C_BYTE", "C_SHORT", "C_LONG",
            "C_FLOAT", "C_DOUBLE", "C_DECIMAL", "C_DATE", "C_DATETIME", });
    assertThat(support.isSupported(list), is(true));

    DataModelJdbcSupport<Object> unsafe = unsafe(support);
    h2.executeFile("types.sql");
    try (Connection conn = h2.open();
            PreparedStatement ps = conn
                    .prepareStatement(MessageFormat.format("INSERT INTO TYPES ({0}) VALUES ({1})", join(list),
                            join(Collections.nCopies(list.size(), "?"))))) {
        DataModelPreparedStatement<Object> p = unsafe.createPreparedStatementSupport(ps, list);

        // set nulls
        ModelWrapper nulls = loaded.newModel("Types");
        p.setParameters(nulls.unwrap());
        ps.executeUpdate();

        // text only
        ModelWrapper text = loaded.newModel("Types");
        text.set("c_text", new Text("Hello, world!"));
        p.setParameters(text.unwrap());
        ps.executeUpdate();

        // all 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));
        p.setParameters(all.unwrap());
        ps.executeUpdate();

        ps.close();

        Statement s = conn.createStatement();
        ResultSet rs = s
                .executeQuery(MessageFormat.format("SELECT {0} FROM TYPES ORDER BY ORDINAL", join(list)));
        DataModelResultSet<Object> r = unsafe.createResultSetSupport(rs, list);
        ModelWrapper buffer = loaded.newModel("Types");

        assertThat(r.next(buffer.unwrap()), is(true));
        assertThat(buffer.unwrap(), is(nulls.unwrap()));

        assertThat(r.next(buffer.unwrap()), is(true));
        assertThat(buffer.unwrap(), is(text.unwrap()));

        assertThat(r.next(buffer.unwrap()), is(true));
        assertThat(buffer.unwrap(), is(all.unwrap()));

        assertThat(r.next(buffer.unwrap()), is(false));
    }
}

From source file:com.asakusafw.dmdl.windgate.stream.driver.StreamSupportEmitterTest.java

License:Apache License

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

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

    DataModelStreamSupport<Object> unsafe = unsafe(support);

    model.set("value", new Text("Hello, world!"));
    byte[] data = write(unsafe, model);

    Object buffer = loaded.newModel("Simple").unwrap();
    DataModelReader<Object> reader = unsafe.createReader("example", new ByteArrayInputStream(data));
    assertThat(reader.readTo(buffer), is(true));
    assertThat(buffer, is(buffer));
    assertThat(reader.readTo(buffer), is(false));
}

From source file:com.asakusafw.dmdl.windgate.stream.driver.StreamSupportEmitterTest.java

License:Apache License

/**
 * All types./* www.  ja  v a2s . co m*/
 * @throws Exception if failed
 */
@Test
public void types() throws Exception {
    ModelLoader loaded = generateJava("types");
    ModelWrapper model = loaded.newModel("Types");
    DataModelStreamSupport<?> support = (DataModelStreamSupport<?>) loaded.newObject("stream",
            "TypesStreamSupport");
    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));

    DataModelStreamSupport<Object> unsafe = unsafe(support);
    byte[] data = write(unsafe, empty, all);

    Object buffer = loaded.newModel("Types").unwrap();
    DataModelReader<Object> reader = unsafe.createReader("example", new ByteArrayInputStream(data));
    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.lang.compiler.extension.testdriver.InternalExporterRetrieverTest.java

License:Apache License

/**
 * retrieve./* ww  w  .  j a va 2  s  .  c o m*/
 * @throws Exception if test was failed
 */
@Test
public void retrieve() throws Exception {
    String base = new File(folder.getRoot(), "part").toURI().toString();
    String prefix = base + "-*";
    String actual = base + "-0000";

    InternalExporterDescription exporter = new InternalExporterDescription.Basic(Text.class, prefix);
    InternalExporterRetriever retriever = new InternalExporterRetriever(factory);

    putText(actual, "Hello, world!", "This is a test.");

    MockTextDefinition definition = new MockTextDefinition();
    try (DataModelSource result = retriever.createSource(definition, exporter, EMPTY)) {
        DataModelReflection ref;
        ref = result.next();
        assertThat(ref, is(not(nullValue())));
        assertThat(definition.toObject(ref), is(new Text("Hello, world!")));

        ref = result.next();
        assertThat(ref, is(not(nullValue())));
        assertThat(definition.toObject(ref), is(new Text("This is a test.")));

        ref = result.next();
        assertThat(ref, is(nullValue()));
    }
}

From source file:com.asakusafw.lang.compiler.extension.testdriver.InternalExporterRetrieverTest.java

License:Apache License

/**
 * prepare.//w w  w.j a  v  a 2  s  . c  o  m
 * @throws Exception if test was failed
 */
@Test
public void prepare() throws Exception {
    String base = new File(folder.getRoot(), "part").toURI().toString();
    String prefix = base + "-*";

    InternalExporterRetriever target = new InternalExporterRetriever(factory);
    try (ModelOutput<Text> open = target.createOutput(new MockTextDefinition(),
            new InternalExporterDescription.Basic(Text.class, prefix), EMPTY)) {
        open.write(new Text("Hello, world!"));
        open.write(new Text("This is a test."));
    }
    try (ModelInput<Text> input = TemporaryStorage.openInput(factory.newInstance(), Text.class,
            new Path(InternalImporterPreparator.resolvePathPrefix(EMPTY, prefix)))) {
        Text text = new Text();
        assertThat(input.readTo(text), is(true));
        assertThat(text.toString(), is("Hello, world!"));
        assertThat(input.readTo(text), is(true));
        assertThat(text.toString(), is("This is a test."));
        assertThat(input.readTo(text), is(false));
    }
}

From source file:com.asakusafw.lang.compiler.extension.testdriver.InternalExporterRetrieverTest.java

License:Apache License

private void putText(String path, String... lines) throws IOException {
    try (ModelOutput<Text> output = TemporaryStorage.openOutput(factory.newInstance(), Text.class,
            new Path(path))) {
        for (String s : lines) {
            output.write(new Text(s));
        }/*from   ww  w .j  av  a  2  s .  co m*/
    }
}

From source file:com.asakusafw.lang.compiler.extension.testdriver.InternalImporterPreparatorTest.java

License:Apache License

/**
 * prepare.//from  w w  w  .  j a  va  2s .co  m
 * @throws Exception if test was failed
 */
@Test
public void prepare() throws Exception {
    String base = new File(folder.getRoot(), "part").toURI().toString();
    String prefix = base + "-*";

    InternalImporterPreparator target = new InternalImporterPreparator(factory);
    try (ModelOutput<Text> open = target.createOutput(new MockTextDefinition(),
            new InternalImporterDescription.Basic(Text.class, prefix), EMPTY)) {
        open.write(new Text("Hello, world!"));
        open.write(new Text("This is a test."));
    }
    try (ModelInput<Text> input = TemporaryStorage.openInput(factory.newInstance(), Text.class,
            new Path(InternalImporterPreparator.resolvePathPrefix(EMPTY, prefix)))) {
        Text text = new Text();
        assertThat(input.readTo(text), is(true));
        assertThat(text.toString(), is("Hello, world!"));
        assertThat(input.readTo(text), is(true));
        assertThat(text.toString(), is("This is a test."));
        assertThat(input.readTo(text), is(false));
    }
}

From source file:com.asakusafw.runtime.directio.hadoop.SequenceFileFormatTest.java

License:Apache License

/**
 * Test for input./*  w w w  .j a  v a  2  s . c  om*/
 * @throws Exception if failed
 */
@Test
public void input_largerecord() throws Exception {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < 1000000; i++) {
        buf.append("Hello, world!");
    }
    Text record = new Text(buf.toString());

    final int count = 5;
    LocalFileSystem fs = FileSystem.getLocal(conf);
    Path path = new Path(folder.newFile("testing").toURI());
    try (SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, path, LongWritable.class,
            Text.class)) {
        LongWritable k = new LongWritable();
        Text v = new Text();
        for (int i = 0; i < count; i++) {
            k.set(i);
            v.set(record);
            writer.append(k, v);
        }
    }

    long fileLen = fs.getFileStatus(path).getLen();
    StringOption value = new StringOption();
    int index = 0;
    long offset = 0;
    while (offset < fileLen) {
        long length = SequenceFile.SYNC_INTERVAL * 2;
        length = Math.min(length, fileLen - offset);
        try (ModelInput<StringOption> in = format.createInput(StringOption.class, fs, path, offset, length,
                new Counter())) {
            while (in.readTo(value)) {
                assertThat(value.get(), is(record));
                index++;
            }
            assertThat("eof", in.readTo(value), is(false));
        }
        offset += length;
    }
    assertThat(index, is(count));
}

From source file:com.asakusafw.runtime.stage.input.TemporaryInputFormatTest.java

License:Apache License

/**
 * Simple case for record readers.// w w  w  .  ja va2 s  .  c  o  m
 * @throws Exception if failed
 */
@Test
public void reader_simple() throws Exception {
    Configuration conf = new ConfigurationProvider().newInstance();
    FileStatus stat = write(conf, 1);
    try (RecordReader<NullWritable, Text> reader = TemporaryInputFormat.createRecordReader()) {
        reader.initialize(new FileSplit(stat.getPath(), 0, stat.getLen(), null),
                JobCompatibility.newTaskAttemptContext(conf, id()));

        assertThat(reader.nextKeyValue(), is(true));
        assertThat(reader.getCurrentValue(), is(new Text("Hello, world!")));

        assertThat(reader.nextKeyValue(), is(false));
        assertThat((double) reader.getProgress(), closeTo(1.0, 0.01));
    }
}