Search in sources :

Example 51 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class KafkaConnectorOptionsUtilTest method testInvalidKeyFormatPrefixProjection.

@Test
public void testInvalidKeyFormatPrefixProjection() {
    final DataType dataType = ROW(FIELD("k_part_1", INT()), FIELD("part_2", STRING()), FIELD("name", STRING()));
    final Map<String, String> options = createTestOptions();
    options.put("key.fields", "k_part_1;part_2");
    options.put("key.fields-prefix", "k_");
    final Configuration config = Configuration.fromMap(options);
    try {
        createKeyFormatProjection(config, dataType);
        fail();
    } catch (ValidationException e) {
        assertThat(e, hasMessage(equalTo("All fields in 'key.fields' must be prefixed with 'k_' when option " + "'key.fields-prefix' is set but field 'part_2' is not prefixed.")));
    }
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) Configuration(org.apache.flink.configuration.Configuration) DataType(org.apache.flink.table.types.DataType) Test(org.junit.Test)

Example 52 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class CanalJsonDeserializationSchema method createJsonRowType.

// --------------------------------------------------------------------------------------------
private static RowType createJsonRowType(DataType physicalDataType, List<ReadableMetadata> readableMetadata) {
    // Canal JSON contains other information, e.g. "ts", "sql", but we don't need them
    DataType root = DataTypes.ROW(DataTypes.FIELD("data", DataTypes.ARRAY(physicalDataType)), DataTypes.FIELD("old", DataTypes.ARRAY(physicalDataType)), DataTypes.FIELD("type", DataTypes.STRING()), ReadableMetadata.DATABASE.requiredJsonField, ReadableMetadata.TABLE.requiredJsonField);
    // append fields that are required for reading metadata in the root
    final List<DataTypes.Field> rootMetadataFields = readableMetadata.stream().filter(m -> m != ReadableMetadata.DATABASE && m != ReadableMetadata.TABLE).map(m -> m.requiredJsonField).distinct().collect(Collectors.toList());
    return (RowType) DataTypeUtils.appendRowFields(root, rootMetadataFields).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) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) 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) ArrayData(org.apache.flink.table.data.ArrayData) List(java.util.List) GenericRowData(org.apache.flink.table.data.GenericRowData) 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) ReadableMetadata(org.apache.flink.formats.json.canal.CanalJsonDecodingFormat.ReadableMetadata) Pattern(java.util.regex.Pattern) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Nullable(javax.annotation.Nullable) DataTypeUtils(org.apache.flink.table.types.utils.DataTypeUtils) DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType)

Example 53 with DataType

use of org.apache.flink.table.types.DataType 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)

Example 54 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class MaxwellJsonDecodingFormat method createRuntimeDecoder.

@Override
public DeserializationSchema<RowData> createRuntimeDecoder(DynamicTableSource.Context context, DataType physicalDataType, int[][] projections) {
    physicalDataType = Projection.of(projections).project(physicalDataType);
    final List<ReadableMetadata> readableMetadata = metadataKeys.stream().map(k -> Stream.of(ReadableMetadata.values()).filter(rm -> rm.key.equals(k)).findFirst().orElseThrow(() -> new IllegalStateException(String.format("Could not find the requested metadata key: %s", k)))).collect(Collectors.toList());
    final List<DataTypes.Field> metadataFields = readableMetadata.stream().map(m -> DataTypes.FIELD(m.key, m.dataType)).collect(Collectors.toList());
    final DataType producedDataType = DataTypeUtils.appendRowFields(physicalDataType, metadataFields);
    final TypeInformation<RowData> producedTypeInfo = context.createTypeInformation(producedDataType);
    return new MaxwellJsonDeserializationSchema(physicalDataType, readableMetadata, producedTypeInfo, ignoreParseErrors, timestampFormat);
}
Also used : DataType(org.apache.flink.table.types.DataType) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) RowData(org.apache.flink.table.data.RowData) TimestampData(org.apache.flink.table.data.TimestampData) ChangelogMode(org.apache.flink.table.connector.ChangelogMode) DataTypes(org.apache.flink.table.api.DataTypes) TimestampFormat(org.apache.flink.formats.common.TimestampFormat) ProjectableDecodingFormat(org.apache.flink.table.connector.format.ProjectableDecodingFormat) Collectors(java.util.stream.Collectors) DeserializationSchema(org.apache.flink.api.common.serialization.DeserializationSchema) LinkedHashMap(java.util.LinkedHashMap) DecodingFormat(org.apache.flink.table.connector.format.DecodingFormat) List(java.util.List) GenericRowData(org.apache.flink.table.data.GenericRowData) Stream(java.util.stream.Stream) RowKind(org.apache.flink.types.RowKind) Map(java.util.Map) MetadataConverter(org.apache.flink.formats.json.maxwell.MaxwellJsonDeserializationSchema.MetadataConverter) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Collections(java.util.Collections) Projection(org.apache.flink.table.connector.Projection) DataTypeUtils(org.apache.flink.table.types.utils.DataTypeUtils) RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) DataType(org.apache.flink.table.types.DataType)

Example 55 with DataType

use of org.apache.flink.table.types.DataType in project flink by apache.

the class MaxwellJsonFormatFactory method createEncodingFormat.

@Override
public EncodingFormat<SerializationSchema<RowData>> createEncodingFormat(DynamicTableFactory.Context context, ReadableConfig formatOptions) {
    FactoryUtil.validateFactoryOptions(this, formatOptions);
    validateEncodingFormatOptions(formatOptions);
    TimestampFormat timestampFormat = JsonFormatOptionsUtil.getTimestampFormat(formatOptions);
    JsonFormatOptions.MapNullKeyMode mapNullKeyMode = JsonFormatOptionsUtil.getMapNullKeyMode(formatOptions);
    String mapNullKeyLiteral = formatOptions.get(JSON_MAP_NULL_KEY_LITERAL);
    final boolean encodeDecimalAsPlainNumber = formatOptions.get(ENCODE_DECIMAL_AS_PLAIN_NUMBER);
    return new EncodingFormat<SerializationSchema<RowData>>() {

        @Override
        public ChangelogMode getChangelogMode() {
            return ChangelogMode.newBuilder().addContainedKind(RowKind.INSERT).addContainedKind(RowKind.UPDATE_BEFORE).addContainedKind(RowKind.UPDATE_AFTER).addContainedKind(RowKind.DELETE).build();
        }

        @Override
        public SerializationSchema<RowData> createRuntimeEncoder(DynamicTableSink.Context context, DataType consumedDataType) {
            final RowType rowType = (RowType) consumedDataType.getLogicalType();
            return new MaxwellJsonSerializationSchema(rowType, timestampFormat, mapNullKeyMode, mapNullKeyLiteral, encodeDecimalAsPlainNumber);
        }
    };
}
Also used : EncodingFormat(org.apache.flink.table.connector.format.EncodingFormat) JsonFormatOptions(org.apache.flink.formats.json.JsonFormatOptions) RowData(org.apache.flink.table.data.RowData) DataType(org.apache.flink.table.types.DataType) RowType(org.apache.flink.table.types.logical.RowType) TimestampFormat(org.apache.flink.formats.common.TimestampFormat)

Aggregations

DataType (org.apache.flink.table.types.DataType)260 Test (org.junit.Test)72 RowType (org.apache.flink.table.types.logical.RowType)59 LogicalType (org.apache.flink.table.types.logical.LogicalType)58 RowData (org.apache.flink.table.data.RowData)54 List (java.util.List)38 FieldsDataType (org.apache.flink.table.types.FieldsDataType)32 ValidationException (org.apache.flink.table.api.ValidationException)31 ArrayList (java.util.ArrayList)29 Collectors (java.util.stream.Collectors)24 AtomicDataType (org.apache.flink.table.types.AtomicDataType)24 Map (java.util.Map)23 Internal (org.apache.flink.annotation.Internal)23 TableException (org.apache.flink.table.api.TableException)23 HashMap (java.util.HashMap)22 GenericRowData (org.apache.flink.table.data.GenericRowData)22 Row (org.apache.flink.types.Row)22 TableSchema (org.apache.flink.table.api.TableSchema)20 TypeConversions.fromLogicalToDataType (org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType)19 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)18