Flink SQL:Queries(Overview)

本文介绍了如何在Flink中使用SQL进行数据查询,包括如何指定查询、执行查询以及语法细节。查询通过TableEnvironment的sqlQuery()方法执行,返回Table结果。表需要在TableEnvironment中注册后才能在SQL查询中使用。Flink SQL支持标准的ANSI SQL,并提供窗口函数、聚合、JOIN操作等。执行查询可以使用TableResult.collect()或TableResult.print(),它们在不同的checkpointing设置下有不同的行为保证。
摘要由CSDN通过智能技术生成

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值