Queries
SELECT statements and VALUES statements are specified with the sqlQuery() method of the TableEnvironment. The method returns the result of the SELECT statement (or the VALUES statements) as a Table. A Table can be used in subsequent SQL and Table API queries, be converted into a DataStream, or written to a TableSink. SQL and Table API queries can be seamlessly mixed and are holistically optimized and translated into a single program.
SELECT语句和VALUES语句是用TableEnvironment的sqlQuery()方法指定的。该方法将SELECT语句(或VALUES语句)的结果作为Table返回。Table可以在后续SQL和Table API查询中使用,可以转换为DataStream,也可以写入TableSink。SQL和Table API查询可以无缝混合,可以整体优化并转换为单个程序。
In order to access a table in a SQL query, it must be registered in the TableEnvironment. A table can be registered from a TableSource, Table, CREATE TABLE statement, DataStream. Alternatively, users can also register catalogs in a TableEnvironment to specify the location of the data sources.
为了在SQL查询中访问表,必须在TableEnvironment中注册该表。可以从TableSource、Table、CREATE TABLE语句、DataStream注册表。或者,用户还可以在TableEnvironment中注册catalogs,以指定数据源的位置。
For convenience, Table.toString() automatically registers the table under a unique name in its TableEnvironment and returns the name. So, Table objects can be directly inlined into SQL queries as shown in the examples below.
为方便起见,Table.toString()自动在TableEnvironment中以唯一名称注册表,并返回该名称。因此,Table对象可以直接内联到SQL查询中,如下面的示例所示。
Note: Queries that include unsupported SQL features cause a TableException. The supported features of SQL on batch and streaming tables are listed in the following sections.
注意:包含不支持的SQL功能的查询会导致TableException。后面的章节列出了批处理表和流处理表上支持的SQL功能。
Specifying a Query
The following examples show how to specify a SQL queries on registered and inlined tables.
以下示例显示如何在已注册和内联表上指定SQL查询。
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
// ingest a DataStream from an external source
DataStream<Tuple3<Long, String, Integer>> ds = env.addSource(...);
// SQL query with an inlined (unregistered) table
Table table = tableEnv.fromDataStream(ds, $("user"), $("product"), $("amount"));
Table result = tableEnv.sqlQuery(
"SELECT SUM(amount) FROM " + table + " WHERE product LIKE '%Rubber%'");
// SQL query with a registered table
// register the DataStream as view "Orders"
tableEnv.createTemporaryView("Orders", ds, $("user"), $("product"), $("amount"));
// run a SQL query on the Table and retrieve the result as a new Table
Table result2 = tableEnv.sqlQuery(
"SELECT product, amount FROM Orders WHERE product LIKE '%Rubber%'");
// create and register a TableSink
final Schema schema = Schema.newBuilder()
.column("product", DataTypes.STRING())
.column("amount", DataTypes.INT()