diff --git a/docs/reference/sql/copy.md b/docs/reference/sql/copy.md index 8b833908d..d0cdfa6f6 100644 --- a/docs/reference/sql/copy.md +++ b/docs/reference/sql/copy.md @@ -21,6 +21,16 @@ The command starts with the keyword `COPY`, followed by the name of the table yo `TO` specifies the file path and name to save the exported data (`/xxx/xxx/output.parquet` in this case). +For example, to export data to CSV with custom timestamp and date formats: + +```sql +COPY tbl TO '/path/to/file.csv' WITH ( + FORMAT = 'csv', + TIMESTAMP_FORMAT = '%Y/%m/%d %H:%M:%S', + DATE_FORMAT = '%Y-%m-%d' +); +``` + #### `WITH` Option `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). |---|---|---| | `FORMAT` | Target file(s) format, e.g., JSON, CSV, Parquet | **Required** | | `START_TIME`/`END_TIME`| The time range within which data should be exported. `START_TIME` is inclusive and `END_TIME` is exclusive. | Optional | +| `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 | +| `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 | +| `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 | #### `CONNECTION` Option @@ -145,6 +158,9 @@ COPY () TO '' WITH (FORMAT = { 'CSV' | 'JSON' | 'PARQUET' }); | `QUERY` | The SQL SELECT statement to execute | **Required** | | `PATH` | The file path where the output will be written | **Required** | | `FORMAT` | The output file format: 'CSV', 'JSON', or 'PARQUET' | **Required** | +| `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 | +| `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 | +| `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 | For example, the following statement exports query results to a CSV file: @@ -152,6 +168,16 @@ For example, the following statement exports query results to a CSV file: COPY (SELECT * FROM tbl WHERE host = 'host1') TO '/path/to/file.csv' WITH (FORMAT = 'csv'); ``` +You can also specify custom date and time formats when exporting to CSV: + +```sql +COPY (SELECT * FROM tbl WHERE host = 'host1') TO '/path/to/file.csv' WITH ( + FORMAT = 'csv', + TIMESTAMP_FORMAT = '%m-%d-%Y %H:%M:%S', + DATE_FORMAT = '%Y/%m/%d' +); +``` + ## COPY DATABASE 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: diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/copy.md b/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/copy.md index ab7ffb346..bdae6e7a5 100644 --- a/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/copy.md +++ b/i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/copy.md @@ -17,6 +17,16 @@ COPY tbl TO '/xxx/xxx/output.parquet' WITH (FORMAT = 'parquet'); 命令以 `COPY` 关键字开始,后面跟着要导出数据的表名(本例中为 `tbl`)。 `TO` 指定导出数据的文件路径和名称(本例中为 `/xxx/xxx/output.parquet`)。 +例如,可以使用自定义时间戳和日期格式导出数据到 CSV 文件: + +```sql +COPY tbl TO '/path/to/file.csv' WITH ( + FORMAT = 'csv', + TIMESTAMP_FORMAT = '%Y/%m/%d %H:%M:%S', + DATE_FORMAT = '%Y-%m-%d' +); +``` + #### `WITH` 选项 `WITH` 可以添加一些选项,比如文件的 `FORMAT` 用来指定导出文件的格式。本例中的格式为 Parquet,它是一种用于大数据处理的列式存储格式。Parquet 为大数据分析高效地压缩和编码列式数据。 @@ -25,6 +35,9 @@ COPY tbl TO '/xxx/xxx/output.parquet' WITH (FORMAT = 'parquet'); |---|---|---| | `FORMAT` | 目标文件格式,例如 JSON, CSV, Parquet | **是** | | `START_TIME`/`END_TIME`| 需要导出数据的时间范围,时间范围为左闭右开 | 可选 | +| `TIMESTAMP_FORMAT` | 导出 CSV 格式时自定义时间戳列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符(例如 `'%Y-%m-%d %H:%M:%S'`)。仅支持 CSV 格式。 | 可选 | +| `DATE_FORMAT` | 导出 CSV 格式时自定义日期列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符(例如 `'%Y-%m-%d'`)。仅支持 CSV 格式。 | 可选 | +| `TIME_FORMAT` | 导出 CSV 格式时自定义时间列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符(例如 `'%H:%M:%S'`)。仅支持 CSV 格式。 | 可选 | #### `CONNECTION` 选项 @@ -138,6 +151,9 @@ COPY () TO '' WITH (FORMAT = { 'CSV' | 'JSON' | 'PARQUET' }); | `QUERY` | 要执行的 SQL SELECT 语句 | **是** | | `PATH` | 输出文件的路径 | **是** | | `FORMAT` | 输出文件格式:'CSV'、'JSON' 或 'PARQUET' | **是** | +| `TIMESTAMP_FORMAT` | 导出 CSV 格式时自定义时间戳列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符。仅支持 CSV 格式。 | 可选 | +| `DATE_FORMAT` | 导出 CSV 格式时自定义日期列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符。仅支持 CSV 格式。 | 可选 | +| `TIME_FORMAT` | 导出 CSV 格式时自定义时间列的格式。使用 [strftime](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) 格式说明符。仅支持 CSV 格式。 | 可选 | 例如,以下语句将查询结果导出到 CSV 文件中: @@ -145,6 +161,16 @@ COPY () TO '' WITH (FORMAT = { 'CSV' | 'JSON' | 'PARQUET' }); COPY (SELECT * FROM tbl WHERE host = 'host1') TO '/path/to/file.csv' WITH (FORMAT = 'csv'); ``` +也可以在导出到 CSV 时指定自定义日期和时间格式: + +```sql +COPY (SELECT * FROM tbl WHERE host = 'host1') TO '/path/to/file.csv' WITH ( + FORMAT = 'csv', + TIMESTAMP_FORMAT = '%m-%d-%Y %H:%M:%S', + DATE_FORMAT = '%Y/%m/%d' +); +``` + ## COPY DATABASE `COPY` 语句除可以导入/导出表之外,也可以导入/导出指定的数据库,其语法如下: