Search in sources :

Example 1 with ReadableMetadata

use of org.apache.flink.formats.json.ogg.OggJsonDecodingFormat.ReadableMetadata in project flink by apache.

the class OggJsonSerDeSchemaTest method testDeserializationWithMetadata.

public void testDeserializationWithMetadata(String resourceFile) throws Exception {
    // we only read the first line for keeping the test simple
    final String firstLine = readLines(resourceFile).get(0);
    final List<ReadableMetadata> requestedMetadata = Arrays.asList(ReadableMetadata.values());
    final DataType producedDataTypes = DataTypeUtils.appendRowFields(PHYSICAL_DATA_TYPE, requestedMetadata.stream().map(m -> DataTypes.FIELD(m.key, m.dataType)).collect(Collectors.toList()));
    final OggJsonDeserializationSchema deserializationSchema = new OggJsonDeserializationSchema(PHYSICAL_DATA_TYPE, requestedMetadata, InternalTypeInfo.of(producedDataTypes.getLogicalType()), false, TimestampFormat.ISO_8601);
    final SimpleCollector collector = new SimpleCollector();
    deserializationSchema.deserialize(firstLine.getBytes(StandardCharsets.UTF_8), collector);
    assertEquals(1, collector.list.size());
    Consumer<RowData> consumer = row -> {
        assertEquals(101, row.getInt(0));
        assertEquals("scooter", row.getString(1).toString());
        assertEquals("Small 2-wheel scooter", row.getString(2).toString());
        assertEquals(3.140000104904175, row.getFloat(3), 1e-15);
        assertEquals("OGG.TBL_TEST", row.getString(4).toString());
        assertEquals("id", row.getArray(5).getString(0).toString());
        assertEquals(1589377175766L, row.getTimestamp(6, 6).getMillisecond());
        assertEquals(1589384406000L, row.getTimestamp(7, 6).getMillisecond());
    };
    consumer.accept(collector.list.get(0));
}
Also used : DataType(org.apache.flink.table.types.DataType) STRING(org.apache.flink.table.api.DataTypes.STRING) Arrays(java.util.Arrays) FLOAT(org.apache.flink.table.api.DataTypes.FLOAT) URL(java.net.URL) ROW(org.apache.flink.table.api.DataTypes.ROW) RowType(org.apache.flink.table.types.logical.RowType) ArrayList(java.util.ArrayList) JsonFormatOptions(org.apache.flink.formats.json.JsonFormatOptions) Collector(org.apache.flink.util.Collector) ReadableMetadata(org.apache.flink.formats.json.ogg.OggJsonDecodingFormat.ReadableMetadata) ExpectedException(org.junit.rules.ExpectedException) Path(java.nio.file.Path) FIELD(org.apache.flink.table.api.DataTypes.FIELD) INT(org.apache.flink.table.api.DataTypes.INT) DataTypeUtils(org.apache.flink.table.types.utils.DataTypeUtils) RowData(org.apache.flink.table.data.RowData) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) DataTypes(org.apache.flink.table.api.DataTypes) Test(org.junit.Test) IOException(java.io.IOException) TimestampFormat(org.apache.flink.formats.common.TimestampFormat) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) List(java.util.List) InternalTypeInfo(org.apache.flink.table.runtime.typeutils.InternalTypeInfo) Rule(org.junit.Rule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) RowData(org.apache.flink.table.data.RowData) DataType(org.apache.flink.table.types.DataType) ReadableMetadata(org.apache.flink.formats.json.ogg.OggJsonDecodingFormat.ReadableMetadata)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 TimestampFormat (org.apache.flink.formats.common.TimestampFormat)1 JsonFormatOptions (org.apache.flink.formats.json.JsonFormatOptions)1 ReadableMetadata (org.apache.flink.formats.json.ogg.OggJsonDecodingFormat.ReadableMetadata)1 DataTypes (org.apache.flink.table.api.DataTypes)1 FIELD (org.apache.flink.table.api.DataTypes.FIELD)1 FLOAT (org.apache.flink.table.api.DataTypes.FLOAT)1 INT (org.apache.flink.table.api.DataTypes.INT)1 ROW (org.apache.flink.table.api.DataTypes.ROW)1