Project: Flink (App Definition and Development / Streaming & Messaging)
File: insert.md

title: "INSERT Statement" weight: 7 type: docs aliases: /dev/table/sql/insert.html <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> INSERT statements are used to add rows to a table. {{< tabs "insert" >}} {{< tab "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. The following examples show how to run a single INSERT statement in `TableEnvironment`, run multiple INSERT statements in `StatementSet`. {{< /tab >}} {{< tab "Scala" >}} 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. The following examples show how to run a single INSERT statement in `TableEnvironment`, run multiple INSERT statements in `StatementSet`. {{< /tab >}} {{< tab "Python" >}} 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. The following examples show how to run a single INSERT statement in `TableEnvironment`, run multiple INSERT statements in `StatementSet`. {{< /tab >}} {{< tab "SQL CLI" >}} Single INSERT statement can be executed in . The following examples show how to run a single INSERT statement in SQL CLI. {{< /tab >}} {{< /tabs >}} {{< tabs "15bc87ce-93fd-4fdd-8c51-3301a432c048" >}} {{< tab "Java" >}} ```java 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 (...)");

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 WHERE product LIKE '%Rubber%'"); stmtSet.addInsertSql( "INSERT INTO GlassOrders SELECT product, amount FROM Orders WHERE product LIKE '%Glass%'"); // execute all statements together TableResult tableResult2 = stmtSet.execute(); // get job status through TableResult System.out.println(tableResult2.getJobClient().get().getJobStatus()); ``` {{< /tab >}} {{< tab "Scala" >}} ```scala val tEnv = TableEnvironment.create(...) // register a source table named "Orders" and a sink table named "RubberOrders" tEnv.executeSql("CREATE TABLE Orders (`user` BIGINT, product STRING, amount INT) WITH (...)") tEnv.executeSql("CREATE TABLE RubberOrders(product STRING, amount INT) WITH (...)") // run a single INSERT query on the registered source table and emit the result to registered sink table val tableResult1 = tEnv.executeSql( "INSERT INTO RubberOrders SELECT product, amount FROM Orders WHERE product LIKE '%Rubber%'") // get job status through TableResult 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 val stmtSet = tEnv.createStatementSet() // only single INSERT query can be accepted by `addInsertSql` method stmtSet.addInsertSql( "INSERT INTO RubberOrders SELECT product, amount FROM Orders WHERE product LIKE '%Rubber%'") stmtSet.addInsertSql( "INSERT INTO GlassOrders SELECT product, amount FROM Orders WHERE product LIKE '%Glass%'") // execute all statements together val tableResult2 = stmtSet.execute() // get job status through TableResult println(tableResult2.getJobClient().get().getJobStatus()) ``` {{< /tab >}} {{< tab "Python" >}} ```python table_env = TableEnvironment.create(...) tableenv.executesql("CREATE TABLE Orders (`user` BIGINT, product STRING, amount INT) WITH (...)") tableenv.executesql("CREATE TABLE RubberOrders(product STRING, amount INT) WITH (...)") tableresult1 = tableenv \ .execute_sql("INSERT INTO RubberOrders SELECT product, amount FROM Orders WHERE product LIKE '%Rubber%'") print(tableresult1getjobclient().getjob_status()) tableenv.executesql("CREATE TABLE GlassOrders(product VARCHAR, amount INT) WITH (...)") stmtset = tableenv.createstatementset() stmt_set \ .addinsertsql("INSERT INTO RubberOrders SELECT product, amount FROM Orders WHERE product LIKE '%Rubber%'") stmt_set \ .addinsertsql("INSERT INTO GlassOrders SELECT product, amount FROM Orders WHERE product LIKE '%Glass%'") tableresult2 = stmtset.execute() print(tableresult2.getjobclient().getjob_status()) ``` {{< /tab >}} {{< tab "SQL CLI" >}} ```sql Flink SQL> CREATE TABLE Orders (`user` BIGINT, product STRING, amount INT) WITH (...); [INFO] Table has been created. Flink SQL> CREATE TABLE RubberOrders(product STRING, amount INT) WITH (...); Flink SQL> SHOW TABLES; Orders RubberOrders Flink SQL> INSERT INTO RubberOrders SELECT product, amount FROM Orders WHERE product LIKE '%Rubber%'; [INFO] Submitting SQL update statement to the

[INFO] Table update statement has been successfully submitted to the cluster: ``` {{< /tab >}} {{< /tabs >}} {{< top >}} Query Results can be inserted into tables by using the insert clause. ```sql tablename [PARTITION partspec] [columnlist] selectstatement part_spec: (partcolname1=val1 [, partcolname2=val2, ...]) column_list: (colname1 [, columnname2, ...]) ``` OVERWRITE `INSERT OVERWRITE` will overwrite any existing data in the table or partition. Otherwise, new data is appended. PARTITION `PARTITION` clause should contain static partition columns of this inserting. COLUMN LIST Given a table T(a INT, b INT, c INT), Flink supports INSERT INTO T(c, b) SELECT x, y FROM S. The expectation is that 'x' is written to column 'c' and 'y' is written to column 'b' and 'a' is set to NULL (assuming column 'a' is nullable).<br /> For connector developers who want to avoid overwriting non-target columns with null values when processing partial column updates, you can get the information about the target columns specified by the user's insert statement from {{< gh_link file="flink-table/flink-table-common/src/main/java/org/apache/flink/table/connector/sink/DynamicTableSink.java" name="DynamicTableSink$Context.getTargetColumns()" >}} and decide how to process the partial updates. ```sql -- Creates a partitioned table CREATE TABLE countrypageview (user STRING, cnt INT, date STRING, country STRING) PARTITIONED BY (date, country) WITH (...) -- Appends rows into the static partition (date='2019-8-30', country='China') INSERT INTO countrypageview PARTITION (date='2019-8-30', country='China') SELECT user, cnt FROM pageviewsource; -- Key word EXECUTE can be added at the beginning of Insert to indicate explicitly that we are going to execute the statement, -- it is equivalent to Statement without the key word. EXECUTE INSERT INTO countrypageview PARTITION (date='2019-8-30', country='China') SELECT user, cnt FROM pageviewsource; -- Appends rows into partition (date, country), where date is static partition with value '2019-8-30', -- country is dynamic partition whose value is dynamic determined by each row. INSERT INTO countrypageview PARTITION (date='2019-8-30') SELECT user, cnt, country FROM pageviewsource; -- Overwrites rows into static partition (date='2019-8-30', country='China') INSERT OVERWRITE countrypageview PARTITION (date='2019-8-30', country='China') SELECT user, cnt FROM pageviewsource; -- Overwrites rows into partition (date, country), where date is static partition with value '2019-8-30', -- country is dynamic partition whose value is dynamic determined by each row. INSERT OVERWRITE countrypageview PARTITION (date='2019-8-30') SELECT user, cnt, country FROM pageviewsource; -- Appends rows into the static partition (date='2019-8-30', country='China') -- the column cnt is set to NULL INSERT INTO countrypageview PARTITION (date='2019-8-30', country='China') (user) SELECT user FROM pageviewsource; ``` The INSERT...VALUES statement can be used to insert data into tables directly from SQL. ```sql tablename VALUES valuesrow [, values_row ...] values_row: (val1 [, val2, ...]) ``` OVERWRITE `INSERT OVERWRITE` will overwrite any existing data in the table. Otherwise, new data is appended. ```sql CREATE TABLE students (name STRING, age INT, gpa DECIMAL(3, 2)) WITH (...); INSERT INTO students VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32); ``` The `STATEMENT SET` can be used to insert data into multiple tables in a statement. ```sql EXECUTE STATEMENT SET BEGIN insert_statement; ... insert_statement; END; insert_statement: <insertfromselect>|<insertfromvalues> ``` ```sql CREATE TABLE students (name STRING, age INT, gpa DECIMAL(3, 2)) WITH (...); EXECUTE STATEMENT SET BEGIN INSERT INTO students VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32); INSERT INTO students VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32); END; ``` {{< top >}}