Search in sources :

Example 1 with ReadableMetadata

use of org.apache.flink.formats.json.maxwell.MaxwellJsonDecodingFormat.ReadableMetadata in project flink by apache.

the class MaxwellJsonSerDerTest method testDeserializationWithMetadata.

@Test
public void testDeserializationWithMetadata() throws Exception {
    // we only read the first line for keeping the test simple
    final String firstLine = readLines("maxwell-data.txt").get(0);
    final List<ReadableMetadata> requestedMetadata = Arrays.asList(ReadableMetadata.values());
    final DataType producedDataType = DataTypeUtils.appendRowFields(PHYSICAL_DATA_TYPE, requestedMetadata.stream().map(m -> DataTypes.FIELD(m.key, m.dataType)).collect(Collectors.toList()));
    final MaxwellJsonDeserializationSchema deserializationSchema = new MaxwellJsonDeserializationSchema(PHYSICAL_DATA_TYPE, requestedMetadata, InternalTypeInfo.of(producedDataType.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 -> {
        assertThat(row.getInt(0), equalTo(101));
        assertThat(row.getString(1).toString(), equalTo("scooter"));
        assertThat(row.getString(2).toString(), equalTo("Small 2-wheel scooter"));
        assertThat(row.getFloat(3), equalTo(3.14f));
        assertThat(row.getString(4).toString(), equalTo("test"));
        assertThat(row.getString(5).toString(), equalTo("product"));
        assertThat(row.getArray(6).getString(0).toString(), equalTo("id"));
        assertThat(row.getTimestamp(7, 3).getMillisecond(), equalTo(1596684883000L));
    };
    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) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) 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) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) 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) ReadableMetadata(org.apache.flink.formats.json.maxwell.MaxwellJsonDecodingFormat.ReadableMetadata) 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) 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.maxwell.MaxwellJsonDecodingFormat.ReadableMetadata) Test(org.junit.Test)

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.maxwell.MaxwellJsonDecodingFormat.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