This topic describes how to write, update, and delete data in a Fluss table.
Write data
-
Primary key tables can process all types of messages, such as INSERT, UPDATE, and DELETE.
-
Log tables accept only INSERT messages. You cannot update or delete records.
-
Log in to the Real-time Compute for Apache Flink console.
-
In the Actions column of the desired Streaming Compute Flink workspace, click Console.
-
In the navigation pane on the left, choose .
Primary table creation
CREATE TEMPORARY TABLE source
(
shop_id BIGINT,
user_id BIGINT,
num_orders INT,
total_amount INT,
PRIMARY KEY (shop_id, user_id) NOT ENFORCED
)
WITH ('connector' = 'datagen');
# Write data to specific columns.
INSERT INTO `fluss-catalog`.`my_db`.`my_pk_tbl` (shop_id, user_id, num_orders)
SELECT shop_id, user_id, num_orders FROM source;
# Write data to all columns.
INSERT INTO `fluss-catalog`.`my_db`.`my_pk_tbl`
SELECT * FROM source;
Log table
CREATE TEMPORARY TABLE source (
order_id BIGINT,
item_id BIGINT,
amount INT,
address STRING
) WITH ('connector' = 'datagen');
INSERT INTO `fluss-catalog`.`my_db`.`my_log_tbl`
SELECT * FROM source;
Write load balancing
In the preceding example, written data is routed to operators by using the default distribution mode. If the number of buckets of the table cannot be evenly divided by the job parallelism, the default distribution may leave some operators idle or cause unbalanced write load. After you set sink.distribution-mode to BUCKET_BALANCE in the WITH clause of the CREATE TABLE statement of the destination table, this mode helps improve the sink operator utilization and the load distribution. The actual effect depends on the distribution of bucket keys. When the number of buckets and the sink parallelism remain unchanged, records with the same bucket key are always routed to the same sink operator. The write order within a bucket is not guaranteed.
CREATE TABLE `fluss-catalog`.`my_db`.`my_pk_tbl` (
shop_id BIGINT,
user_id BIGINT,
num_orders INT,
total_amount INT,
PRIMARY KEY (shop_id, user_id) NOT ENFORCED
) WITH (
'sink.distribution-mode' = 'BUCKET_BALANCE'
);
INSERT INTO `fluss-catalog`.`my_db`.`my_pk_tbl`
SELECT * FROM source;
-
This policy applies when the number of buckets cannot be evenly divided by the parallelism, or when some operators are idle. If the number of buckets already matches the parallelism, the benefit is limited.
-
Records with the same bucket key are always routed to the same operator. Therefore, this policy cannot eliminate the write skew caused by hot bucket keys, and it does not guarantee that every operator receives data.
The default value of sink.distribution-mode is AUTO, in which the system selects a distribution mode based on the table type. The following table describes the behavior and the limits of each value.
|
Distribution mode |
Behavior |
Limits |
|
|
Automatically selects a mode based on the table type: BUCKET for primary key tables and for log tables with |
— |
|
|
Uses the default distribution strategy of Flink without additional shuffle. |
— |
|
|
Shuffles data by bucket ID before writing, so that the data of the same bucket is processed by the same sink operator. This improves client processing efficiency and reduces resource consumption. This mode is recommended for primary key tables. |
Takes effect for log tables only when |
|
|
Allows different bucket keys of the same physical bucket to be assigned to different sink operators for concurrent writes, which improves the sink operator utilization and the load distribution. |
|
|
|
Dynamically adjusts the distribution strategy based on the traffic of partition keys to balance the load across partitions. Use this mode when data is highly skewed across partitions or when the number of partitions is large. |
Only partitioned log tables are supported. Primary key tables are not supported. This mode incurs overhead for data statistics collection and additional shuffle operations. |
Update data
-
You can update data only in primary key tables.
-
You can update only a single record at a time based on an equivalent condition for the primary key.
-
In the navigation pane on the left, choose .
-
Write and run the SQL statement.
-- Updates are supported only based on an equivalent condition for the primary key. UPDATE `my-catalog`.`my_db`.`my_pk_tbl` SET total_amount = 2 WHERE shop_id = 10000 and user_id = 123456;
Delete data
-
You can delete data only from primary key tables.
-
You can delete only a single record at a time based on an equivalent condition for the primary key.
-
In the navigation pane on the left, choose .
-
Write and run the SQL statement.
-- Deletions are supported only based on an equivalent condition for the primary key. DELETE FROM `my-catalog`.`my_db`.`my_pk_tbl` WHERE shop_id = 10000 and user_id = 123456;