NOTE Please note that unloading data with INSERT INTO FILES does not support directly exporting data into local file systems. However, you can export the data into local files by using NFS. See Unload to local files using NFS.
Preparation
The following example creates a databaseunload and a table sales_records as the data objects that can be used in the following tutorial. You can use your own data instead.
sales_records contains the transaction ID record_id, salesperson seller, store ID store_id, time sales_time, and sales amount sales_amt for each transaction. It is partitioned on a daily basis in accordance with sales_time.
You also need to prepare a remote storage system with write access. The following examples export data to the following remote storages:
- An HDFS cluster with the simple authentication method enabled.
- An AWS S3 bucket using the IAM User credential.
Unload data
INSERT INTO FILES supports unloading data into a single file or multiple files. You can further partition these data files by specifying separate storage paths for them. When unloading data using INSERT INTO FILES, you must manually set the compression algorithm using the propertycompression. For more information on the data compression algorithm supported by FILES, see unload_data_param.
Unload data into multiple files
By default, INSERT INTO FILES unloads data into multiple data files, each with a size of 1 GB. You can configure the file size using the propertytarget_max_file_size (Unit: Bytes).
The following example unloads all data rows in sales_records as multiple Parquet files prefixed by data1. The size of each file is 1 KB.
Note
Here, setting target_max_file_size to 1 KB is to demonstrate unloading into multiple files with a small dataset. In production environment, you are strongly advised to set this value within the range of hundreds of MB to multiple GB.
- To S3:
- To HDFS:
Unload data into multiple files under different paths
You can also partition the data files in different storage paths by extracting the values of the specified column using the propertypartition_by.
The following example unloads all data rows in sales_records as multiple Parquet files under the path /unload/partitioned/. These files are stored in different subpaths distinguished by the values in the column sales_time.
- To S3:
- To HDFS:
Unload data into a single file
To unload data into a single data file, you must specify the propertysingle as true.
The following example unloads all data rows in sales_records as a single Parquet file prefixed by data2.
- To S3:
- To HDFS:
Unload to MinIO
The parameters used for MinIO are different from those used for AWS S3. Example:Unload to local files using NFS
To access the files in NFS via thefile:// protocol, you need to mount a NAS device as NFS under the same directory of each BE or CN node.
Example:
csv.enclose is set, every non-NULL field value is wrapped with the enclose character,
and occurrences of the enclose character (and the escape character itself, when different)
inside field values are escaped with the csv.escape character. NULL values are output as
\N without enclosing.
If you need to re-import RFC 4180-style doubled-quote output (where csv.escape equals
csv.enclose) back into the database, set only csv.enclose on the read side and do NOT set
csv.escape. The database’ CSV reader natively handles doubled quotes via its ENCLOSE state.
Also note that NULL values round-trip cleanly only when csv.escape is not
backslash (\). The NULL marker \N is fixed, and when csv.escape = '\\' the reader’s
ESCAPE state strips the leading backslash and reads \N as the literal character N
instead of NULL. Use csv.escape = '"' (RFC 4180 doubling) for datasets that need
NULL roundtrip.
See also
- For more instructions on the usage of INSERT, see SQL reference - INSERT.
- For more instructions on the usage of FILES(), see SQL reference - FILES().