Search in sources :

Example 11 with ReflectionDatumReader

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

the class RecordWithString method testEnum.

@Test
public void testEnum() throws IOException, UnsupportedTypeException {
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    Schema schema = new ReflectionSchemaGenerator().generate(TestEnum.class);
    ReflectionDatumWriter<TestEnum> writer = new ReflectionDatumWriter<>(schema);
    BinaryEncoder encoder = new BinaryEncoder(output);
    writer.encode(TestEnum.VALUE1, encoder);
    writer.encode(TestEnum.VALUE3, encoder);
    writer.encode(TestEnum.VALUE2, encoder);
    BinaryDecoder decoder = new BinaryDecoder(input);
    Schema readSchema = Schema.parseJson(schema.toString());
    ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(readSchema, TypeToken.of(TestEnum.class));
    Assert.assertEquals(TestEnum.VALUE1, reader.read(decoder, readSchema));
    Assert.assertEquals(TestEnum.VALUE3, reader.read(decoder, readSchema));
    Assert.assertEquals(TestEnum.VALUE2, reader.read(decoder, readSchema));
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Schema(co.cask.cdap.api.data.schema.Schema) ReflectionDatumWriter(co.cask.cdap.internal.io.ReflectionDatumWriter) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 12 with ReflectionDatumReader

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

the class ASMDatumCodecTest method testList.

@Test
public void testList() throws IOException, UnsupportedTypeException {
    TypeToken<List<Long>> type = new TypeToken<List<Long>>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    List<Long> writeValue = ImmutableList.of(1L, 10L, 100L, 1000L);
    DatumWriter<List<Long>> writer = getWriter(type);
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<List<Long>> reader = new ReflectionDatumReader<>(getSchema(type), type);
    List<Long> 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) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) 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 13 with ReflectionDatumReader

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

the class ASMDatumCodecTest method testStreamEvent.

@Test
public void testStreamEvent() throws IOException, UnsupportedTypeException {
    TypeToken<StreamEvent> type = new TypeToken<StreamEvent>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<StreamEvent> writer = getWriter(type);
    StreamEvent event = new StreamEvent(ImmutableMap.of("key", "value"), ByteBuffer.wrap("Testing message".getBytes(Charsets.UTF_8)));
    writer.encode(event, new BinaryEncoder(os));
    ReflectionDatumReader<StreamEvent> reader = new ReflectionDatumReader<>(getSchema(type), type);
    StreamEvent value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(event.getHeaders(), value.getHeaders());
    Assert.assertEquals(event.getBody(), value.getBody());
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) 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 14 with ReflectionDatumReader

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

the class ASMDatumCodecTest method testInt.

@Test
public void testInt() throws UnsupportedTypeException, IOException {
    TypeToken<Integer> type = new TypeToken<Integer>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Integer> writer = getWriter(type);
    writer.encode(12234234, new BinaryEncoder(os));
    ReflectionDatumReader<Integer> reader = new ReflectionDatumReader<>(getSchema(type), type);
    int value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(12234234, 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 15 with ReflectionDatumReader

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

the class ASMDatumCodecTest method testShort.

@Test
public void testShort() throws UnsupportedTypeException, IOException {
    TypeToken<Short> type = new TypeToken<Short>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Short> writer = getWriter(type);
    writer.encode((short) 3000, new BinaryEncoder(os));
    ReflectionDatumReader<Short> reader = new ReflectionDatumReader<>(getSchema(type), type);
    short value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals((short) 3000, 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)

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