use of co.cask.cdap.internal.io.ReflectionDatumWriter in project cdap by caskdata.
the class RecordWithString method testEmptyValue.
// this tests that the datum reader treats empty fields correctly. It reproduces the issue in ENG-2404.
@Test
public void testEmptyValue() throws UnsupportedTypeException, IOException {
Schema schema = new ReflectionSchemaGenerator().generate(RecordWithString.class);
TypeRepresentation typeRep = new TypeRepresentation(RecordWithString.class);
DatumWriter<RecordWithString> datumWriter = new ReflectionDatumWriter<>(schema);
@SuppressWarnings("unchecked") ReflectionDatumReader<RecordWithString> datumReader = new ReflectionDatumReader<>(schema, (TypeToken<RecordWithString>) TypeToken.of(typeRep.toType()));
RecordWithString record = new RecordWithString();
record.setA(42);
record.setTheString("");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BinaryEncoder encoder = new BinaryEncoder(bos);
datumWriter.encode(record, encoder);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BinaryDecoder decoder = new BinaryDecoder(bis);
RecordWithString rec = datumReader.read(decoder, schema);
Assert.assertEquals(record.getA(), rec.getA());
Assert.assertEquals(record.getTheString(), rec.getTheString());
}
use of co.cask.cdap.internal.io.ReflectionDatumWriter in project cdap by caskdata.
the class ASMDatumCodecTest method testSpeed.
@Ignore
@Test
public void testSpeed() throws UnsupportedTypeException, IOException {
TypeToken<Node> type = new TypeToken<Node>() {
};
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
long startTime;
long endTime;
Node writeValue = 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));
DatumWriter<Node> writer = getWriter(type);
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
os.reset();
writer.encode(writeValue, new BinaryEncoder(os));
}
endTime = System.nanoTime();
System.out.println("Time spent: " + TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
ReflectionDatumWriter<Node> datumWriter = new ReflectionDatumWriter<>(getSchema(type));
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
os.reset();
datumWriter.encode(writeValue, new BinaryEncoder(os));
}
endTime = System.nanoTime();
System.out.println("Time spent: " + TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
writer = getWriter(type);
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
os.reset();
writer.encode(writeValue, new BinaryEncoder(os));
}
endTime = System.nanoTime();
System.out.println("Time spent: " + TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
datumWriter = new ReflectionDatumWriter<>(getSchema(type));
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
os.reset();
datumWriter.encode(writeValue, new BinaryEncoder(os));
}
endTime = System.nanoTime();
System.out.println("Time spent: " + TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
}
use of co.cask.cdap.internal.io.ReflectionDatumWriter 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));
}
Aggregations