Search in sources :

Example 11 with ChangelogMode

use of org.apache.flink.table.connector.ChangelogMode in project flink by apache.

the class DynamicSourceUtils method isSourceChangeEventsDuplicate.

/**
 * Returns true if the table source produces duplicate change events.
 */
public static boolean isSourceChangeEventsDuplicate(ResolvedSchema resolvedSchema, DynamicTableSource tableSource, TableConfig tableConfig) {
    if (!(tableSource instanceof ScanTableSource)) {
        return false;
    }
    ChangelogMode mode = ((ScanTableSource) tableSource).getChangelogMode();
    boolean isCDCSource = !mode.containsOnly(RowKind.INSERT) && !isUpsertSource(resolvedSchema, tableSource);
    boolean changeEventsDuplicate = tableConfig.getConfiguration().getBoolean(ExecutionConfigOptions.TABLE_EXEC_SOURCE_CDC_EVENTS_DUPLICATE);
    boolean hasPrimaryKey = resolvedSchema.getPrimaryKey().isPresent();
    return isCDCSource && changeEventsDuplicate && hasPrimaryKey;
}
Also used : ScanTableSource(org.apache.flink.table.connector.source.ScanTableSource) ChangelogMode(org.apache.flink.table.connector.ChangelogMode)

Example 12 with ChangelogMode

use of org.apache.flink.table.connector.ChangelogMode in project flink by apache.

the class TestValuesTableFactory method createDynamicTableSink.

@Override
public DynamicTableSink createDynamicTableSink(Context context) {
    FactoryUtil.TableFactoryHelper helper = FactoryUtil.createTableFactoryHelper(this, context);
    helper.validate();
    String sinkClass = helper.getOptions().get(TABLE_SINK_CLASS);
    boolean isInsertOnly = helper.getOptions().get(SINK_INSERT_ONLY);
    String runtimeSink = helper.getOptions().get(RUNTIME_SINK);
    int expectedNum = helper.getOptions().get(SINK_EXPECTED_MESSAGES_NUM);
    Integer parallelism = helper.getOptions().get(SINK_PARALLELISM);
    boolean dropLateEvent = helper.getOptions().get(SINK_DROP_LATE_EVENT);
    final Map<String, DataType> writableMetadata = convertToMetadataMap(helper.getOptions().get(WRITABLE_METADATA), context.getClassLoader());
    final ChangelogMode changelogMode = Optional.ofNullable(helper.getOptions().get(SINK_CHANGELOG_MODE_ENFORCED)).map(m -> parseChangelogMode(m)).orElse(null);
    final DataType consumedType = context.getCatalogTable().getSchema().toPhysicalRowDataType();
    final int[] primaryKeyIndices = TableSchemaUtils.getPrimaryKeyIndices(context.getCatalogTable().getSchema());
    if (sinkClass.equals("DEFAULT")) {
        int rowTimeIndex = validateAndExtractRowtimeIndex(context.getCatalogTable(), dropLateEvent, isInsertOnly);
        return new TestValuesTableSink(consumedType, primaryKeyIndices, context.getObjectIdentifier().getObjectName(), isInsertOnly, runtimeSink, expectedNum, writableMetadata, parallelism, changelogMode, rowTimeIndex);
    } else {
        try {
            return InstantiationUtil.instantiate(sinkClass, DynamicTableSink.class, Thread.currentThread().getContextClassLoader());
        } catch (FlinkException e) {
            throw new TableException("Can't instantiate class " + sinkClass, e);
        }
    }
}
Also used : SumAggFunction(org.apache.flink.table.planner.functions.aggfunctions.SumAggFunction) DataType(org.apache.flink.table.types.DataType) Arrays(java.util.Arrays) CatalogTable(org.apache.flink.table.catalog.CatalogTable) SupportsPartitionPushDown(org.apache.flink.table.connector.source.abilities.SupportsPartitionPushDown) InputFormatProvider(org.apache.flink.table.connector.source.InputFormatProvider) MaxAggFunction(org.apache.flink.table.planner.functions.aggfunctions.MaxAggFunction) Map(java.util.Map) FromElementsFunction(org.apache.flink.streaming.api.functions.source.FromElementsFunction) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) SinkFunction(org.apache.flink.streaming.api.functions.sink.SinkFunction) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) Seq(scala.collection.Seq) AggregateExpression(org.apache.flink.table.expressions.AggregateExpression) Set(java.util.Set) TableSchema(org.apache.flink.table.api.TableSchema) AsyncTestValueLookupFunction(org.apache.flink.table.planner.factories.TestValuesRuntimeFunctions.AsyncTestValueLookupFunction) RetractingSinkFunction(org.apache.flink.table.planner.factories.TestValuesRuntimeFunctions.RetractingSinkFunction) FactoryUtil(org.apache.flink.table.factories.FactoryUtil) DataStreamSinkProvider(org.apache.flink.table.connector.sink.DataStreamSinkProvider) Watermark(org.apache.flink.api.common.eventtime.Watermark) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) Row(org.apache.flink.types.Row) TableFunction(org.apache.flink.table.functions.TableFunction) FlinkException(org.apache.flink.util.FlinkException) ChangelogMode(org.apache.flink.table.connector.ChangelogMode) ScanTableSource(org.apache.flink.table.connector.source.ScanTableSource) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SourceFunction(org.apache.flink.streaming.api.functions.source.SourceFunction) AppendingSinkFunction(org.apache.flink.table.planner.factories.TestValuesRuntimeFunctions.AppendingSinkFunction) SupportsSourceWatermark(org.apache.flink.table.connector.source.abilities.SupportsSourceWatermark) DynamicTableSourceFactory(org.apache.flink.table.factories.DynamicTableSourceFactory) ConfigOption(org.apache.flink.configuration.ConfigOption) SourceFunctionProvider(org.apache.flink.table.connector.source.SourceFunctionProvider) Nullable(javax.annotation.Nullable) DataTypeUtils(org.apache.flink.table.types.utils.DataTypeUtils) DataStreamSink(org.apache.flink.streaming.api.datastream.DataStreamSink) AsyncTableFunctionProvider(org.apache.flink.table.connector.source.AsyncTableFunctionProvider) SupportsLimitPushDown(org.apache.flink.table.connector.source.abilities.SupportsLimitPushDown) DynamicTableSinkFactory(org.apache.flink.table.factories.DynamicTableSinkFactory) TableException(org.apache.flink.table.api.TableException) WatermarkSpec(org.apache.flink.table.api.WatermarkSpec) IOException(java.io.IOException) SupportsFilterPushDown(org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown) RowKind(org.apache.flink.types.RowKind) LogicalTypeParser(org.apache.flink.table.types.logical.utils.LogicalTypeParser) TableSchemaUtils(org.apache.flink.table.utils.TableSchemaUtils) CollectionInputFormat(org.apache.flink.api.java.io.CollectionInputFormat) SupportsWatermarkPushDown(org.apache.flink.table.connector.source.abilities.SupportsWatermarkPushDown) ConfigOptions(org.apache.flink.configuration.ConfigOptions) Sum0AggFunction(org.apache.flink.table.planner.functions.aggfunctions.Sum0AggFunction) SupportsWritingMetadata(org.apache.flink.table.connector.sink.abilities.SupportsWritingMetadata) SupportsProjectionPushDown(org.apache.flink.table.connector.source.abilities.SupportsProjectionPushDown) CountAggFunction(org.apache.flink.table.planner.functions.aggfunctions.CountAggFunction) DataStreamScanProvider(org.apache.flink.table.connector.source.DataStreamScanProvider) InstantiationUtil(org.apache.flink.util.InstantiationUtil) SupportsPartitioning(org.apache.flink.table.connector.sink.abilities.SupportsPartitioning) LookupTableSource(org.apache.flink.table.connector.source.LookupTableSource) KeyedUpsertingSinkFunction(org.apache.flink.table.planner.factories.TestValuesRuntimeFunctions.KeyedUpsertingSinkFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) OutputFormat(org.apache.flink.api.common.io.OutputFormat) Collection(java.util.Collection) AsyncTableFunction(org.apache.flink.table.functions.AsyncTableFunction) WatermarkStrategy(org.apache.flink.api.common.eventtime.WatermarkStrategy) Count1AggFunction(org.apache.flink.table.planner.functions.aggfunctions.Count1AggFunction) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) Preconditions.checkArgument(org.apache.flink.util.Preconditions.checkArgument) ValidationException(org.apache.flink.table.api.ValidationException) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Optional(java.util.Optional) TableFunctionProvider(org.apache.flink.table.connector.source.TableFunctionProvider) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) MinAggFunction(org.apache.flink.table.planner.functions.aggfunctions.MinAggFunction) SupportsAggregatePushDown(org.apache.flink.table.connector.source.abilities.SupportsAggregatePushDown) FilterUtils(org.apache.flink.table.planner.utils.FilterUtils) HashMap(java.util.HashMap) DataStreamSource(org.apache.flink.streaming.api.datastream.DataStreamSource) Function(java.util.function.Function) TestValuesLookupFunction(org.apache.flink.table.planner.factories.TestValuesRuntimeFunctions.TestValuesLookupFunction) HashSet(java.util.HashSet) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) SinkFunctionProvider(org.apache.flink.table.connector.sink.SinkFunctionProvider) AppendingOutputFormat(org.apache.flink.table.planner.factories.TestValuesRuntimeFunctions.AppendingOutputFormat) RowData(org.apache.flink.table.data.RowData) Iterator(java.util.Iterator) ProviderContext(org.apache.flink.table.connector.ProviderContext) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) BigIntType(org.apache.flink.table.types.logical.BigIntType) RuntimeConverter(org.apache.flink.table.connector.RuntimeConverter) FailingCollectionSource(org.apache.flink.table.planner.runtime.utils.FailingCollectionSource) DataStream(org.apache.flink.streaming.api.datastream.DataStream) OutputFormatProvider(org.apache.flink.table.connector.sink.OutputFormatProvider) JavaScalaConversionUtil(org.apache.flink.table.planner.utils.JavaScalaConversionUtil) SupportsReadingMetadata(org.apache.flink.table.connector.source.abilities.SupportsReadingMetadata) Comparator(java.util.Comparator) Collections(java.util.Collections) TableException(org.apache.flink.table.api.TableException) FactoryUtil(org.apache.flink.table.factories.FactoryUtil) FlinkException(org.apache.flink.util.FlinkException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChangelogMode(org.apache.flink.table.connector.ChangelogMode) DataType(org.apache.flink.table.types.DataType)

Example 13 with ChangelogMode

use of org.apache.flink.table.connector.ChangelogMode in project flink by apache.

the class TestValuesTableFactory method createDynamicTableSource.

@Override
public DynamicTableSource createDynamicTableSource(Context context) {
    FactoryUtil.TableFactoryHelper helper = FactoryUtil.createTableFactoryHelper(this, context);
    helper.validate();
    ChangelogMode changelogMode = parseChangelogMode(helper.getOptions().get(CHANGELOG_MODE));
    String runtimeSource = helper.getOptions().get(RUNTIME_SOURCE);
    boolean isBounded = helper.getOptions().get(BOUNDED);
    String dataId = helper.getOptions().get(DATA_ID);
    String sourceClass = helper.getOptions().get(TABLE_SOURCE_CLASS);
    boolean isAsync = helper.getOptions().get(ASYNC_ENABLED);
    String lookupFunctionClass = helper.getOptions().get(LOOKUP_FUNCTION_CLASS);
    boolean disableLookup = helper.getOptions().get(DISABLE_LOOKUP);
    boolean enableProjectionPushDown = helper.getOptions().get(ENABLE_PROJECTION_PUSH_DOWN);
    boolean nestedProjectionSupported = helper.getOptions().get(NESTED_PROJECTION_SUPPORTED);
    boolean enableWatermarkPushDown = helper.getOptions().get(ENABLE_WATERMARK_PUSH_DOWN);
    boolean failingSource = helper.getOptions().get(FAILING_SOURCE);
    int numElementToSkip = helper.getOptions().get(SOURCE_NUM_ELEMENT_TO_SKIP);
    boolean internalData = helper.getOptions().get(INTERNAL_DATA);
    Optional<List<String>> filterableFields = helper.getOptions().getOptional(FILTERABLE_FIELDS);
    Set<String> filterableFieldsSet = new HashSet<>();
    filterableFields.ifPresent(filterableFieldsSet::addAll);
    final Map<String, DataType> readableMetadata = convertToMetadataMap(helper.getOptions().get(READABLE_METADATA), context.getClassLoader());
    if (sourceClass.equals("DEFAULT")) {
        if (internalData) {
            return new TestValuesScanTableSourceWithInternalData(dataId, isBounded);
        }
        Collection<Row> data = registeredData.getOrDefault(dataId, Collections.emptyList());
        List<Map<String, String>> partitions = parsePartitionList(helper.getOptions().get(PARTITION_LIST));
        DataType producedDataType = context.getCatalogTable().getSchema().toPhysicalRowDataType();
        // pushing project into scan will prune schema and we have to get the mapping between
        // partition and row
        Map<Map<String, String>, Collection<Row>> partition2Rows;
        if (!partitions.isEmpty()) {
            partition2Rows = mapPartitionToRow(producedDataType, data, partitions);
        } else {
            // put all data into one partition
            partitions = Collections.emptyList();
            partition2Rows = new HashMap<>();
            partition2Rows.put(Collections.emptyMap(), data);
        }
        if (!enableProjectionPushDown) {
            return new TestValuesScanTableSourceWithoutProjectionPushDown(producedDataType, changelogMode, isBounded, runtimeSource, failingSource, partition2Rows, nestedProjectionSupported, null, Collections.emptyList(), filterableFieldsSet, numElementToSkip, Long.MAX_VALUE, partitions, readableMetadata, null);
        }
        if (disableLookup) {
            if (enableWatermarkPushDown) {
                return new TestValuesScanTableSourceWithWatermarkPushDown(producedDataType, changelogMode, runtimeSource, failingSource, partition2Rows, context.getObjectIdentifier().getObjectName(), nestedProjectionSupported, null, Collections.emptyList(), filterableFieldsSet, numElementToSkip, Long.MAX_VALUE, partitions, readableMetadata, null);
            } else {
                return new TestValuesScanTableSource(producedDataType, changelogMode, isBounded, runtimeSource, failingSource, partition2Rows, nestedProjectionSupported, null, Collections.emptyList(), filterableFieldsSet, numElementToSkip, Long.MAX_VALUE, partitions, readableMetadata, null);
            }
        } else {
            return new TestValuesScanLookupTableSource(producedDataType, changelogMode, isBounded, runtimeSource, failingSource, partition2Rows, isAsync, lookupFunctionClass, nestedProjectionSupported, null, Collections.emptyList(), filterableFieldsSet, numElementToSkip, Long.MAX_VALUE, partitions, readableMetadata, null);
        }
    } else {
        try {
            return InstantiationUtil.instantiate(sourceClass, DynamicTableSource.class, Thread.currentThread().getContextClassLoader());
        } catch (FlinkException e) {
            throw new TableException("Can't instantiate class " + sourceClass, e);
        }
    }
}
Also used : FactoryUtil(org.apache.flink.table.factories.FactoryUtil) ChangelogMode(org.apache.flink.table.connector.ChangelogMode) DataType(org.apache.flink.table.types.DataType) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) TableException(org.apache.flink.table.api.TableException) FlinkException(org.apache.flink.util.FlinkException) Collection(java.util.Collection) Row(org.apache.flink.types.Row) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Aggregations

ChangelogMode (org.apache.flink.table.connector.ChangelogMode)13 DataType (org.apache.flink.table.types.DataType)4 Collections (java.util.Collections)3 List (java.util.List)3 TableException (org.apache.flink.table.api.TableException)3 DynamicTableSink (org.apache.flink.table.connector.sink.DynamicTableSink)3 ScanTableSource (org.apache.flink.table.connector.source.ScanTableSource)3 FactoryUtil (org.apache.flink.table.factories.FactoryUtil)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 Internal (org.apache.flink.annotation.Internal)2 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)2