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.
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
- Linux
- Windows
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.
Run the following in Windows PowerShell, substituting the actual source address and identity. Adjust the executable path if installed elsewhere:
$outputDir = Join-Path $env:TEMP ('taosx-csv-' + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $outputDir | Out-Null
$outputFile = Join-Path $outputDir 'sample.csv'
$outputDsn = 'csv:' + ($outputFile.Replace('\', '/'))
& 'C:\TDengine\taosx.exe' run -f 'taos+ws://<用户>:<编码后的密码>@<源主机>:6041?query=SELECT%201%20AS%20sample_value' -t $outputDsn
if ($LASTEXITCODE -ne 0) { throw 'CSV 导出失败 请保留错误信息' }
Get-Content -LiteralPath $outputFile -Encoding utf8
$rows = @(Import-Csv -LiteralPath $outputFile -Encoding utf8)
if ($rows.Count -ne 1 -or $rows[0].sample_value -ne '1') { throw 'CSV 内容与预期不符' }
Expect header sample_value and one data row 1. Import-Csv parses quotes and delimiters according to CSV rules, making it more reliable than counting text lines; fields can contain line breaks.
3. Parameters and Business Queries
| Item | Meaning and notes |
|---|---|
Source taos+ws:// | Connect to source taosAdapter; example port 6041. |
Source query | SQL to export, URL-encoded as a DSN parameter; not a file path. |
Target csv: path | A file on the host actually running taosx run, not a source-cluster server or browser path. |
| Quotes | Keep quotes around the complete DSN in PowerShell and Bash to prevent shell interpretation of & and other characters. |
| Column names and values | The 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
| Symptom | Next action |
|---|---|
| Command fails or no output file | Check source authentication, query errors, target directory permissions, and free space. |
| Header only | Run the same query on the source and check range, filters, and source data existence. |
| Apparently excessive record count | Read with a CSV parser to avoid counting embedded line breaks as physical records. |
| Old file shrinks after rerun | The 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 incorrectly | Import 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
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.