Insert Data
Function
Inserts the result of a SELECT query or specific data values into a table.
Notes and Constraints
- The INSERT OVERWRITE syntax is not applicable for self-read-write scenarios on the same table (including both partitioned and non-partitioned tables). Executing INSERT OVERWRITE directly on the original table may lead to data loss or data inconsistency.
To perform data operations in a self-read-write scenario, you are advised to use a temporary table to process the data.
Self-read-write means the destination table and the source data table are the same table. For example, the following statement extracts student information where class_no = 1 from the student table and overwrites the original table. This is a typical example of a self-read-write scenario.
INSERT OVERWRITE TABLE student SELECT name FROM student WHERE class_no = 1;
- When using Hive and Datasource (excluding Hudi) tables to execute data modification commands (e.g., INSERT INTO, LOAD DATA), because the data source does not support transactions, system failures or endpoint resource restarts may lead to data duplication or data inconsistency.
To avoid this, you are advised to prioritize data sources that support transactions, such as the Hudi data source. These data sources have atomicity, consistency, isolation, durability (ACID) capabilities, which help ensure data consistency and accuracy.
Syntax
- Insert the result of a SELECT query into a table:
INSERT INTO [TABLE] [db_name.]table_name [PARTITION part_spec] select_statement;
INSERT OVERWRITE TABLE [db_name.]table_name [PARTITION part_spec] select_statement;
part_spec: : (part_col_name1=val1 [, part_col_name2=val2, ...]) - Insert specific data values into a table:
INSERT INTO [TABLE] [db_name.]table_name [PARTITION part_spec] VALUES values_row [, values_row ...];
INSERT OVERWRITE TABLE [db_name.]table_name [PARTITION part_spec] VALUES values_row [, values_row ...];
values_row: : (val1 [, val2, ...])
Keywords
| Parameter | Description |
|---|---|
| db_name | The name of the database containing the table on which the INSERT command is to be executed. |
| table_name | The name of the table on which the INSERT command is to be executed. |
| part_spec | Specifies detailed partition information. If the partition field has multiple fields, all fields must be included, but the corresponding values may be omitted. The system will match the corresponding partition. The maximum number of partitions per table is 100,000. |
| select_statement | A SELECT query on the source table (supports managed tables and OBS tables). |
| values_row | The values to be inserted into the table, with columns separated by commas. |
Precautions
- The table must already exist.
- If dynamic partitioning does not require specifying a partition, place part_spec as a regular field in the SELECT statement.
- The OBS table being inserted into must specify a folder path during table creation.
- The data types and number of columns in the source and target tables should match. Otherwise, the insertion will fail.
- Concurrent data insertion into the same table is not recommended, as concurrency conflicts may lead to abnormal insertion results.
- The INSERT INTO command appends the query results to the target table.
- The INSERT OVERWRITE command overwrites existing data in the target table.
- INSERT INTO commands can be executed in parallel. INSERT OVERWRITE commands can only be executed in parallel on partitioned tables when inserting into different static partitions.
- The result of executing INSERT INTO and INSERT OVERWRITE commands simultaneously is unknown.
- During the process of inserting data from a source table into a target table, data cannot be imported or updated in the source table.
- For dynamic INSERT OVERWRITE on Hive partitioned tables, only the data in the affected partitions is overwritten. Overwriting the entire table is not supported.
- To overwrite data in a specified partition of a Datasource table, the parameter spark.sql.dynamicPartitionOverwrite.enabled must be set to true first, and then the INSERT OVERWRITE statement is used. The default value of spark.sql.dynamicPartitionOverwrite.enabled is false, meaning the entire table is overwritten. For example:
insert overwrite table tb1 partition(part1='v1', part2='v2') select * from ...
- By configuring the spark.sql.shuffle.partitions parameter, the number of files inserted into an OBS bucket for non-managed tables can be set. Additionally, to avoid data skew, distribute by rand() can be added after the INSERT statement to increase the concurrency of the processing job. For example:
insert into table table_target select * from table_source distribute by cast(rand() * N as int);
Examples
Before importing data, refer to the examples in "Creating an OBS Table" or "Creating a Managed Table" to create the corresponding tables.
- Example 1: Insert the result of a SELECT query into a table.
- Create a partitioned table in Parquet format using Datasource syntax:
CREATE TABLE data_source_tab1 (col1 INT, p1 INT, p2 INT) USING PARQUET PARTITIONED BY (p1, p2);
- Insert query results into partition (p1 = 3, p2 = 4):
INSERT INTO data_source_tab1 PARTITION (p1 = 3, p2 = 4) SELECT id FROM RANGE(1, 3);
- Insert new query results into partition (p1 = 3, p2 = 4):
INSERT OVERWRITE TABLE data_source_tab1 PARTITION (p1 = 3, p2 = 4) SELECT id FROM RANGE(3, 5);
- Create a partitioned table in Parquet format using Datasource syntax:
- Example 2: Insert specific data values into a table.
- Create a partitioned table in Parquet format using Hive syntax:
CREATE TABLE hive_serde_tab1 (col1 INT, p1 INT, p2 INT) USING HIVE OPTIONS(fileFormat 'PARQUET') PARTITIONED BY (p1, p2);
- Insert two data rows into partition (p1 = 3, p2 = 4):
INSERT INTO hive_serde_tab1 PARTITION (p1 = 3, p2 = 4) VALUES (1), (2);
- Insert new data into partition (p1 = 3, p2 = 4):
INSERT OVERWRITE TABLE hive_serde_tab1 PARTITION (p1 = 3, p2 = 4) VALUES (3), (4);
- Create a partitioned table in Parquet format using Hive syntax:
How Do I Handle Duplicate Records After Executing INSERT INTO?
- Symptom
When using Hive and Datasource (excluding Hudi) tables to execute data modification commands (e.g., INSERT INTO, LOAD DATA), because the data source does not support transactions, system failures or endpoint resource restarts may lead to data duplication or data inconsistency.
- Cause Analysis
If an endpoint resource restart occurs during the commit phase of data writing, the data may have already been written to the final directory. If an INSERT INTO statement is being executed, a retry triggered after the resource restart may result in duplicate data writes.
- Solution
- Hudi data sources that support ACID properties are recommended.
- You are advised to use idempotent syntax like INSERT OVERWRITE instead of non-idempotent syntax like INSERT INTO for data insertion whenever possible.
- If strict requirements prevent data duplication, you are advised to perform a deduplication operation on the table data after executing INSERT INTO to prevent duplicate data.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot