Search in sources :

Example 21 with BinaryEncoder

use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class RecordWithString method testTypeProject.

@Test
public void testTypeProject() throws IOException, UnsupportedTypeException {
    final Record1 r1 = new Record1(10, Maps.<Integer, Value>newHashMap(), new URL("http://www.yahoo.com"));
    r1.properties.put(1, new Value(1, "Name1"));
    r1.properties.put(2, new Value(2, "Name2"));
    r1.properties.put(3, null);
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    Schema sourceSchema = new ReflectionSchemaGenerator().generate(Record1.class);
    Schema targetSchema = new ReflectionSchemaGenerator().generate(Record2.class);
    new ReflectionDatumWriter<Record1>(sourceSchema).encode(r1, new BinaryEncoder(output));
    Record2 r2 = new ReflectionDatumReader<>(targetSchema, TypeToken.of(Record2.class)).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertEquals(10L, r2.i.longValue());
    Assert.assertTrue(Iterables.all(r2.properties.entrySet(), new Predicate<Map.Entry<String, Value>>() {

        @Override
        public boolean apply(Map.Entry<String, Value> input) {
            Value value = r1.properties.get(Integer.valueOf(input.getKey()));
            return (value == null && input.getValue() == null) || (value.equals(input.getValue()));
        }
    }));
    Assert.assertNull(r2.name);
    Assert.assertArrayEquals(new long[] { 1L, 2L }, r2.numbers);
    Assert.assertEquals(URI.create("http://www.yahoo.com"), r2.url);
    Assert.assertEquals(r1.uuid, r2.uuid);
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) URL(java.net.URL) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Predicate(com.google.common.base.Predicate) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 22 with BinaryEncoder

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

use of co.cask.cdap.common.io.BinaryEncoder in project cdap by caskdata.

the class ObjectStoreDataset method encode.

private byte[] encode(T object) {
    // encode T using schema
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BinaryEncoder encoder = new BinaryEncoder(bos);
    try {
        this.datumWriter.encode(object, encoder);
    } catch (IOException e) {
        // SHOULD NEVER happen
        throw new DataSetException("Failed to encode object to be written: " + e.getMessage(), e);
    }
    return bos.toByteArray();
}
Also used : DataSetException(co.cask.cdap.api.dataset.DataSetException) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 24 with BinaryEncoder

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

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

Aggregations

BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)32 Test (org.junit.Test)24 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)22 PipedInputStream (java.io.PipedInputStream)21 PipedOutputStream (java.io.PipedOutputStream)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)18 TypeToken (com.google.common.reflect.TypeToken)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Encoder (co.cask.cdap.common.io.Encoder)7 Schema (co.cask.cdap.api.data.schema.Schema)6 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 IOException (java.io.IOException)4 ReflectionDatumWriter (co.cask.cdap.internal.io.ReflectionDatumWriter)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 ByteBuffer (java.nio.ByteBuffer)2 DataSetException (co.cask.cdap.api.dataset.DataSetException)1 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)1