El contenido no se encuentra disponible en el idioma seleccionado. Estamos trabajando continuamente para agregar más idiomas. Gracias por su apoyo.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Inserting Data

Updated on 2025-01-22 GMT+08:00

Function

This statement is used to insert the SELECT query result or a certain data record into a table.

Notes and Constraints

  • The INSERT OVERWRITE syntax is not suitable for "read-write" scenarios, where data is continuously processed and updated. Using this syntax in such scenarios may result in data loss.

    "Read-write" refers to the ability to read data while generating new data or modifying existing data during data processing.

  • When using Hive and Datasource tables (excluding Hudi), executing data modification commands (such as insert into and load data) may result in data duplication or inconsistency if the data source does not support transactions and there is a system failure or queue restart.

    To avoid this situation, you are advised to prioritize data sources that support transactions, such as Hudi data sources. This type of data source has Atomicity, Consistency, Isolation, Durability (ACID) capabilities, which helps ensure data consistency and accuracy.

    To learn more, refer to How Do I Handle Duplicate Records After Executing the INSERT INTO Statement?

Syntax

  • Insert the SELECT query result into a table.
    1
    2
    INSERT INTO [TABLE] [db_name.]table_name
      [PARTITION part_spec] select_statement;
    
    1
    2
    INSERT OVERWRITE TABLE [db_name.]table_name
      [PARTITION part_spec] select_statement;
    
    part_spec:  
      : (part_col_name1=val1 [, part_col_name2=val2, ...])
  • Insert a data record into a table.
    1
    2
    INSERT INTO [TABLE] [db_name.]table_name
      [PARTITION part_spec] VALUES values_row [, values_row ...];
    
    1
    2
    INSERT OVERWRITE TABLE [db_name.]table_name
      [PARTITION part_spec] VALUES values_row [, values_row ...];
    
    values_row:
      : (val1 [, val2, ...])

Keywords

Table 1 INSERT keywords

Parameter

Description

db_name

Name of the database where the target table resides.

table_name

Name of the target table.

part_spec

Detailed partition information. If there are multiple partition fields, all fields must be contained, but the corresponding values are optional. The system matches the corresponding partition. A maximum of 100,000 partitions can be created in a single table.

select_statement

SELECT query on the source table.

values_row

Value to be inserted to a table. Use commas (,) to separate columns.

Precautions

  • The target DLI table must exist.
  • If no partition needs to be specified for dynamic partitioning, place part_spec in the SELECT statement as a common field.
  • During creation of the target OBS table, only the folder path can be specified.
  • The source table and the target table must have the same data types and column field quantity. Otherwise, data insertion fails.
  • You are not advised to insert data concurrently into the same table as it may result in abnormal data insertion due to concurrency conflicts.
  • The INSERT INTO statement is used to add the query result to the target table.
  • The INSERT OVERWRITE statement is used to overwrite existing data in the source table.
  • The INSERT INTO statement can be batch executed, but the INSERT OVERWRITE statement can be batch executed only when data of different partitioned tables is inserted to different static partitions.
  • The INSERT INTO and INSERT OVERWRITE statements can be executed at the same time. However, the result is unknown.
  • When you insert data of the source table to the target table, you cannot import or update data of the source table.
  • The dynamic INSERT OVERWRITE statement of Hive partitioned tables can overwrite the involved partition data but cannot overwrite the entire table data.
  • To overwrite data in a specified partition of the datasource table, set dli.sql.dynamicPartitionOverwrite.enabled to true and run the insert overwrite statement. The default value of dli.sql.dynamicPartitionOverwrite.enabled is false, indicating that data in the entire table is overwritten. The following is an example:
    1
    insert overwrite table tb1 partition(part1='v1', part2='v2') select * from ...
    
    NOTE:

    On the DLI management console, click SQL Editor. In the upper right corner of the editing window, click Settings to configure parameters.

  • You can configure the spark.sql.shuffle.partitions parameter to set the number of files to be inserted into the OBS bucket in the table. In addition, to avoid data skew, you can add distribute by rand() to the end of the INSERT statement to increase the number of concurrent jobs. The following is an example:
    insert into table table_target select * from table_source distribute by cast(rand() * N as int);

Example

NOTE:

Before importing data, you must create a table. For details, see Creating an OBS Table.

  • Insert the SELECT query result into a table.
    • Use the DataSource syntax to create a parquet partitioned table.
      CREATE TABLE data_source_tab1 (col1 INT, p1 INT, p2 INT)
        USING PARQUET PARTITIONED BY (p1, p2);
    • Insert the query result to the partition (p1 = 3, p2 = 4).
      INSERT INTO data_source_tab1 PARTITION (p1 = 3, p2 = 4)
        SELECT id FROM RANGE(1, 3);
    • Insert the new query result to the partition (p1 = 3, p2 = 4).
      INSERT OVERWRITE TABLE data_source_tab1 PARTITION (p1 = 3, p2 = 4)
        SELECT id FROM RANGE(3, 5);
  • Insert a data record into a table.
    • Create a Parquet partitioned table with Hive format
      CREATE TABLE hive_serde_tab1 (col1 INT, p1 INT, p2 INT)
        USING HIVE OPTIONS(fileFormat 'PARQUET') PARTITIONED BY (p1, p2);
    • Insert two data records into the partition (p1 = 3, p2 = 4).
      INSERT INTO hive_serde_tab1 PARTITION (p1 = 3, p2 = 4)
        VALUES (1), (2);
    • Insert new data to the partition (p1 = 3, p2 = 4).
      INSERT OVERWRITE TABLE hive_serde_tab1 PARTITION (p1 = 3, p2 = 4)
        VALUES (3), (4);

How Do I Handle Duplicate Records After Executing the INSERT INTO Statement?

  • Symptom

    When using Hive and Datasource tables (excluding Hudi), executing data modification commands (such as insert into and load data) may result in data duplication or inconsistency if the data source does not support transactions and there is a system failure or queue restart.

  • Possible causes

    If queue resources are restarted in the data commit phase, data may have been restored to a formal directory. If an insert into statement is executed and a retry is triggered after a resource restart, there is a possibility that data will be repeatedly written.

  • Solution
    1. Hudi data sources that support ACID properties are recommended.
    2. Use idempotent syntax such as insert overwrite instead of non-idempotent syntax such as insert into to insert data.
    3. If it is strictly required that data cannot be duplicated, you are advised to perform deduplication on the table data after executing the insert into statement to prevent duplicate data.

Utilizamos cookies para mejorar nuestro sitio y tu experiencia. Al continuar navegando en nuestro sitio, tú aceptas nuestra política de cookies. Descubre más

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback