Search in sources :

Example 1 with StreamTableSource

use of org.apache.flink.table.sources.StreamTableSource 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 StreamTableSource

use of org.apache.flink.table.sources.StreamTableSource in project flink by apache.

the class SimpleCatalogFactory method createCatalog.

@Override
public Catalog createCatalog(Context context) {
    final Configuration configuration = Configuration.fromMap(context.getOptions());
    final String database = configuration.getString(DEFAULT_DATABASE);
    final String tableName = configuration.getString(TABLE_NAME);
    final GenericInMemoryCatalog genericInMemoryCatalog = new GenericInMemoryCatalog(context.getName(), database);
    StreamTableSource<Row> tableSource = new StreamTableSource<Row>() {

        @Override
        public DataStream<Row> getDataStream(StreamExecutionEnvironment execEnv) {
            return execEnv.fromCollection(TABLE_CONTENTS).returns(new RowTypeInfo(new TypeInformation[] { Types.INT(), Types.STRING() }, new String[] { "id", "string" }));
        }

        @Override
        public TableSchema getTableSchema() {
            return TableSchema.builder().field("id", DataTypes.INT()).field("string", DataTypes.STRING()).build();
        }

        @Override
        public DataType getProducedDataType() {
            return DataTypes.ROW(DataTypes.FIELD("id", DataTypes.INT()), DataTypes.FIELD("string", DataTypes.STRING())).notNull();
        }
    };
    try {
        genericInMemoryCatalog.createTable(new ObjectPath(database, tableName), ConnectorCatalogTable.source(tableSource, false), false);
    } catch (Exception e) {
        throw new WrappingRuntimeException(e);
    }
    return genericInMemoryCatalog;
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) WrappingRuntimeException(org.apache.flink.util.WrappingRuntimeException) Configuration(org.apache.flink.configuration.Configuration) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) Row(org.apache.flink.types.Row) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) GenericInMemoryCatalog(org.apache.flink.table.catalog.GenericInMemoryCatalog) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) WrappingRuntimeException(org.apache.flink.util.WrappingRuntimeException)

Example 3 with StreamTableSource

use of org.apache.flink.table.sources.StreamTableSource in project flink by apache.

the class CommonExecLegacyTableSourceScan method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    final Transformation<?> sourceTransform;
    final StreamExecutionEnvironment env = planner.getExecEnv();
    if (tableSource instanceof InputFormatTableSource) {
        InputFormatTableSource<Object> inputFormat = (InputFormatTableSource<Object>) tableSource;
        TypeInformation<Object> typeInfo = (TypeInformation<Object>) fromDataTypeToTypeInfo(inputFormat.getProducedDataType());
        InputFormat<Object, ?> format = inputFormat.getInputFormat();
        sourceTransform = createInput(env, format, typeInfo);
    } else if (tableSource instanceof StreamTableSource) {
        sourceTransform = ((StreamTableSource<?>) tableSource).getDataStream(env).getTransformation();
    } else {
        throw new UnsupportedOperationException(tableSource.getClass().getSimpleName() + " is unsupported.");
    }
    final TypeInformation<?> inputType = sourceTransform.getOutputType();
    final DataType producedDataType = tableSource.getProducedDataType();
    // check that declared and actual type of table source DataStream are identical
    if (!inputType.equals(TypeInfoDataTypeConverter.fromDataTypeToTypeInfo(producedDataType))) {
        throw new TableException(String.format("TableSource of type %s " + "returned a DataStream of data type %s that does not match with the " + "data type %s declared by the TableSource.getProducedDataType() method. " + "Please validate the implementation of the TableSource.", tableSource.getClass().getCanonicalName(), inputType, producedDataType));
    }
    final RowType outputType = (RowType) getOutputType();
    final RelDataType relDataType = FlinkTypeFactory.INSTANCE().buildRelNodeRowType(outputType);
    // get expression to extract rowtime attribute
    final Optional<RexNode> rowtimeExpression = JavaScalaConversionUtil.toJava(TableSourceUtil.getRowtimeAttributeDescriptor(tableSource, relDataType)).map(desc -> TableSourceUtil.getRowtimeExtractionExpression(desc.getTimestampExtractor(), producedDataType, planner.getRelBuilder(), getNameRemapping()));
    return createConversionTransformationIfNeeded(planner.getExecEnv(), config, sourceTransform, rowtimeExpression.orElse(null));
}
Also used : TableException(org.apache.flink.table.api.TableException) RowType(org.apache.flink.table.types.logical.RowType) RelDataType(org.apache.calcite.rel.type.RelDataType) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) DataType(org.apache.flink.table.types.DataType) RelDataType(org.apache.calcite.rel.type.RelDataType) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) InputFormatTableSource(org.apache.flink.table.sources.InputFormatTableSource) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with StreamTableSource

use of org.apache.flink.table.sources.StreamTableSource in project flink by apache.

the class CatalogSchemaTable method findAndCreateTableSource.

private Optional<TableSource<?>> findAndCreateTableSource() {
    Optional<TableSource<?>> tableSource = Optional.empty();
    try {
        if (contextResolvedTable.getTable() instanceof CatalogTable) {
            // Use an empty config for TableSourceFactoryContextImpl since we can't fetch the
            // actual TableConfig here. And currently the empty config do not affect the logic.
            ReadableConfig config = new Configuration();
            TableSourceFactory.Context context = new TableSourceFactoryContextImpl(contextResolvedTable.getIdentifier(), contextResolvedTable.getTable(), config, contextResolvedTable.isTemporary());
            TableSource<?> source = TableFactoryUtil.findAndCreateTableSource(context);
            if (source instanceof StreamTableSource) {
                if (!isStreamingMode && !((StreamTableSource<?>) source).isBounded()) {
                    throw new ValidationException("Cannot query on an unbounded source in batch mode, but " + contextResolvedTable.getIdentifier().asSummaryString() + " is unbounded.");
                }
                tableSource = Optional.of(source);
            } else {
                throw new ValidationException("Catalog tables only support " + "StreamTableSource and InputFormatTableSource.");
            }
        }
    } catch (Exception e) {
        tableSource = Optional.empty();
    }
    return tableSource;
}
Also used : TableSourceFactory(org.apache.flink.table.factories.TableSourceFactory) ReadableConfig(org.apache.flink.configuration.ReadableConfig) TableSource(org.apache.flink.table.sources.TableSource) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) ValidationException(org.apache.flink.table.api.ValidationException) Configuration(org.apache.flink.configuration.Configuration) ConnectorCatalogTable(org.apache.flink.table.catalog.ConnectorCatalogTable) CatalogTable(org.apache.flink.table.catalog.CatalogTable) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) ValidationException(org.apache.flink.table.api.ValidationException) TableSourceFactoryContextImpl(org.apache.flink.table.factories.TableSourceFactoryContextImpl)

Aggregations

StreamTableSource (org.apache.flink.table.sources.StreamTableSource)4 Configuration (org.apache.flink.configuration.Configuration)3 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 CatalogTable (org.apache.flink.table.catalog.CatalogTable)2 ObjectPath (org.apache.flink.table.catalog.ObjectPath)2 TableSourceFactoryContextImpl (org.apache.flink.table.factories.TableSourceFactoryContextImpl)2 TableSource (org.apache.flink.table.sources.TableSource)2 HashMap (java.util.HashMap)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexNode (org.apache.calcite.rex.RexNode)1 RowTypeInfo (org.apache.flink.api.java.typeutils.RowTypeInfo)1 ReadableConfig (org.apache.flink.configuration.ReadableConfig)1 TableException (org.apache.flink.table.api.TableException)1 TableSchema (org.apache.flink.table.api.TableSchema)1 ValidationException (org.apache.flink.table.api.ValidationException)1 CatalogDatabaseImpl (org.apache.flink.table.catalog.CatalogDatabaseImpl)1 CatalogTableImpl (org.apache.flink.table.catalog.CatalogTableImpl)1 ConnectorCatalogTable (org.apache.flink.table.catalog.ConnectorCatalogTable)1 GenericInMemoryCatalog (org.apache.flink.table.catalog.GenericInMemoryCatalog)1