Search in sources :

Example 51 with DynamicTableSink

use of org.apache.flink.table.connector.sink.DynamicTableSink in project flink by apache.

the class FactoryUtilTest method testDiscoveryForSeparateSourceSinkFactory.

@Test
public void testDiscoveryForSeparateSourceSinkFactory() {
    final Map<String, String> options = createAllOptions();
    // the "test" source and sink factory is not in one factory class
    // see TestDynamicTableSinkFactory and TestDynamicTableSourceFactory
    options.put("connector", "test");
    final DynamicTableSource actualSource = createTableSource(SCHEMA, options);
    final DynamicTableSource expectedSource = new DynamicTableSourceMock("MyTarget", null, new DecodingFormatMock(",", false), new DecodingFormatMock("|", true));
    assertThat(actualSource).isEqualTo(expectedSource);
    final DynamicTableSink actualSink = createTableSink(SCHEMA, options);
    final DynamicTableSink expectedSink = new DynamicTableSinkMock("MyTarget", 1000L, new EncodingFormatMock(","), new EncodingFormatMock("|"));
    assertThat(actualSink).isEqualTo(expectedSink);
}
Also used : EncodingFormatMock(org.apache.flink.table.factories.TestFormatFactory.EncodingFormatMock) DecodingFormatMock(org.apache.flink.table.factories.TestFormatFactory.DecodingFormatMock) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) DynamicTableSourceMock(org.apache.flink.table.factories.TestDynamicTableFactory.DynamicTableSourceMock) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) DynamicTableSinkMock(org.apache.flink.table.factories.TestDynamicTableFactory.DynamicTableSinkMock) Test(org.junit.jupiter.api.Test)

Example 52 with DynamicTableSink

use of org.apache.flink.table.connector.sink.DynamicTableSink in project flink by apache.

the class FactoryUtilTest method testAlternativeValueFormat.

@Test
public void testAlternativeValueFormat() {
    final Map<String, String> options = createAllOptions();
    options.remove("value.format");
    options.remove("value.test-format.delimiter");
    options.remove("value.test-format.fail-on-missing");
    options.put("format", "test-format");
    options.put("test-format.delimiter", ";");
    options.put("test-format.fail-on-missing", "true");
    final DynamicTableSource actualSource = createTableSource(SCHEMA, options);
    final DynamicTableSource expectedSource = new DynamicTableSourceMock("MyTarget", null, new DecodingFormatMock(",", false), new DecodingFormatMock(";", true));
    assertThat(actualSource).isEqualTo(expectedSource);
    final DynamicTableSink actualSink = createTableSink(SCHEMA, options);
    final DynamicTableSink expectedSink = new DynamicTableSinkMock("MyTarget", 1000L, new EncodingFormatMock(","), new EncodingFormatMock(";"));
    assertThat(actualSink).isEqualTo(expectedSink);
}
Also used : EncodingFormatMock(org.apache.flink.table.factories.TestFormatFactory.EncodingFormatMock) DecodingFormatMock(org.apache.flink.table.factories.TestFormatFactory.DecodingFormatMock) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) DynamicTableSourceMock(org.apache.flink.table.factories.TestDynamicTableFactory.DynamicTableSourceMock) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) DynamicTableSinkMock(org.apache.flink.table.factories.TestDynamicTableFactory.DynamicTableSinkMock) Test(org.junit.jupiter.api.Test)

Example 53 with DynamicTableSink

use of org.apache.flink.table.connector.sink.DynamicTableSink in project flink by apache.

the class FactoryUtilTest method testOptionalFormat.

@Test
public void testOptionalFormat() {
    final Map<String, String> options = createAllOptions();
    options.remove("key.format");
    options.remove("key.test-format.delimiter");
    final DynamicTableSource actualSource = createTableSource(SCHEMA, options);
    final DynamicTableSource expectedSource = new DynamicTableSourceMock("MyTarget", null, null, new DecodingFormatMock("|", true));
    assertThat(actualSource).isEqualTo(expectedSource);
    final DynamicTableSink actualSink = createTableSink(SCHEMA, options);
    final DynamicTableSink expectedSink = new DynamicTableSinkMock("MyTarget", 1000L, null, new EncodingFormatMock("|"));
    assertThat(actualSink).isEqualTo(expectedSink);
}
Also used : EncodingFormatMock(org.apache.flink.table.factories.TestFormatFactory.EncodingFormatMock) DecodingFormatMock(org.apache.flink.table.factories.TestFormatFactory.DecodingFormatMock) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) DynamicTableSourceMock(org.apache.flink.table.factories.TestDynamicTableFactory.DynamicTableSourceMock) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) DynamicTableSinkMock(org.apache.flink.table.factories.TestDynamicTableFactory.DynamicTableSinkMock) Test(org.junit.jupiter.api.Test)

Example 54 with DynamicTableSink

use of org.apache.flink.table.connector.sink.DynamicTableSink 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)

Aggregations

DynamicTableSink (org.apache.flink.table.connector.sink.DynamicTableSink)54 Test (org.junit.Test)34 SinkRuntimeProviderContext (org.apache.flink.table.runtime.connector.sink.SinkRuntimeProviderContext)23 RowData (org.apache.flink.table.data.RowData)21 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)19 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)14 SinkV2Provider (org.apache.flink.table.connector.sink.SinkV2Provider)12 TestDynamicTableFactory (org.apache.flink.table.factories.TestDynamicTableFactory)12 Test (org.junit.jupiter.api.Test)10 EncodingFormatMock (org.apache.flink.table.factories.TestFormatFactory.EncodingFormatMock)8 HashMap (java.util.HashMap)7 HBaseWriteOptions (org.apache.flink.connector.hbase.options.HBaseWriteOptions)6 AvroRowDataSerializationSchema (org.apache.flink.formats.avro.AvroRowDataSerializationSchema)6 SinkFunctionProvider (org.apache.flink.table.connector.sink.SinkFunctionProvider)5 Collections (java.util.Collections)4 HBaseDynamicTableSink (org.apache.flink.connector.hbase2.sink.HBaseDynamicTableSink)4 SupportsPartitioning (org.apache.flink.table.connector.sink.abilities.SupportsPartitioning)4 DataType (org.apache.flink.table.types.DataType)4 RowType (org.apache.flink.table.types.logical.RowType)4 ArrayList (java.util.ArrayList)3