Search in sources :

Example 16 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by cdapio.

the class ASMDatumCodecTest method testRecordArray.

@Test
public void testRecordArray() 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[][] { { 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.assertArrayEquals(writeValue, value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 17 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by cdapio.

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(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 18 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by cdapio.

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(io.cdap.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(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 19 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by cdapio.

the class ASMDatumCodecTest method testMap.

@Test
public void testMap() throws IOException, UnsupportedTypeException {
    TypeToken<Map<String, List<String>>> type = new TypeToken<Map<String, List<String>>>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Map<String, List<String>>> writer = getWriter(type);
    ImmutableMap<String, List<String>> map = ImmutableMap.<String, List<String>>of("k1", Lists.newArrayList("v1"), "k2", Lists.newArrayList("v2", null));
    writer.encode(map, new BinaryEncoder(os));
    ReflectionDatumReader<Map<String, List<String>>> reader = new ReflectionDatumReader<>(getSchema(type), type);
    Assert.assertEquals(map, reader.read(new BinaryDecoder(is), getSchema(type)));
}
Also used : PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 20 with ReflectionDatumReader

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

the class RecordWithString method testEmptyValue.

// this tests that the datum reader treats empty fields correctly. It reproduces the issue in ENG-2404.
@Test
public void testEmptyValue() throws UnsupportedTypeException, IOException {
    Schema schema = new ReflectionSchemaGenerator().generate(RecordWithString.class);
    TypeRepresentation typeRep = new TypeRepresentation(RecordWithString.class);
    DatumWriter<RecordWithString> datumWriter = new ReflectionDatumWriter<>(schema);
    @SuppressWarnings("unchecked") ReflectionDatumReader<RecordWithString> datumReader = new ReflectionDatumReader<>(schema, (TypeToken<RecordWithString>) TypeToken.of(typeRep.toType()));
    RecordWithString record = new RecordWithString();
    record.setA(42);
    record.setTheString("");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BinaryEncoder encoder = new BinaryEncoder(bos);
    datumWriter.encode(record, encoder);
    byte[] bytes = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    BinaryDecoder decoder = new BinaryDecoder(bis);
    RecordWithString rec = datumReader.read(decoder, schema);
    Assert.assertEquals(record.getA(), rec.getA());
    Assert.assertEquals(record.getTheString(), rec.getTheString());
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) ReflectionDatumWriter(io.cdap.cdap.internal.io.ReflectionDatumWriter) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) TypeRepresentation(io.cdap.cdap.internal.io.TypeRepresentation) Test(org.junit.Test)

Aggregations

ReflectionDatumReader (io.cdap.cdap.internal.io.ReflectionDatumReader)36 Test (org.junit.Test)36 BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)34 BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)34 PipedInputStream (java.io.PipedInputStream)32 PipedOutputStream (java.io.PipedOutputStream)32 TypeToken (com.google.common.reflect.TypeToken)30 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 Schema (io.cdap.cdap.api.data.schema.Schema)4 ReflectionDatumWriter (io.cdap.cdap.internal.io.ReflectionDatumWriter)4 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 MetricValues (io.cdap.cdap.api.metrics.MetricValues)2 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)2 TypeRepresentation (io.cdap.cdap.internal.io.TypeRepresentation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 Map (java.util.Map)2