Skip to content

Commit 31a072e

Browse files
Copilotkillme2008
andauthored
docs: add CSV format options for COPY command (#2194)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: killme2008 <14142+killme2008@users.noreply.github.com>
1 parent a106daa commit 31a072e

File tree

2 files changed

+52
-0
lines changed
  • docs/reference/sql
  • i18n/zh/docusaurus-plugin-content-docs/current/reference/sql

2 files changed

+52
-0
lines changed

docs/reference/sql/copy.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ The command starts with the keyword `COPY`, followed by the name of the table yo
2121
`TO` specifies the file path and name to save the exported
2222
data (`/xxx/xxx/output.parquet` in this case).
2323

24+
For example, to export data to CSV with custom timestamp and date formats:
25+
26+
```sql
27+
COPY tbl TO '/path/to/file.csv' WITH (
28+
FORMAT = 'csv',
29+
TIMESTAMP_FORMAT = '%Y/%m/%d %H:%M:%S',
30+
DATE_FORMAT = '%Y-%m-%d'
31+
);
32+
```
33+
2434
#### `WITH` Option
2535

2636
`WITH` adds options such as the file `FORMAT` which specifies the format of the exported file. In this example, the format is Parquet; it is a columnar storage format used for big data processing. Parquet efficiently compresses and encodes columnar data for big data analytics.
@@ -29,6 +39,9 @@ data (`/xxx/xxx/output.parquet` in this case).
2939
|---|---|---|
3040
| `FORMAT` | Target file(s) format, e.g., JSON, CSV, Parquet | **Required** |
3141
| `START_TIME`/`END_TIME`| The time range within which data should be exported. `START_TIME` is inclusive and `END_TIME` is exclusive. | Optional |
42+
| `TIMESTAMP_FORMAT` | Custom format for timestamp columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers (e.g., `'%Y-%m-%d %H:%M:%S'`). Only supported for CSV format. | Optional |
43+
| `DATE_FORMAT` | Custom format for date columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers (e.g., `'%Y-%m-%d'`). Only supported for CSV format. | Optional |
44+
| `TIME_FORMAT` | Custom format for time columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers (e.g., `'%H:%M:%S'`). Only supported for CSV format. | Optional |
3245

3346
#### `CONNECTION` Option
3447

@@ -145,13 +158,26 @@ COPY (<QUERY>) TO '<PATH>' WITH (FORMAT = { 'CSV' | 'JSON' | 'PARQUET' });
145158
| `QUERY` | The SQL SELECT statement to execute | **Required** |
146159
| `PATH` | The file path where the output will be written | **Required** |
147160
| `FORMAT` | The output file format: 'CSV', 'JSON', or 'PARQUET' | **Required** |
161+
| `TIMESTAMP_FORMAT` | Custom format for timestamp columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers. Only supported for CSV format. | Optional |
162+
| `DATE_FORMAT` | Custom format for date columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers. Only supported for CSV format. | Optional |
163+
| `TIME_FORMAT` | Custom format for time columns when exporting to CSV format. Uses [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) format specifiers. Only supported for CSV format. | Optional |
148164

149165
For example, the following statement exports query results to a CSV file:
150166

151167
```sql
152168
COPY (SELECT * FROM tbl WHERE host = 'host1') TO '/path/to/file.csv' WITH (FORMAT = 'csv');
153169
```
154170

171+
You can also specify custom date and time formats when exporting to CSV:
172+
173+
```sql
174+
COPY (SELECT * FROM tbl WHERE host = 'host1') TO '/path/to/file.csv' WITH (
175+
FORMAT = 'csv',
176+
TIMESTAMP_FORMAT = '%m-%d-%Y %H:%M:%S',
177+
DATE_FORMAT = '%Y/%m/%d'
178+
);
179+
```
180+
155181
## COPY DATABASE
156182

157183
Beside copying specific table to/from some path, `COPY` statement can also be used to copy whole database to/from some path. The syntax for copying databases is:

i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/copy.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ COPY tbl TO '/xxx/xxx/output.parquet' WITH (FORMAT = 'parquet');
1717
命令以 `COPY` 关键字开始,后面跟着要导出数据的表名(本例中为 `tbl`)。
1818
`TO` 指定导出数据的文件路径和名称(本例中为 `/xxx/xxx/output.parquet`)。
1919

20+
例如,可以使用自定义时间戳和日期格式导出数据到 CSV 文件:
21+
22+
```sql
23+
COPY tbl TO '/path/to/file.csv' WITH (
24+
FORMAT = 'csv',
25+
TIMESTAMP_FORMAT = '%Y/%m/%d %H:%M:%S',
26+
DATE_FORMAT = '%Y-%m-%d'
27+
);
28+
```
29+
2030
#### `WITH` 选项
2131

2232
`WITH` 可以添加一些选项,比如文件的 `FORMAT` 用来指定导出文件的格式。本例中的格式为 Parquet,它是一种用于大数据处理的列式存储格式。Parquet 为大数据分析高效地压缩和编码列式数据。
@@ -25,6 +35,9 @@ COPY tbl TO '/xxx/xxx/output.parquet' WITH (FORMAT = 'parquet');
2535
|---|---|---|
2636
| `FORMAT` | 目标文件格式,例如 JSON, CSV, Parquet | **** |
2737
| `START_TIME`/`END_TIME`| 需要导出数据的时间范围,时间范围为左闭右开 | 可选 |
38+
| `TIMESTAMP_FORMAT` | 导出 CSV 格式时自定义时间戳列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符(例如 `'%Y-%m-%d %H:%M:%S'`)。仅支持 CSV 格式。 | 可选 |
39+
| `DATE_FORMAT` | 导出 CSV 格式时自定义日期列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符(例如 `'%Y-%m-%d'`)。仅支持 CSV 格式。 | 可选 |
40+
| `TIME_FORMAT` | 导出 CSV 格式时自定义时间列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符(例如 `'%H:%M:%S'`)。仅支持 CSV 格式。 | 可选 |
2841

2942
#### `CONNECTION` 选项
3043

@@ -138,13 +151,26 @@ COPY (<QUERY>) TO '<PATH>' WITH (FORMAT = { 'CSV' | 'JSON' | 'PARQUET' });
138151
| `QUERY` | 要执行的 SQL SELECT 语句 | **** |
139152
| `PATH` | 输出文件的路径 | **** |
140153
| `FORMAT` | 输出文件格式:'CSV'、'JSON' 或 'PARQUET' | **** |
154+
| `TIMESTAMP_FORMAT` | 导出 CSV 格式时自定义时间戳列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符。仅支持 CSV 格式。 | 可选 |
155+
| `DATE_FORMAT` | 导出 CSV 格式时自定义日期列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符。仅支持 CSV 格式。 | 可选 |
156+
| `TIME_FORMAT` | 导出 CSV 格式时自定义时间列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符。仅支持 CSV 格式。 | 可选 |
141157

142158
例如,以下语句将查询结果导出到 CSV 文件中:
143159

144160
```sql
145161
COPY (SELECT * FROM tbl WHERE host = 'host1') TO '/path/to/file.csv' WITH (FORMAT = 'csv');
146162
```
147163

164+
也可以在导出到 CSV 时指定自定义日期和时间格式:
165+
166+
```sql
167+
COPY (SELECT * FROM tbl WHERE host = 'host1') TO '/path/to/file.csv' WITH (
168+
FORMAT = 'csv',
169+
TIMESTAMP_FORMAT = '%m-%d-%Y %H:%M:%S',
170+
DATE_FORMAT = '%Y/%m/%d'
171+
);
172+
```
173+
148174
## COPY DATABASE
149175

150176
`COPY` 语句除可以导入/导出表之外,也可以导入/导出指定的数据库,其语法如下:

0 commit comments

Comments
 (0)