从flink的官方文档,我们知道flink的编程模型分为四层,sql层是最高层的api,Table api是中间层,DataStream/DataSet Api 是核心,stateful Streaming process层是底层实现。

其中,
flink dataset api使用及原理介绍了DataSet Api
flink DataStream API使用及原理介绍了DataStream Api
flink中的时间戳如何使用?---Watermark使用及原理介绍了底层实现的基础Watermark
flink window实例分析介绍了window的概念及使用原理
Flink中的状态与容错介绍了State的概念及checkpoint,savepoint的容错机制
上上篇<使用flink Table &Sql api来构建批量和流式应用(1)Table的基本概念>介绍了Table的基本概念及使用方法
上篇<使用flink Table &Sql api来构建批量和流式应用(2)Table API概述>
本篇主要看看Flink Sql 有哪些功能及背后的原理
1. sql功能
体现在org.apache.flink.table.api.TableEnvironment,目前flink仅支持select和insert操作
(1) select

/** * Evaluates a SQL query on registered tables and retrieves the result as a {@link Table}.
*
* <p>All tables referenced by the query must be registered in the TableEnvironment.
* A {@link Table} is automatically registered when its {@link Table#toString()} method is
* called, for example when it is embedded into a String.
* Hence, SQL queries can directly reference a {@link Table} as follows:
*
* <pre>
* {@code * Table table = ...;
* String tableName = table.toString();
* // the table is not registered to the table environment
* tEnv.sqlQuery("SELECT * FROM tableName");
* }
* </pre>
*
* @param query The SQL query to evaluate.
* @return The result of the query as Table
*/ Table sqlQuery(String query);

(2) update(当前仅支持insert)

/** * Evaluates a SQL statement such as INSERT, UPDATE or DELETE; or a DDL statement;
* NOTE: Currently only SQL INSERT statements are supported.
*
* <p>All tables referenced by the query must be registered in the TableEnvironment.
* A {@link Table} is automatically registered when its {@link Table#toString()} method is
* called, for example when it is embedded into a String.
* Hence, SQL queries can directly reference a {@link Table} as follows:
*
* <pre>
* {@code * // register the configured table sink into which the result is inserted.
* tEnv.registerTableSink("sinkTable", configuredSink);
* Table sourceTable = ...
* String tableName = sourceTable.toString();
* // sourceTable is not registered to the table environment
* tEnv.sqlUpdate(s"INSERT INTO sinkTable SELECT * FROM tableName");
* }
* </pre>
*
* @param stmt The SQL statement to evaluate.
*/void sqlUpdate(String stmt);
/** * Evaluates a SQL statement such as INSERT, UPDATE or DELETE; or a DDL statement;
* NOTE: Currently only SQL INSERT statements are supported.
*
* <p>All tables referenced by the query must be registered in the TableEnvironment.
* A {@link Table} is automatically registered when its {@link Table#toString()} method is
* called, for example when it is embedded into a String.
* Hence, SQL queries can directly reference a {@link Table} as follows:
*
* <pre>
* {@code * // register the configured table sink into which the result is inserted.
* tEnv.registerTableSink("sinkTable", configuredSink);
* Table sourceTable = ...
* String tableName = sourceTable.toString();
* // sourceTable is not registered to the table environment
* tEnv.sqlUpdate(s"INSERT INTO sinkTable SELECT * FROM tableName", config);
* }
* </pre>
*
* @param stmt The SQL statement to evaluate.
* @param config The {@link QueryConfig} to use.
*/voidsqlUpdate(String stmt, QueryConfig config);

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2730/viewspace-3163517/ ,如需转载,请注明出处,否则将追究法律责任。