Flink DML
Cloudera SQL Stream Builder (SSB) supports the Flink SQL insert statement and statement sets, which write the results of continuous queries to sink tables.
Data Manipulation Language (DML) statements write data to tables. In SSB, a DML statement writes the results of a query to a sink table, and keeps writing them for as long as the job runs.
| Statement | Description | Usage notes |
|---|---|---|
INSERT INTO |
Writes the results of a query to a table. | You can list the target columns. Add an ON CONFLICT clause when
the upsert key of the query differs from the primary key of the sink table. |
INSERT OVERWRITE |
Replaces the data in a table. | Cloudera SQL Stream Builder does not support INSERT OVERWRITE in streaming jobs. |
EXECUTE STATEMENT SET |
Runs several INSERT INTO statements as one job. |
Use a statement set to write the same data to several sinks without reading the source more than once. |
Writing query results to a table
The following statement writes enriched orders to the product_orders
table:
INSERT INTO product_orders
SELECT p.name, o.order_id
FROM orders o
JOIN products p ON o.product_name = p.name;
Resolving primary key conflicts
When a query creates updates, such as a join or an aggregation, its upsert key can differ
from the primary key of the sink table. Records with different upsert keys can then map to
the same primary key. In that case, Flink stops the job from starting unless you choose a
conflict strategy with the ON CONFLICT clause, which Flink
2.3 supports. The
table.exec.sink.require-on-conflict option, which is
true by default, controls whether Flink requires the clause.
| Strategy | What it does |
|---|---|
DO ERROR |
Fails the job at run time if records with different upsert keys map to the same primary key. Use it when you know that the keys match, even though Flink cannot prove it. |
DO NOTHING |
Keeps the first record for a primary key and discards later conflicting records. |
DO DEDUPLICATE |
Keeps the full history of changes for each primary key, so that Flink can roll back retracted records correctly. |
INSERT INTO product_orders
SELECT p.name, o.order_id
FROM orders o
JOIN products p ON o.product_name = p.name
ON CONFLICT DO ERROR;
Writing to several tables in one job
EXECUTE STATEMENT SET
BEGIN
INSERT INTO orders_archive
SELECT * FROM orders;
INSERT INTO large_orders
SELECT * FROM orders WHERE price > 1000;
END;
For more information, see INSERT statement in the Apache Flink documentation.
