use of org.apache.flink.formats.avro.generated.LogicalTimeRecord in project flink by apache.
the class AvroRowDataDeSerializationSchemaTest method testSpecificType.
@Test
public void testSpecificType() throws Exception {
LogicalTimeRecord record = new LogicalTimeRecord();
Instant timestamp = Instant.parse("2010-06-30T01:20:20Z");
record.setTypeTimestampMillis(timestamp);
record.setTypeDate(LocalDate.parse("2014-03-01"));
record.setTypeTimeMillis(LocalTime.parse("12:12:12"));
SpecificDatumWriter<LogicalTimeRecord> datumWriter = new SpecificDatumWriter<>(LogicalTimeRecord.class);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().binaryEncoder(byteArrayOutputStream, null);
datumWriter.write(record, encoder);
encoder.flush();
byte[] input = byteArrayOutputStream.toByteArray();
DataType dataType = ROW(FIELD("type_timestamp_millis", TIMESTAMP(3).notNull()), FIELD("type_date", DATE().notNull()), FIELD("type_time_millis", TIME(3).notNull())).notNull();
AvroRowDataSerializationSchema serializationSchema = createSerializationSchema(dataType);
AvroRowDataDeserializationSchema deserializationSchema = createDeserializationSchema(dataType);
RowData rowData = deserializationSchema.deserialize(input);
byte[] output = serializationSchema.serialize(rowData);
RowData rowData2 = deserializationSchema.deserialize(output);
Assert.assertEquals(rowData, rowData2);
Assert.assertEquals(timestamp, rowData.getTimestamp(0, 3).toInstant());
Assert.assertEquals("2014-03-01", DataFormatConverters.LocalDateConverter.INSTANCE.toExternal(rowData.getInt(1)).toString());
Assert.assertEquals("12:12:12", DataFormatConverters.LocalTimeConverter.INSTANCE.toExternal(rowData.getInt(2)).toString());
}
Aggregations