INSERT Statement
INSERT statements are used to add rows to a table.
INSERT语句用于向表中添加行。
Run an INSERT statement
Java
Single INSERT statement can be executed through the executeSql() method of the TableEnvironment. The executeSql() method for INSERT statement will submit a Flink job immediately, and return a TableResult instance which associates the submitted job. Multiple INSERT statements can be executed through the addInsertSql() method of the StatementSet which can be created by the TableEnvironment.createStatementSet() method. The addInsertSql() method is a lazy execution, they will be executed only when StatementSet.execute() is invoked.
单个INSERT语句可以通过TableEnvironment的executeSql()方法执行。执行INSERT语句的executeSql()方法将立即提交Flink作业,并返回与提交的作业关联的TableResult实例。可以通过TableEnvironment.createStatementSet()创建的StatementSet的addInsertSql()方法执行多个INSERT语句。addInsertSql()方法是一个延迟执行,它们将仅在调用StatementSet.execute()时执行。
The following examples show how to run a single INSERT statement in TableEnvironment, run multiple INSERT statements in StatementSet.
以下示例演示如何在TableEnvironment中运行单个INSERT语句,在StatementSet中运行多个INSERT。
TableEnvironment tEnv = TableEnvironment.create(...);
// register a source table named "Orders" and a sink table named "RubberOrders"
tEnv.executeSql("CREATE TABLE Orders (`user` BIGINT, product VARCHAR, amount INT) WITH (...)");
tEnv.executeSql("CREATE TABLE RubberOrders(product VARCHAR, amount INT) WITH (...)");
// run a single INSERT query on the registered source table and emit the result to registered sink table
TableResult tableResult1 = tEnv.executeSql(
"INSERT INTO RubberOrders SELECT product, amount FROM Orders WHERE product LIKE '%Rubber%'");
// get job status through TableResult
System.out.println(tableResult1.getJobClient().get().getJobStatus());
//----------------------------------------------------------------------------
// register another sink table named "GlassOrders" for multiple INSERT queries
tEnv.executeSql("CREATE TABLE GlassOrders(product VARCHAR, amount INT) WITH (...)");
// run multiple INSERT queries on the registered source table and emit the result to registered sink tables
StatementSet stmtSet = tEnv.createStatementSet();
// only single INSERT query can be accepted by `addInsertSql` method
stmtSet.addInsertSql(
"INSERT INTO RubberOrders SELECT product, amount FROM Orders WHE