use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.
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);
}
use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.
the class RecordWithString method testCollection.
@Test
public void testCollection() throws UnsupportedTypeException, IOException {
List<String> list = Lists.newArrayList("1", "2", "3");
Schema sourceSchema = new ReflectionSchemaGenerator().generate(new TypeToken<List<String>>() {
}.getType());
Schema targetSchema = new ReflectionSchemaGenerator().generate(new TypeToken<Set<String>>() {
}.getType());
PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);
new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
Set<String> set = new ReflectionDatumReader<>(targetSchema, new TypeToken<Set<String>>() {
}).read(new BinaryDecoder(input), sourceSchema);
Assert.assertEquals(Sets.newHashSet("1", "2", "3"), set);
targetSchema = new ReflectionSchemaGenerator().generate(String[].class);
new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
String[] array = new ReflectionDatumReader<>(targetSchema, new TypeToken<String[]>() {
}).read(new BinaryDecoder(input), sourceSchema);
Assert.assertArrayEquals(new String[] { "1", "2", "3" }, array);
}
use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.
the class RecordWithString method testCircularRef.
@Test(expected = IOException.class)
public void testCircularRef() throws UnsupportedTypeException, IOException {
Schema schema = new ReflectionSchemaGenerator().generate(Node.class);
Node head = new Node();
head.next = new Node();
head.next.next = head;
ByteArrayOutputStream output = new ByteArrayOutputStream();
new ReflectionDatumWriter<Node>(schema).encode(head, new BinaryEncoder(output));
}
use of io.cdap.cdap.common.io.BinaryEncoder in project cdap by cdapio.
the class RecordWithString method testReduceProjection.
@Test
public void testReduceProjection() throws IOException, UnsupportedTypeException {
PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);
Schema sourceSchema = new ReflectionSchemaGenerator().generate(MoreFields.class);
Schema targetSchema = new ReflectionSchemaGenerator().generate(LessFields.class);
MoreFields moreFields = new MoreFields(10, 20.2, "30", ImmutableList.of("1", "2"));
new ReflectionDatumWriter<MoreFields>(sourceSchema).encode(moreFields, new BinaryEncoder(output));
LessFields lessFields = new ReflectionDatumReader<>(targetSchema, TypeToken.of(LessFields.class)).read(new BinaryDecoder(input), sourceSchema);
Assert.assertEquals("30", lessFields.k);
Assert.assertEquals(moreFields.inner.b, lessFields.inner.b);
}
use of io.cdap.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);
}
Aggregations