org.apache.flink.table.factories.TableFactory Java Examples

The following examples show how to use org.apache.flink.table.factories.TableFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HiveTableFactoryTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericTable() throws Exception {
	TableSchema schema = TableSchema.builder()
		.field("name", DataTypes.STRING())
		.field("age", DataTypes.INT())
		.build();

	Map<String, String> properties = new HashMap<>();
	properties.put(CatalogConfig.IS_GENERIC, String.valueOf(true));
	properties.put("connector", "COLLECTION");

	catalog.createDatabase("mydb", new CatalogDatabaseImpl(new HashMap<>(), ""), true);
	ObjectPath path = new ObjectPath("mydb", "mytable");
	CatalogTable table = new CatalogTableImpl(schema, properties, "csv table");
	catalog.createTable(path, table, true);
	Optional<TableFactory> opt = catalog.getTableFactory();
	assertTrue(opt.isPresent());
	HiveTableFactory tableFactory = (HiveTableFactory) opt.get();
	TableSource tableSource = tableFactory.createTableSource(path, table);
	assertTrue(tableSource instanceof StreamTableSource);
	TableSink tableSink = tableFactory.createTableSink(path, table);
	assertTrue(tableSink instanceof StreamTableSink);
}
 
Example #2
Source File: HiveTableFactoryTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testHiveTable() throws Exception {
	TableSchema schema = TableSchema.builder()
		.field("name", DataTypes.STRING())
		.field("age", DataTypes.INT())
		.build();

	Map<String, String> properties = new HashMap<>();

	catalog.createDatabase("mydb", new CatalogDatabaseImpl(new HashMap<>(), ""), true);
	ObjectPath path = new ObjectPath("mydb", "mytable");
	CatalogTable table = new CatalogTableImpl(schema, properties, "hive table");
	catalog.createTable(path, table, true);
	Optional<TableFactory> opt = catalog.getTableFactory();
	assertTrue(opt.isPresent());
	HiveTableFactory tableFactory = (HiveTableFactory) opt.get();
	TableSink tableSink = tableFactory.createTableSink(path, table);
	assertTrue(tableSink instanceof HiveTableSink);
	TableSource tableSource = tableFactory.createTableSource(path, table);
	assertTrue(tableSource instanceof HiveTableSource);
}
 
Example #3
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 6 votes vote down vote up
private Table convertTable(ObjectIdentifier identifier, TableLookupResult lookupResult, @Nullable TableFactory tableFactory) {
	CatalogBaseTable table = lookupResult.getTable();
	TableSchema resolvedSchema = lookupResult.getResolvedSchema();
	if (table instanceof QueryOperationCatalogView) {
		return QueryOperationCatalogViewTable.createCalciteTable(
			((QueryOperationCatalogView) table),
			resolvedSchema);
	} else if (table instanceof ConnectorCatalogTable) {
		return convertConnectorTable((ConnectorCatalogTable<?, ?>) table, resolvedSchema);
	} else {
		if (table instanceof CatalogTable) {
			return convertCatalogTable(
				identifier,
				(CatalogTable) table,
				resolvedSchema,
				tableFactory);
		} else if (table instanceof CatalogView) {
			return convertCatalogView(
				identifier.getObjectName(),
				(CatalogView) table,
				resolvedSchema);
		} else {
			throw new TableException("Unsupported table type: " + table);
		}
	}
}
 
Example #4
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Table getTable(String tableName) {
	ObjectIdentifier identifier = ObjectIdentifier.of(catalogName, databaseName, tableName);
	return catalogManager.getTable(identifier)
		.map(result -> {
			final TableFactory tableFactory;
			if (result.isTemporary()) {
				tableFactory = null;
			} else {
				tableFactory = catalogManager.getCatalog(catalogName)
					.flatMap(Catalog::getTableFactory)
					.orElse(null);
			}
			return convertTable(identifier, result, tableFactory);
		})
		.orElse(null);
}
 
Example #5
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 6 votes vote down vote up
private Table convertCatalogTable(ObjectPath tablePath, CatalogTable table) {
	TableSource<?> tableSource;
	Optional<TableFactory> tableFactory = catalog.getTableFactory();
	if (tableFactory.isPresent()) {
		TableFactory tf = tableFactory.get();
		if (tf instanceof TableSourceFactory) {
			tableSource = ((TableSourceFactory) tf).createTableSource(tablePath, table);
		} else {
			throw new TableException(String.format("Cannot query a sink-only table. TableFactory provided by catalog %s must implement TableSourceFactory",
				catalog.getClass()));
		}
	} else {
		tableSource = TableFactoryUtil.findAndCreateTableSource(table);
	}

	if (!(tableSource instanceof StreamTableSource)) {
		throw new TableException("Catalog tables support only StreamTableSource and InputFormatTableSource");
	}

	return new TableSourceTable<>(
		tableSource,
		!((StreamTableSource<?>) tableSource).isBounded(),
		FlinkStatistic.UNKNOWN()
	);
}
 
Example #6
Source File: AmbiguousTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public AmbiguousTableFactoryException(
		List<? extends TableFactory> matchingFactories,
		Class<? extends TableFactory> factoryClass,
		List<TableFactory> factories,
		Map<String, String> properties) {

	this(matchingFactories, factoryClass, factories, properties, null);
}
 
Example #7
Source File: NoMatchingTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public NoMatchingTableFactoryException(
	String message,
	@Nullable String matchCandidatesMessage,
	Class<?> factoryClass,
	List<TableFactory> factories,
	Map<String, String> properties,
	Throwable cause) {

	super(cause);
	this.message = message;
	this.matchCandidatesMessage = matchCandidatesMessage;
	this.factoryClass = factoryClass;
	this.factories = factories;
	this.properties = properties;
}
 
Example #8
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private Table convertCatalogTable(
		ObjectIdentifier identifier,
		CatalogTable table,
		TableSchema resolvedSchema,
		@Nullable TableFactory tableFactory) {
	final TableSource<?> tableSource;
	final TableSourceFactory.Context context = new TableSourceFactoryContextImpl(
			identifier, table, tableConfig.getConfiguration());
	if (tableFactory != null) {
		if (tableFactory instanceof TableSourceFactory) {
			tableSource = ((TableSourceFactory<?>) tableFactory).createTableSource(context);
		} else {
			throw new TableException(
				"Cannot query a sink-only table. TableFactory provided by catalog must implement TableSourceFactory");
		}
	} else {
		tableSource = TableFactoryUtil.findAndCreateTableSource(context);
	}

	if (!(tableSource instanceof StreamTableSource)) {
		throw new TableException("Catalog tables support only StreamTableSource and InputFormatTableSource");
	}

	return new TableSourceTable<>(
		resolvedSchema,
		tableSource,
		// this means the TableSource extends from StreamTableSource, this is needed for the
		// legacy Planner. Blink Planner should use the information that comes from the TableSource
		// itself to determine if it is a streaming or batch source.
		isStreamingMode,
		FlinkStatistic.UNKNOWN()
	);
}
 
Example #9
Source File: HiveTableFactoryTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testHiveTable() throws Exception {
	TableSchema schema = TableSchema.builder()
		.field("name", DataTypes.STRING())
		.field("age", DataTypes.INT())
		.build();

	Map<String, String> properties = new HashMap<>();
	properties.put(CatalogConfig.IS_GENERIC, String.valueOf(false));

	catalog.createDatabase("mydb", new CatalogDatabaseImpl(new HashMap<>(), ""), true);
	ObjectPath path = new ObjectPath("mydb", "mytable");
	CatalogTable table = new CatalogTableImpl(schema, properties, "hive table");
	catalog.createTable(path, table, true);
	Optional<TableFactory> opt = catalog.getTableFactory();
	assertTrue(opt.isPresent());
	HiveTableFactory tableFactory = (HiveTableFactory) opt.get();
	TableSink tableSink = tableFactory.createTableSink(new TableSinkFactoryContextImpl(
			ObjectIdentifier.of("mycatalog", "mydb", "mytable"),
			table,
			new Configuration(),
			true));
	assertTrue(tableSink instanceof HiveTableSink);
	TableSource tableSource = tableFactory.createTableSource(new TableSourceFactoryContextImpl(
			ObjectIdentifier.of("mycatalog", "mydb", "mytable"), table, new Configuration()));
	assertTrue(tableSource instanceof HiveTableSource);
}
 
Example #10
Source File: HiveTableFactoryTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericTable() throws Exception {
	TableSchema schema = TableSchema.builder()
		.field("name", DataTypes.STRING())
		.field("age", DataTypes.INT())
		.build();

	Map<String, String> properties = new HashMap<>();
	properties.put(CatalogConfig.IS_GENERIC, String.valueOf(true));
	properties.put("connector", "COLLECTION");

	catalog.createDatabase("mydb", new CatalogDatabaseImpl(new HashMap<>(), ""), true);
	ObjectPath path = new ObjectPath("mydb", "mytable");
	CatalogTable table = new CatalogTableImpl(schema, properties, "csv table");
	catalog.createTable(path, table, true);
	Optional<TableFactory> opt = catalog.getTableFactory();
	assertTrue(opt.isPresent());
	HiveTableFactory tableFactory = (HiveTableFactory) opt.get();
	TableSource tableSource = tableFactory.createTableSource(new TableSourceFactoryContextImpl(
			ObjectIdentifier.of("mycatalog", "mydb", "mytable"), table, new Configuration()));
	assertTrue(tableSource instanceof StreamTableSource);
	TableSink tableSink = tableFactory.createTableSink(new TableSinkFactoryContextImpl(
			ObjectIdentifier.of("mycatalog", "mydb", "mytable"),
			table,
			new Configuration(),
			true));
	assertTrue(tableSink instanceof StreamTableSink);
}
 
Example #11
Source File: NoMatchingTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public NoMatchingTableFactoryException(
	String message,
	Class<?> factoryClass,
	List<TableFactory> factories,
	Map<String, String> properties) {
	this(message, null, factoryClass, factories, properties, null);
}
 
Example #12
Source File: NoMatchingTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public NoMatchingTableFactoryException(
	String message,
	@Nullable String matchCandidatesMessage,
	Class<?> factoryClass,
	List<TableFactory> factories,
	Map<String, String> properties) {
	this(message, matchCandidatesMessage, factoryClass, factories, properties, null);
}
 
Example #13
Source File: AmbiguousTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public AmbiguousTableFactoryException(
		List<? extends TableFactory> matchingFactories,
		Class<? extends TableFactory> factoryClass,
		List<TableFactory> factories,
		Map<String, String> properties,
		Throwable cause) {

	super(cause);
	this.matchingFactories = matchingFactories;
	this.factoryClass = factoryClass;
	this.factories = factories;
	this.properties = properties;
}
 
Example #14
Source File: NoMatchingTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public NoMatchingTableFactoryException(
	String message,
	Class<?> factoryClass,
	List<TableFactory> factories,
	Map<String, String> properties) {

	this(message, factoryClass, factories, properties, null);
}
 
Example #15
Source File: NoMatchingTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public NoMatchingTableFactoryException(
	String message,
	Class<?> factoryClass,
	List<TableFactory> factories,
	Map<String, String> properties,
	Throwable cause) {

	super(cause);
	this.message = message;
	this.factoryClass = factoryClass;
	this.factories = factories;
	this.properties = properties;
}
 
Example #16
Source File: AmbiguousTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public AmbiguousTableFactoryException(
		List<? extends TableFactory> matchingFactories,
		Class<? extends TableFactory> factoryClass,
		List<TableFactory> factories,
		Map<String, String> properties,
		Throwable cause) {

	super(cause);
	this.matchingFactories = matchingFactories;
	this.factoryClass = factoryClass;
	this.factories = factories;
	this.properties = properties;
}
 
Example #17
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private Table convertCatalogTable(ObjectPath tablePath, CatalogTable table) {
	TableSource<?> tableSource;
	Optional<TableFactory> tableFactory = catalog.getTableFactory();
	if (tableFactory.isPresent()) {
		TableFactory tf = tableFactory.get();
		if (tf instanceof TableSourceFactory) {
			tableSource = ((TableSourceFactory) tf).createTableSource(tablePath, table);
		} else {
			throw new TableException(String.format("Cannot query a sink-only table. TableFactory provided by catalog %s must implement TableSourceFactory",
				catalog.getClass()));
		}
	} else {
		tableSource = TableFactoryUtil.findAndCreateTableSource(table);
	}

	if (!(tableSource instanceof StreamTableSource)) {
		throw new TableException("Catalog tables support only StreamTableSource and InputFormatTableSource");
	}

	return new TableSourceTable<>(
		tableSource,
		// this means the TableSource extends from StreamTableSource, this is needed for the
		// legacy Planner. Blink Planner should use the information that comes from the TableSource
		// itself to determine if it is a streaming or batch source.
		isStreamingMode,
		FlinkStatistic.UNKNOWN()
	);
}
 
Example #18
Source File: AmbiguousTableFactoryException.java    From flink with Apache License 2.0 5 votes vote down vote up
public AmbiguousTableFactoryException(
		List<? extends TableFactory> matchingFactories,
		Class<? extends TableFactory> factoryClass,
		List<TableFactory> factories,
		Map<String, String> properties) {

	this(matchingFactories, factoryClass, factories, properties, null);
}
 
Example #19
Source File: HiveCatalog.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<TableFactory> getTableFactory() {
	return Optional.of(new HiveTableFactory(hiveConf));
}
 
Example #20
Source File: PulsarCatalog.java    From pulsar-flink with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<TableFactory> getTableFactory() {
    Properties props = new Properties();
    props.putAll(properties);
    return Optional.of(new PulsarTableSourceSinkFactory(props));
}
 
Example #21
Source File: KuduCatalog.java    From bahir-flink with Apache License 2.0 4 votes vote down vote up
public Optional<TableFactory> getTableFactory() {
    return Optional.of(getKuduTableFactory());
}
 
Example #22
Source File: HiveCatalog.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<TableFactory> getTableFactory() {
	return Optional.of(new HiveTableFactory(hiveConf));
}
 
Example #23
Source File: Catalog.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Get an optional {@link TableFactory} instance that's responsible for generating table-related
 * instances stored in this catalog, instances such as source/sink.
 *
 * @return an optional TableFactory instance
 * @deprecated Use {@link #getFactory()} for the new factory stack. The new factory stack uses the
 *             new table sources and sinks defined in FLIP-95 and a slightly different discovery mechanism.
 */
@Deprecated
default Optional<TableFactory> getTableFactory() {
	return Optional.empty();
}
 
Example #24
Source File: Catalog.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Get an optional {@link TableFactory} instance that's responsible for generating table-related
 * instances stored in this catalog, instances such as source/sink and function definitions.
 *
 * @return an optional TableFactory instance
 */
default Optional<TableFactory> getTableFactory() {
	return Optional.empty();
}