use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.
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 co.cask.cdap.common.io.BinaryDecoder 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);
}
use of co.cask.cdap.common.io.BinaryDecoder 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));
}
use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.
the class ObjectStoreDataset method decode.
private T decode(byte[] bytes) {
if (bytes == null) {
return null;
}
// decode T using schema
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BinaryDecoder decoder = new BinaryDecoder(bis);
try {
return getReflectionDatumReader().read(decoder, this.schema);
} catch (IOException e) {
// SHOULD NEVER happen
throw new DataSetException("Failed to decode read object: " + e.getMessage(), e);
}
}
use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.
the class QueueEntry method deserializeHashKeys.
public static Map<String, Integer> deserializeHashKeys(byte[] bytes, int off, int len) throws IOException {
if (bytes == null || (len == 1 && bytes[off] == 0)) {
// No hash keys.
return ImmutableMap.of();
}
ByteArrayInputStream bis = new ByteArrayInputStream(bytes, off, len);
BinaryDecoder decoder = new BinaryDecoder(bis);
int size = decoder.readInt();
Map<String, Integer> hashKeys = Maps.newHashMapWithExpectedSize(size);
while (size > 0) {
// per avro spec, ther ecan be multiple blocks
while (size-- > 0) {
String key = decoder.readString();
int value = decoder.readInt();
hashKeys.put(key, value);
}
// next block length, will be always zero in this case
size = decoder.readInt();
}
return hashKeys;
}
Aggregations