Search in sources :

Example 1 with ReadableMetadata

use of org.apache.flink.formats.json.debezium.DebeziumJsonDecodingFormat.ReadableMetadata in project flink by apache.

the class DebeziumJsonSerDeSchemaTest method testDeserializationWithMetadata.

private void testDeserializationWithMetadata(String resourceFile, boolean schemaInclude, Consumer<RowData> testConsumer) 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 producedDataType = DataTypeUtils.appendRowFields(PHYSICAL_DATA_TYPE, requestedMetadata.stream().map(m -> DataTypes.FIELD(m.key, m.dataType)).collect(Collectors.toList()));
    final DebeziumJsonDeserializationSchema deserializationSchema = new DebeziumJsonDeserializationSchema(PHYSICAL_DATA_TYPE, requestedMetadata, InternalTypeInfo.of(producedDataType.getLogicalType()), schemaInclude, false, TimestampFormat.ISO_8601);
    final SimpleCollector collector = new SimpleCollector();
    deserializationSchema.deserialize(firstLine.getBytes(StandardCharsets.UTF_8), collector);
    assertEquals(1, collector.list.size());
    testConsumer.accept(collector.list.get(0));
}
Also used : DataType(org.apache.flink.table.types.DataType) ReadableMetadata(org.apache.flink.formats.json.debezium.DebeziumJsonDecodingFormat.ReadableMetadata)

Example 2 with ReadableMetadata

use of org.apache.flink.formats.json.debezium.DebeziumJsonDecodingFormat.ReadableMetadata in project flink by apache.

the class DebeziumJsonDeserializationSchema method createJsonRowType.

// --------------------------------------------------------------------------------------------
private static RowType createJsonRowType(DataType physicalDataType, List<ReadableMetadata> readableMetadata, boolean schemaInclude) {
    DataType payload = DataTypes.ROW(DataTypes.FIELD("before", physicalDataType), DataTypes.FIELD("after", physicalDataType), DataTypes.FIELD("op", DataTypes.STRING()));
    // append fields that are required for reading metadata in the payload
    final List<DataTypes.Field> payloadMetadataFields = readableMetadata.stream().filter(m -> m.isJsonPayload).map(m -> m.requiredJsonField).distinct().collect(Collectors.toList());
    payload = DataTypeUtils.appendRowFields(payload, payloadMetadataFields);
    DataType root = payload;
    if (schemaInclude) {
        // when Debezium Kafka connect enables "value.converter.schemas.enable",
        // the JSON will contain "schema" information and we need to extract data from
        // "payload".
        root = DataTypes.ROW(DataTypes.FIELD("payload", payload));
    }
    // append fields that are required for reading metadata in the root
    final List<DataTypes.Field> rootMetadataFields = readableMetadata.stream().filter(m -> !m.isJsonPayload).map(m -> m.requiredJsonField).distinct().collect(Collectors.toList());
    root = DataTypeUtils.appendRowFields(root, rootMetadataFields);
    return (RowType) root.getLogicalType();
}
Also used : DataType(org.apache.flink.table.types.DataType) RowData(org.apache.flink.table.data.RowData) DataTypes(org.apache.flink.table.api.DataTypes) IOException(java.io.IOException) TimestampFormat(org.apache.flink.formats.common.TimestampFormat) RowType(org.apache.flink.table.types.logical.RowType) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Serializable(java.io.Serializable) DeserializationSchema(org.apache.flink.api.common.serialization.DeserializationSchema) Objects(java.util.Objects) List(java.util.List) GenericRowData(org.apache.flink.table.data.GenericRowData) ReadableMetadata(org.apache.flink.formats.json.debezium.DebeziumJsonDecodingFormat.ReadableMetadata) RowKind(org.apache.flink.types.RowKind) Collector(org.apache.flink.util.Collector) JsonRowDataDeserializationSchema(org.apache.flink.formats.json.JsonRowDataDeserializationSchema) Internal(org.apache.flink.annotation.Internal) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) DataTypeUtils(org.apache.flink.table.types.utils.DataTypeUtils) DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType)

Aggregations

ReadableMetadata (org.apache.flink.formats.json.debezium.DebeziumJsonDecodingFormat.ReadableMetadata)2 DataType (org.apache.flink.table.types.DataType)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 String.format (java.lang.String.format)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Internal (org.apache.flink.annotation.Internal)1 DeserializationSchema (org.apache.flink.api.common.serialization.DeserializationSchema)1 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)1 TimestampFormat (org.apache.flink.formats.common.TimestampFormat)1 JsonRowDataDeserializationSchema (org.apache.flink.formats.json.JsonRowDataDeserializationSchema)1 DataTypes (org.apache.flink.table.api.DataTypes)1 GenericRowData (org.apache.flink.table.data.GenericRowData)1 RowData (org.apache.flink.table.data.RowData)1 RowType (org.apache.flink.table.types.logical.RowType)1 DataTypeUtils (org.apache.flink.table.types.utils.DataTypeUtils)1 RowKind (org.apache.flink.types.RowKind)1 Collector (org.apache.flink.util.Collector)1