Search in sources :

Example 6 with ReflectionDatumReader

use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class ASMDatumCodecTest method testRecord.

@Test
public void testRecord() throws IOException, UnsupportedTypeException {
    TypeToken<Record> type = new TypeToken<Record>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Record> writer = getWriter(type);
    Record writeValue = new Record(10, "testing", ImmutableList.of("a", "b", "c"), TestEnum.VALUE2);
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<Record> reader = new ReflectionDatumReader<>(getSchema(type), type);
    Record value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(writeValue, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 7 with ReflectionDatumReader

use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class ASMDatumCodecTest method testEnum.

@Test
public void testEnum() throws UnsupportedTypeException, IOException {
    TypeToken<TestEnum> type = new TypeToken<TestEnum>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<TestEnum> writer = getWriter(type);
    BinaryEncoder encoder = new BinaryEncoder(os);
    writer.encode(TestEnum.VALUE1, encoder);
    writer.encode(TestEnum.VALUE4, encoder);
    writer.encode(TestEnum.VALUE3, encoder);
    ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(getSchema(type), type);
    TestEnum value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE1, value);
    value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE4, value);
    value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE3, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 8 with ReflectionDatumReader

use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class ASMDatumCodecTest method testPrimitiveArray.

@Test
public void testPrimitiveArray() throws IOException, UnsupportedTypeException {
    TypeToken<int[]> type = new TypeToken<int[]>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    int[] writeValue = { 1, 2, 3, 4, -5, -6, -7, -8 };
    DatumWriter<int[]> writer = getWriter(type);
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<int[]> reader = new ReflectionDatumReader<>(getSchema(type), type);
    int[] value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertArrayEquals(writeValue, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 9 with ReflectionDatumReader

use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class ASMDatumCodecTest method testTree.

@Test
public void testTree() throws IOException, UnsupportedTypeException {
    TypeToken<Node> type = new TypeToken<Node>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Node> writer = getWriter(type);
    Node root = new Node((short) 1, new Node((short) 2, null, new Node((short) 3, null, null)), new Node((short) 4, new Node((short) 5, null, null), null));
    writer.encode(root, new BinaryEncoder(os));
    ReflectionDatumReader<Node> reader = new ReflectionDatumReader<>(getSchema(type), type);
    Node value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(root, value);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 10 with ReflectionDatumReader

use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class FlowletProgramRunner method createInputDatumDecoder.

private <T> Function<ByteBuffer, T> createInputDatumDecoder(final TypeToken<T> dataType, final Schema schema, final SchemaCache schemaCache) {
    final ReflectionDatumReader<T> datumReader = new ReflectionDatumReader<>(schema, dataType);
    final ByteBufferInputStream byteBufferInput = new ByteBufferInputStream(null);
    final BinaryDecoder decoder = new BinaryDecoder(byteBufferInput);
    return new Function<ByteBuffer, T>() {

        @Nullable
        @Override
        public T apply(ByteBuffer input) {
            byteBufferInput.reset(input);
            try {
                final Schema sourceSchema = schemaCache.get(input);
                Preconditions.checkNotNull(sourceSchema, "Fail to find source schema.");
                return datumReader.read(decoder, sourceSchema);
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }

        @Override
        public String toString() {
            return Objects.toStringHelper(this).add("dataType", dataType).add("schema", schema).toString();
        }
    };
}
Also used : Function(com.google.common.base.Function) Schema(co.cask.cdap.api.data.schema.Schema) ByteBufferInputStream(co.cask.common.io.ByteBufferInputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder)

Aggregations

ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)20 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)19 Test (org.junit.Test)19 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)18 PipedInputStream (java.io.PipedInputStream)17 PipedOutputStream (java.io.PipedOutputStream)17 TypeToken (com.google.common.reflect.TypeToken)16 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 Schema (co.cask.cdap.api.data.schema.Schema)3 ReflectionDatumWriter (co.cask.cdap.internal.io.ReflectionDatumWriter)2 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)2 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)1 MetricValues (co.cask.cdap.api.metrics.MetricValues)1 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)1 TypeRepresentation (co.cask.cdap.internal.io.TypeRepresentation)1 ByteBufferInputStream (co.cask.common.io.ByteBufferInputStream)1 Function (com.google.common.base.Function)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1