Updated on 2026-11-06 GMT+08:00

INSERT INTO

Overview

The INSERT INTO statement is a core data writing statement in Flink SQL. It is used to write the result of a SELECT query or explicitly specified values to a target table (sink table). AI DataLake supports INSERT INTO, which appends data to the target table without overwriting existing data. This is the most commonly used write mode and applies to both streaming and batch processing scenarios.

Additionally, AI DataLake supports using the STATEMENT SET syntax to combine multiple INSERT statements into a single Flink job for execution. This allows multiple write operations to share the same directed acyclic graph (DAG), thereby reducing TaskManager resource overhead. This mode is suitable for multi-sink data distribution scenarios.

Application Scenarios

  • Real-time data ingestion: Real-time data from message queues such as Kafka can be written to storage systems like Paimon or Hive using INSERT INTO to build a real-time data warehouse.
  • Multi-sink data distribution: After ETL processing, data from the same source needs to be written to multiple target tables (for example, detail tables and aggregation tables simultaneously). Using STATEMENT SET combines these operations into one job, reducing resource consumption.
  • Dynamic parameter tuning: You can use table hints to dynamically adjust connector parameters in INSERT statements, allowing the table to adapt to different running environments without modifying the table DDL.

Notes and Constraints

  • All INSERT statements in a STATEMENT SET must share the same source table. Different INSERT statements cannot use completely independent source tables. Otherwise, they may not be combined into the same DAG.
  • Multiple INSERT statements in a STATEMENT SET are submitted as a whole. You cannot individually control the start or stop of a single INSERT statement.
  • The number and types of columns in the SELECT clause of the INSERT statement must match the column definitions of the target table, or the corresponding columns must be specified in the explicit column_list.
  • When writing to a partition, the dynamic partition columns must appear at the end of the SELECT clause and be in the same order as declared in the PARTITION clause.
  • Table hints (/*+ OPTIONS(...) */) take effect only for the table instance in the current INSERT statement and do not modify the persistent definition of the table in the catalog.

Syntax Structure

INSERT INTO single-sink write

INSERT INTO [catalog_name.][db_name.]table_name
[PARTITION part_spec]
[column_list]
select_statement | VALUES (value [, value ...]) [, (value [, value ...]) ...];
part_spec ::= (key1=value1, key2=value2, ...)
column_list ::= (col1 [, col2, ...])

INSERT INTO multi-sink write

EXECUTE STATEMENT SET
BEGIN
INSERT INTO sink_table1 select_statement1;
INSERT INTO sink_table2 select_statement2;
[...]
END;

All INSERT statements between EXECUTE STATEMENT SET and END are combined into a single Flink job. Multiple INSERT statements share the source operator and are executed in the same DAG, effectively reducing TaskManager resource usage.

Dynamic parameter passing using table hints

INSERT INTO table_name /*+ OPTIONS('key1'='value1', 'key2'='value2') */
select_statement;

Usage Example

Example 1: INSERT INTO single-sink write

Use the DataGen connector to generate simulated sensor data and use the INSERT INTO statement to write the data to the Print connector for output.

-- Create a sensor data source table.
CREATE TABLE sensor_source (
sensor_id INT,
temperature DOUBLE,
humidity DOUBLE,
record_time TIMESTAMP(3),
WATERMARK FOR record_time AS record_time - INTERVAL '5' SECOND
) WITH (
'connector' = 'datagen',
'rows-per-second' = '10',
'fields.sensor_id.kind' = 'random',
'fields.sensor_id.min' = '1',
'fields.sensor_id.max' = '100',
'fields.temperature.min' = '15.0',
'fields.temperature.max' = '40.0',
'fields.humidity.min' = '30.0',
'fields.humidity.max' = '90.0'
);
-- Create the sensor data result table.
CREATE TABLE sensor_sink (
sensor_id INT,
temperature DOUBLE,
humidity DOUBLE
) WITH (
'connector' = 'print',
'print-identifier' = 'sensor_data'
);
-- Submit the job: Filter sensor data with temperature exceeding 30°C.
INSERT INTO sensor_sink
SELECT sensor_id, temperature, humidity
FROM sensor_source
WHERE temperature > 30;

Example 2: INSERT INTO multi-sink write (STATEMENT SET)

Use the DataGen connector to generate order data, and use STATEMENT SET to write the data to the detail table and the aggregate statistics table respectively. The two INSERT statements are executed in the same Flink job.

-- Create an order data source table.

CREATE TABLE order_source (
order_id BIGINT,
customer_id BIGINT,
product_id BIGINT,
amount DECIMAL(10, 2),
order_time TIMESTAMP(3),
WATERMARK FOR order_time AS order_time - INTERVAL '5' SECOND
) WITH (
'connector' = 'datagen',
'rows-per-second' = '100',
'fields.order_id.kind' = 'sequence',
'fields.order_id.start' = '1',
'fields.order_id.end' = '1000000',
'fields.customer_id.kind' = 'random',
'fields.customer_id.min' = '1',
'fields.customer_id.max' = '5000',
'fields.product_id.kind' = 'random',
'fields.product_id.min' = '1',
'fields.product_id.max' = '200',
'fields.amount.min' = '10.00',
'fields.amount.max' = '1000.00'
);
-- Create the order details result table.
CREATE TABLE order_detail_sink (
order_id BIGINT,
customer_id BIGINT,
amount DECIMAL(10, 2)
) WITH (
'connector' = 'print',
'print-identifier' = 'order_detail'
);
-- Create the customer aggregation result table.
CREATE TABLE customer_agg_sink (
customer_id BIGINT,
order_count BIGINT,
total_amount DECIMAL(10, 2)
) WITH (
'connector' = 'print',
'print-identifier' = 'customer_agg'
);
-- Submit the job: multi-sink writes, two INSERT statements share the same DAG.
EXECUTE STATEMENT SET
BEGIN
INSERT INTO order_detail_sink
  SELECT order_id, customer_id, amount
FROM order_source;
INSERT INTO customer_agg_sink
  SELECT customer_id, COUNT(*) AS order_count, SUM(amount) AS total_amount
FROM order_source
GROUP BY customer_id;
END;