Search in sources :

Example 21 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.

the class ASMDatumCodecTest method testUUID.

@Test
public void testUUID() throws UnsupportedTypeException, IOException {
    TypeToken<UUID> type = new TypeToken<UUID>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<UUID> writer = getWriter(type);
    UUID uuid = UUID.randomUUID();
    writer.encode(uuid, new BinaryEncoder(os));
    ReflectionDatumReader<UUID> reader = new ReflectionDatumReader<>(getSchema(type), type);
    UUID value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(uuid, 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) UUID(java.util.UUID) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 22 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder 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 23 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder 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 24 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder 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 25 with BinaryEncoder

use of io.cdap.cdap.common.io.BinaryEncoder 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)

Aggregations

BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)54 Test (org.junit.Test)46 BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)42 PipedInputStream (java.io.PipedInputStream)40 PipedOutputStream (java.io.PipedOutputStream)40 TypeToken (com.google.common.reflect.TypeToken)34 ReflectionDatumReader (io.cdap.cdap.internal.io.ReflectionDatumReader)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Schema (io.cdap.cdap.api.data.schema.Schema)12 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)12 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 Encoder (io.cdap.cdap.common.io.Encoder)8 ReflectionDatumWriter (io.cdap.cdap.internal.io.ReflectionDatumWriter)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 Predicate (com.google.common.base.Predicate)2 DataSetException (io.cdap.cdap.api.dataset.DataSetException)2 Decoder (io.cdap.cdap.common.io.Decoder)2 TypeRepresentation (io.cdap.cdap.internal.io.TypeRepresentation)2