use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
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);
}
use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class ASMDatumCodecTest method testEnum.
@Test
public void testEnum() throws UnsupportedTypeException, IOException {
TypeToken<TestEnum> type = new TypeToken<TestEnum>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
DatumWriter<TestEnum> writer = getWriter(type);
BinaryEncoder encoder = new BinaryEncoder(os);
writer.encode(TestEnum.VALUE1, encoder);
writer.encode(TestEnum.VALUE4, encoder);
writer.encode(TestEnum.VALUE3, encoder);
ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(getSchema(type), type);
TestEnum value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertEquals(TestEnum.VALUE1, value);
value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertEquals(TestEnum.VALUE4, value);
value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertEquals(TestEnum.VALUE3, value);
}
use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class ASMDatumCodecTest method testPrimitiveArray.
@Test
public void testPrimitiveArray() throws IOException, UnsupportedTypeException {
TypeToken<int[]> type = new TypeToken<int[]>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
int[] writeValue = { 1, 2, 3, 4, -5, -6, -7, -8 };
DatumWriter<int[]> writer = getWriter(type);
writer.encode(writeValue, new BinaryEncoder(os));
ReflectionDatumReader<int[]> reader = new ReflectionDatumReader<>(getSchema(type), type);
int[] value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertArrayEquals(writeValue, value);
}
use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class ASMDatumCodecTest method testTree.
@Test
public void testTree() throws IOException, UnsupportedTypeException {
TypeToken<Node> type = new TypeToken<Node>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
DatumWriter<Node> writer = getWriter(type);
Node root = new Node((short) 1, new Node((short) 2, null, new Node((short) 3, null, null)), new Node((short) 4, new Node((short) 5, null, null), null));
writer.encode(root, new BinaryEncoder(os));
ReflectionDatumReader<Node> reader = new ReflectionDatumReader<>(getSchema(type), type);
Node value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertEquals(root, value);
}
use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class FlowletProgramRunner method createInputDatumDecoder.
private <T> Function<ByteBuffer, T> createInputDatumDecoder(final TypeToken<T> dataType, final Schema schema, final SchemaCache schemaCache) {
final ReflectionDatumReader<T> datumReader = new ReflectionDatumReader<>(schema, dataType);
final ByteBufferInputStream byteBufferInput = new ByteBufferInputStream(null);
final BinaryDecoder decoder = new BinaryDecoder(byteBufferInput);
return new Function<ByteBuffer, T>() {
@Nullable
@Override
public T apply(ByteBuffer input) {
byteBufferInput.reset(input);
try {
final Schema sourceSchema = schemaCache.get(input);
Preconditions.checkNotNull(sourceSchema, "Fail to find source schema.");
return datumReader.read(decoder, sourceSchema);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("dataType", dataType).add("schema", schema).toString();
}
};
}
Aggregations