Skip to main content

Export CSV

CSV export uses the installation package's taosx run command to write one SQL query's results to a CSV text file with column names on the command-line host. Check existing files before rerunning; appending is not a resume mechanism.

Where to operate: run taosx run on a Windows or Linux command-line host that can access the source cluster. Queries connect through taosAdapter. The current execution account creates the CSV on the command execution host.

warning

The first CSV row contains column names, followed by data. It does not preserve complete database structures or recovery state and does not continuously track new writes. CSV output differs from Parquet: a same-named target file is directly created or truncated. There is no overwrite protection, temporary completion file, or append guarantee.

1. Prepare a Read-Only Query and New Path​

First run the planned SELECT in the taosExplorer Data Browser or taos CLI and verify column names, time range, time zone, and expected values. Use only reviewed read-only queries. Do not put arbitrary externally obtained SQL directly into an export command.

Initially use SELECT 1 AS sample_value to verify the entry point without business data. Prepare a new controlled directory with write permissions and sufficient space. Give query columns unique names where possible so downstream CSV readers do not mishandle duplicate names.

2. Run a Minimal Export​

Run in Linux Bash:

output_dir=$(mktemp -d "${TMPDIR:-/tmp}/taosx-csv.XXXXXX") || exit 1
output_file="$output_dir/sample.csv"
taosx run -f 'taos+ws://<用户>:<编码后的密码>@<源主机>:6041?query=SELECT%201%20AS%20sample_value' -t "csv:$output_file"
export_status=$?
printf 'exit=%s output=%s\n' "$export_status" "$output_file"
if [ "$export_status" -ne 0 ]; then exit "$export_status"; fi
python3 - "$output_file" <<'PY'
import csv
import sys

with open(sys.argv[1], encoding="utf-8", newline="") as input_file:
reader = csv.DictReader(input_file)
assert reader.fieldnames == ["sample_value"], reader.fieldnames
rows = list(reader)
assert rows == [{"sample_value": "1"}], rows
print(rows)
PY

Python only reads the generated file. It does not connect to the database or modify tasks. Expect one record with sample_value=1. If Python is unavailable, perform equivalent checks with an existing standard CSV tool.

3. Parameters and Business Queries​

ItemMeaning and notes
Source taos+ws://Connect to source taosAdapter; example port 6041.
Source querySQL to export, URL-encoded as a DSN parameter; not a file path.
Target csv: pathA file on the host actually running taosx run, not a source-cluster server or browser path.
QuotesKeep quotes around the complete DSN in PowerShell and Bash to prevent shell interpretation of & and other characters.
Column names and valuesThe file has a header and converts all values to text. It does not include complete schema definitions such as database types or tag categories.

For business queries, define columns, objects, and a half-open time range inclusive at the start and exclusive at the end. Check in the taosExplorer Data Browser or taos CLI before encoding SQL. When exporting a supertable and device or child-table identity is needed, explicitly project the identifier and required tags. CSV does not automatically add all context.

4. Verification​

Compare CSV record counts, primary timestamps, values, and device identifiers within the same stable window. Use a CSV parser to check commas, quotes, newlines, and Chinese text. File size does not replace content verification. Validate text representations of NULL, binary data, times, and special values using actual query samples first. Do not assume export and reimport are necessarily lossless.

5. Troubleshooting​

SymptomNext action
Command fails or no output fileCheck source authentication, query errors, target directory permissions, and free space.
Header onlyRun the same query on the source and check range, filters, and source data existence.
Apparently excessive record countRead with a CSV parser to avoid counting embedded line breaks as physical records.
Old file shrinks after rerunThe same path may have been truncated. Preserve the scene first. Original recovery requires an independent backup; rerunning cannot restore it.
Excel displays times or identifiers incorrectlyImport as text with explicit types to avoid automatic conversion of long integers or leading zeros.

For data containing untrusted strings, also guard against spreadsheets interpreting content as formulas. Do not open or hand over unreviewed files with formulas enabled.

For other task, write, and runtime issues, see Troubleshooting by Symptom.

6. Stop and Rerun​

warning

Stop foreground execution with Ctrl+C. Generated files may contain only partial results and must not be delivered. Retain originals and errors, export again from the beginning to a new directory or filename, then compare content again. Do not append to existing results or concatenate two outputs as a resumed export.

7. Limitations​

The minimal constant query checks only the header and single-row value. It does not establish lossless round trips for all business types or a corresponding GUI CSV publishing entry point. Verify large result sets, complex types, and downstream processing separately with actual queries.