Help Center/ AI DataLake/ Spark reference/ Exporting Query Results
Updated on 2026-11-06 GMT+08:00

Exporting Query Results

Function

INSERT OVERWRITE DIRECTORY is used to write query results directly to a specified directory. It supports storage in CSV, Parquet, ORC, JSON, and Avro formats.

Syntax

1
2
3
4
INSERT OVERWRITE DIRECTORY path
  USING file_format
  [OPTIONS(key1=value1)]
  select_statement;

Keywords

  • USING: Specifies the storage format.
  • OPTIONS: A list of properties for the export, optional.

Parameters

Table 1 INSERT OVERWRITE DIRECTORY parameter descriptions

Parameter

Description

path

The OBS path to which the query results will be written.

file_format

The file format for writing. Supports CSV, Parquet, ORC, JSON, and Avro formats.

Precautions

  • 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);
  • When the configuration is OPTIONS('DELIMITER'=','), the delimiter can be specified. The default value is ,.

    For CSV data, the following delimiters are supported:

    • Tab character, e.g., 'DELIMITER'='\t'.
    • Specifying a delimiter using a Unicode encoding, e.g., 'DELIMITER'='\u0001'.
    • Single quote (`'`), which must be enclosed in double quotes (" "), e.g., 'DELIMITER'= "".

Example

1
2
3
4
INSERT OVERWRITE DIRECTORY 'obs://bucket/dir'
  USING csv
  OPTIONS(key1=value1)
  select * from db1.tb1;