SELECT INTO OUTFILE
Use SELECT INTO OUTFILE to export the result of a query from a VeloDB Cloud warehouse to object storage or HDFS in Parquet, ORC, or CSV format. The statement runs synchronously and returns when the export completes or fails.
- On success: returns information such as the number of exported files, their size, and their paths.
- On failure: returns an error message.
For guidance on choosing between SELECT INTO OUTFILE and EXPORT, see Export Overview.
For the complete command reference, see SELECT INTO OUTFILE syntax.
Applicable scenarios
SELECT INTO OUTFILE is suitable for the following data export scenarios:
| Scenario type | Description |
|---|---|
| Complex computation export | The exported data must go through complex computation logic, such as filtering, aggregation, or joins (JOIN). |
| Synchronous task | The business workflow must wait for the export to complete before performing subsequent operations. |
Limitations
- Query-plan operators that gather data to one output can limit export parallelism.
- CSV compression depends on the selected writer codec. Core 26.1.3 accepts
plain,gz,bz2,snappyblock,lz4block, andzstdthroughcompress_type. Check compatibility with the receiving reader.
Capability overview
Supported storage locations
| Storage type | Specific support |
|---|---|
| Object storage | Amazon S3, Tencent Cloud COS, Alibaba Cloud OSS, Huawei Cloud OBS, Google GCS |
| Distributed file system | HDFS |
Supported file formats
- Parquet
- ORC
- csv
- csv_with_names
- csv_with_names_and_types
Prerequisites
- Connect to a database in your warehouse with a SQL user that has
SELECTpermission on the source tables. To create the example table, you also need permission to create tables and insert rows. - Use an object-storage bucket or HDFS destination that the warehouse can reach. This applies to both SaaS and BYOC warehouses. Private HDFS networks require access to the NameNode and the advertised DataNode addresses.
- Grant the destination credentials permission to write the exported files. Use HTTPS for object-storage endpoints. Replace
<bucket>,<region>,<access-key>, and<secret-key>with your destination settings. For HDFS, replace the NameNode host, port, and username placeholders with your HDFS settings. Keep credentials out of shared SQL files and query examples. - Use a new output prefix for each run. Replace
<run-id>, where shown, with a unique run identifier. Export consumes warehouse compute and may incur external storage and network charges. After verifying the files, remove unneeded objects with your storage tools.
Quick start
1. Create a table and load data
CREATE TABLE IF NOT EXISTS tbl (
`c1` int(11) NULL,
`c2` string NULL,
`c3` bigint NULL
)
DISTRIBUTED BY HASH(c1) BUCKETS 20;
insert into tbl values
(1, 'doris', 18),
(2, 'nereids', 20),
(3, 'pipelibe', 99999),
(4, 'Apache', 122123455),
(5, null, null);
2. Export to a destination
Choose HDFS or object storage. These are alternative destinations. Replace <namenode-host>, <port>, and <hdfs-user> with your HDFS settings.
Export to HDFS
Export the query result to the hdfs://path/to/ directory in Parquet format:
SELECT c1, c2, c3 FROM tbl
INTO OUTFILE "hdfs://<namenode-host>:<port>/path/to/result_"
FORMAT AS PARQUET
PROPERTIES
(
"fs.defaultFS" = "hdfs://<namenode-host>:<port>",
"hadoop.username" = "<hdfs-user>"
);
Export to object storage
Export the query result to the s3://<bucket>/export/ directory in ORC format. Provide destination credentials:
SELECT * FROM tbl
INTO OUTFILE "s3://<bucket>/export/<run-id>/result_"
FORMAT AS ORC
PROPERTIES(
"s3.endpoint" = "https://s3.<region>.amazonaws.com",
"s3.region" = "<region>",
"s3.secret_key" = "<secret-key>",
"s3.access_key" = "<access-key>"
);
Advanced capabilities
Enable parallel export
Enable parallel export through the session variable enable_parallel_outfile:
SET enable_parallel_outfile=true;
| Dimension | Description |
|---|---|
| Working mechanism | Uses multiple BE nodes and multiple threads to export result data concurrently. |
| Advantage | Can improve throughput when the plan and destination allow parallel writes. |
| Side effect | May produce more files. |
| When it does not apply | Global gather operators, such as a final sort or aggregation, can constrain the number of output writers. |
| How to verify it took effect | If the export command returns more than one row, parallel export has taken effect. |
Export examples
Example 1: Export to an HDFS cluster with high availability enabled
Export to an HDFS cluster with high availability enabled
If HDFS has HA enabled, you must additionally provide configurations related to nameservices and NameNodes:
SELECT c1, c2, c3 FROM tbl
INTO OUTFILE "hdfs://hacluster/path/to/result_"
FORMAT AS PARQUET
PROPERTIES
(
"fs.defaultFS" = "hdfs://hacluster",
"hadoop.username" = "<hdfs-user>",
"dfs.nameservices" = "hacluster",
"dfs.ha.namenodes.hacluster" = "nn1,nn2",
"dfs.namenode.rpc-address.hacluster.nn1" = "<namenode-1-host>:<port>",
"dfs.namenode.rpc-address.hacluster.nn2" = "<namenode-2-host>:<port>",
"dfs.client.failover.proxy.provider.hacluster" = "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider"
);
Example 2: Export to an HDFS cluster with HA and Kerberos authentication enabled
Export to an HDFS cluster with HA and Kerberos authentication enabled
Before using this example, ask VeloDB Cloud support to confirm HDFS connectivity and service-side principal/keytab provisioning. The keytab path refers to a file provisioned for the service, not your client. If HDFS also uses HA, include the nameservice properties:
SELECT * FROM tbl
INTO OUTFILE "hdfs://hacluster/path/to/result_"
FORMAT AS PARQUET
PROPERTIES
(
"fs.defaultFS"="hdfs://hacluster/",
"hadoop.username" = "<hdfs-user>",
"dfs.nameservices"="hacluster",
"dfs.ha.namenodes.hacluster"="n1,n2",
"dfs.namenode.rpc-address.hacluster.n1"="<namenode-1-host>:8020",
"dfs.namenode.rpc-address.hacluster.n2"="<namenode-2-host>:8020",
"dfs.client.failover.proxy.provider.hacluster"="org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider",
"dfs.namenode.kerberos.principal"="hadoop/_HOST@REALM.COM",
"hadoop.security.authentication"="kerberos",
"hadoop.kerberos.principal"="<service-principal>",
"hadoop.kerberos.keytab"="<provisioned-keytab-path>"
);
Example 3: Generate an export-success marker file
Generate an export-success marker file
Background: SELECT INTO OUTFILE is a synchronous command. If the connection is interrupted while the SQL is executing, you cannot tell whether the export completed.
Solution: Use the success_file_name parameter. After a successful export, a marker file (similar to Hive's _SUCCESS) is generated in the directory. Use a fresh prefix and a distinct marker name for each run, so a marker from an earlier run cannot be mistaken for completion. The marker indicates successful completion of that statement. Validate the data separately.
The following example exports the query result to S3 in CSV format and generates a marker file named SUCCESS after completion:
SELECT c1, c2, c3 FROM tbl LIMIT 100000
INTO OUTFILE "s3://<bucket>/export/<run-id>/result_"
FORMAT AS CSV
PROPERTIES
(
"s3.endpoint" = "https://s3.<region>.amazonaws.com",
"s3.region" = "<region>",
"s3.secret_key" = "<secret-key>",
"s3.access_key" = "<access-key>",
"column_separator" = ",",
"line_delimiter" = "\n",
"success_file_name" = "SUCCESS"
);
Example 4: Clear the target directory before export
Clear the target directory before export
Before using delete_existing_files, ask VeloDB Cloud support to confirm whether the managed enable_delete_existing_files setting is enabled for your warehouse. Its source default is false. When it is disabled, use a new prefix instead.
Warning:
This option deletes all files and subdirectories under the parent export directory, including other jobs' files. Use only a dedicated, disposable prefix.
If the setting is enabled, use the following example:
SELECT * FROM tbl
INTO OUTFILE "s3://<bucket>/export/<run-id>/result_"
FORMAT AS CSV
PROPERTIES
(
"s3.endpoint" = "https://s3.<region>.amazonaws.com",
"s3.region" = "<region>",
"s3.secret_key" = "<secret-key>",
"s3.access_key" = "<access-key>",
"column_separator" = ",",
"line_delimiter" = "\n",
"delete_existing_files" = "true"
);
Conditions and risks:
| Item | Description |
|---|---|
| Behavior | First deletes all files and subdirectories under s3://<bucket>/export/, then exports the data. |
| Enable conditions | The service-managed enable_delete_existing_files setting must be enabled. Confirm it with support. |
| Risk warning | This operation deletes data in an external system. It is a high-risk operation, so you must ensure the permissions and data security of the external system on your own. |
Example 5: Control the size of a single exported file
Control the size of a single exported file
Use the max_file_size parameter to control the maximum size of each exported file:
SELECT * FROM tbl
INTO OUTFILE "s3://<bucket>/export/<run-id>/result_"
FORMAT AS ORC
PROPERTIES(
"s3.endpoint" = "https://s3.<region>.amazonaws.com",
"s3.region" = "<region>",
"s3.secret_key" = "<secret-key>",
"s3.access_key" = "<access-key>",
"max_file_size" = "2048MB"
);
Notes:
- This example uses a 2 GiB rollover threshold per writer. Parallel writers can produce multiple files even when the total output is smaller than this value.
- The Core 26.1.3 validator accepts values from
5MB(5 MiB) and defaults to1GB(1 GiB), without the historical 2 GB validation ceiling. - A row is not split between files. Buffered blocks and format row groups can make a file exceed the threshold. Destination storage limits still apply.
Notes
Performance and timeout
| Topic | Description |
|---|---|
| Composition of export time | SELECT INTO OUTFILE is essentially a SQL query, so the total time = query time + result-set write-out time. |
| Single-thread bottleneck | When parallel export is not enabled, the query result is written out by a single BE node in a single thread. |
| Performance optimization | Enable enable_parallel_outfile and measure the result. Throughput depends on the query plan, warehouse capacity, and destination. |
| Export timeout | The timeout of the export command is the same as the query timeout. If the data volume is large, you can set query_timeout to extend it appropriately. |
File management
- VeloDB Cloud does not manage exported files: Whether the export succeeds or files are left behind after a failure, you must clean them up yourself.
- Output prefixes: Use a distinct prefix for each run. Remote path creation and collision behavior depend on the destination. Do not rely on an export to clean up earlier output.
Data and format
- Empty result set: Even if the query result set is empty, an empty file is still produced.
- File splitting rule: One row of data is guaranteed to be stored entirely within a single file, so the file size is not strictly equal to
max_file_size. - Functions that produce non-printable characters: Functions such as
BITMAPandHLLthat output non-printable characters are written as\Nwhen exported to CSV.
Verify the export
Check the returned file count, row count, sizes, and URLs, then list the objects in the destination. For a stable source, compare the exported row count with the query result count and read a sample with the intended consumer. Example values and durations are illustrative.
OUTFILE writes server-side files. It does not download them to your computer or expose service-local disks. Use object storage for downloadable output and your storage provider's tools to retrieve it.
Common errors
| Symptom | What to check |
|---|---|
| Access denied or write failure | Check source SELECT permissions, destination credentials, bucket policy, endpoint, and region. |
| HDFS connection or authentication error | Check NameNode and advertised DataNode reachability. Ask support about managed Kerberos setup. |
| Deleting existing files is not allowed | Use a fresh prefix or confirm the managed gate with support. |
| Connection interrupted or query timed out | Check for partial output. Retry with a fresh prefix and, if appropriate, adjust query_timeout. Use a distinct success marker when the workflow needs one. |