Search in sources :

Example 1 with LogicalTimeRecord

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());
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) Encoder(org.apache.avro.io.Encoder) Instant(java.time.Instant) LogicalTimeRecord(org.apache.flink.formats.avro.generated.LogicalTimeRecord) DataType(org.apache.flink.table.types.DataType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Instant (java.time.Instant)1 Encoder (org.apache.avro.io.Encoder)1 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)1 LogicalTimeRecord (org.apache.flink.formats.avro.generated.LogicalTimeRecord)1 GenericRowData (org.apache.flink.table.data.GenericRowData)1 RowData (org.apache.flink.table.data.RowData)1 DataType (org.apache.flink.table.types.DataType)1 Test (org.junit.Test)1