Search in sources :

Example 1 with StreamTableSink

use of org.apache.flink.table.sinks.StreamTableSink in project flink by apache.

the class HiveTableFactoryTest method testGenericTable.

@Test
public void testGenericTable() throws Exception {
    final TableSchema schema = TableSchema.builder().field("name", DataTypes.STRING()).field("age", DataTypes.INT()).build();
    catalog.createDatabase("mydb", new CatalogDatabaseImpl(new HashMap<>(), ""), true);
    final Map<String, String> options = Collections.singletonMap(FactoryUtil.CONNECTOR.key(), "COLLECTION");
    final CatalogTable table = new CatalogTableImpl(schema, options, "csv table");
    catalog.createTable(new ObjectPath("mydb", "mytable"), table, true);
    final Optional<TableFactory> tableFactoryOpt = catalog.getTableFactory();
    assertTrue(tableFactoryOpt.isPresent());
    final HiveTableFactory tableFactory = (HiveTableFactory) tableFactoryOpt.get();
    final TableSource tableSource = tableFactory.createTableSource(new TableSourceFactoryContextImpl(ObjectIdentifier.of("mycatalog", "mydb", "mytable"), table, new Configuration(), false));
    assertTrue(tableSource instanceof StreamTableSource);
    final TableSink tableSink = tableFactory.createTableSink(new TableSinkFactoryContextImpl(ObjectIdentifier.of("mycatalog", "mydb", "mytable"), table, new Configuration(), true, false));
    assertTrue(tableSink instanceof StreamTableSink);
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) TableSchema(org.apache.flink.table.api.TableSchema) Configuration(org.apache.flink.configuration.Configuration) HashMap(java.util.HashMap) StreamTableSink(org.apache.flink.table.sinks.StreamTableSink) TableSink(org.apache.flink.table.sinks.TableSink) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) StreamTableSink(org.apache.flink.table.sinks.StreamTableSink) CatalogTable(org.apache.flink.table.catalog.CatalogTable) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) CatalogDatabaseImpl(org.apache.flink.table.catalog.CatalogDatabaseImpl) TableSinkFactoryContextImpl(org.apache.flink.table.factories.TableSinkFactoryContextImpl) TableSource(org.apache.flink.table.sources.TableSource) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) TableFactory(org.apache.flink.table.factories.TableFactory) CatalogTableImpl(org.apache.flink.table.catalog.CatalogTableImpl) TableSourceFactoryContextImpl(org.apache.flink.table.factories.TableSourceFactoryContextImpl) Test(org.junit.Test)

Example 2 with StreamTableSink

use of org.apache.flink.table.sinks.StreamTableSink in project flink by apache.

the class CommonExecLegacySink method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<T> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    if (tableSink instanceof StreamTableSink) {
        final Transformation<T> transform;
        if (tableSink instanceof RetractStreamTableSink) {
            transform = translateToTransformation(planner, config, true);
        } else if (tableSink instanceof UpsertStreamTableSink) {
            UpsertStreamTableSink<T> upsertSink = (UpsertStreamTableSink<T>) tableSink;
            final boolean isAppendOnlyTable = !needRetraction;
            upsertSink.setIsAppendOnly(isAppendOnlyTable);
            if (upsertKeys != null) {
                upsertSink.setKeyFields(upsertKeys);
            } else {
                if (isAppendOnlyTable) {
                    upsertSink.setKeyFields(null);
                } else {
                    throw new TableException("UpsertStreamTableSink requires that Table has a full primary keys if it is updated.");
                }
            }
            transform = translateToTransformation(planner, config, true);
        } else if (tableSink instanceof AppendStreamTableSink) {
            // verify table is an insert-only (append-only) table
            if (needRetraction) {
                throw new TableException("AppendStreamTableSink requires that Table has only insert changes.");
            }
            transform = translateToTransformation(planner, config, false);
        } else {
            if (isStreaming) {
                throw new TableException("Stream Tables can only be emitted by AppendStreamTableSink, " + "RetractStreamTableSink, or UpsertStreamTableSink.");
            } else {
                transform = translateToTransformation(planner, config, false);
            }
        }
        final DataStream<T> dataStream = new DataStream<T>(planner.getExecEnv(), transform);
        final DataStreamSink<T> dsSink = (DataStreamSink<T>) ((StreamTableSink<T>) tableSink).consumeDataStream(dataStream);
        if (dsSink == null) {
            throw new TableException(String.format("The StreamTableSink#consumeDataStream(DataStream) must be implemented " + "and return the sink transformation DataStreamSink. " + "However, %s doesn't implement this method.", tableSink.getClass().getCanonicalName()));
        }
        return dsSink.getLegacyTransformation();
    } else if (tableSink instanceof DataStreamTableSink) {
        // is no real table sink, so we just need translate its input to Transformation.
        return translateToTransformation(planner, config, ((DataStreamTableSink<T>) tableSink).withChangeFlag());
    } else {
        throw new TableException(String.format("Only Support StreamTableSink! However %s is not a StreamTableSink.", tableSink.getClass().getCanonicalName()));
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) UpsertStreamTableSink(org.apache.flink.table.sinks.UpsertStreamTableSink) DataStream(org.apache.flink.streaming.api.datastream.DataStream) DataStreamTableSink(org.apache.flink.table.planner.sinks.DataStreamTableSink) AppendStreamTableSink(org.apache.flink.table.sinks.AppendStreamTableSink) UpsertStreamTableSink(org.apache.flink.table.sinks.UpsertStreamTableSink) StreamTableSink(org.apache.flink.table.sinks.StreamTableSink) DataStreamTableSink(org.apache.flink.table.planner.sinks.DataStreamTableSink) RetractStreamTableSink(org.apache.flink.table.sinks.RetractStreamTableSink) AppendStreamTableSink(org.apache.flink.table.sinks.AppendStreamTableSink) RetractStreamTableSink(org.apache.flink.table.sinks.RetractStreamTableSink) DataStreamSink(org.apache.flink.streaming.api.datastream.DataStreamSink)

Aggregations

StreamTableSink (org.apache.flink.table.sinks.StreamTableSink)2 HashMap (java.util.HashMap)1 Configuration (org.apache.flink.configuration.Configuration)1 DataStream (org.apache.flink.streaming.api.datastream.DataStream)1 DataStreamSink (org.apache.flink.streaming.api.datastream.DataStreamSink)1 TableException (org.apache.flink.table.api.TableException)1 TableSchema (org.apache.flink.table.api.TableSchema)1 CatalogDatabaseImpl (org.apache.flink.table.catalog.CatalogDatabaseImpl)1 CatalogTable (org.apache.flink.table.catalog.CatalogTable)1 CatalogTableImpl (org.apache.flink.table.catalog.CatalogTableImpl)1 ObjectPath (org.apache.flink.table.catalog.ObjectPath)1 ResolvedCatalogTable (org.apache.flink.table.catalog.ResolvedCatalogTable)1 DynamicTableSink (org.apache.flink.table.connector.sink.DynamicTableSink)1 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)1 TableFactory (org.apache.flink.table.factories.TableFactory)1 TableSinkFactoryContextImpl (org.apache.flink.table.factories.TableSinkFactoryContextImpl)1 TableSourceFactoryContextImpl (org.apache.flink.table.factories.TableSourceFactoryContextImpl)1 DataStreamTableSink (org.apache.flink.table.planner.sinks.DataStreamTableSink)1 AppendStreamTableSink (org.apache.flink.table.sinks.AppendStreamTableSink)1 RetractStreamTableSink (org.apache.flink.table.sinks.RetractStreamTableSink)1