2022-02-26 07:43:24 +00:00
|
|
|
---
|
2022-02-26 11:51:04 +00:00
|
|
|
title: 如何从 ClickHouse 导出数据到一个文件?
|
2022-02-26 07:43:24 +00:00
|
|
|
toc_hidden: true
|
2022-04-10 23:08:18 +00:00
|
|
|
sidebar_position: 10
|
2022-02-26 07:43:24 +00:00
|
|
|
---
|
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
# 如何从 ClickHouse 导出数据到一个文件? {#how-to-export-to-file}
|
2022-02-26 07:43:24 +00:00
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
## 使用 INTO OUTFILE 语法 {#using-into-outfile-clause}
|
2022-02-26 07:43:24 +00:00
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
加一个 [INTO OUTFILE](../../sql-reference/statements/select/into-outfile.md#into-outfile-clause) 语法到你的查询语句中.
|
2022-02-26 07:43:24 +00:00
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
例如:
|
2022-02-26 07:43:24 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT * FROM table INTO OUTFILE 'file'
|
|
|
|
```
|
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
ClickHouse 默认使用[TabSeparated](../../interfaces/formats.md#tabseparated) 格式写入数据. 修改[数据格式](../../interfaces/formats.md), 请用 [FORMAT 语法](../../sql-reference/statements/select/format.md#format-clause).
|
2022-02-26 07:43:24 +00:00
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
例如:
|
2022-02-26 07:43:24 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT * FROM table INTO OUTFILE 'file' FORMAT CSV
|
|
|
|
```
|
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
## 使用一个文件引擎表 {#using-a-file-engine-table}
|
2022-02-26 07:43:24 +00:00
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
查看 [File](../../engines/table-engines/special/file.md) 表引擎.
|
2022-02-26 07:43:24 +00:00
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
## 使用命令行重定向 {#using-command-line-redirection}
|
2022-02-26 07:43:24 +00:00
|
|
|
|
|
|
|
``` bash
|
|
|
|
$ clickhouse-client --query "SELECT * from table" --format FormatName > result.txt
|
|
|
|
```
|
|
|
|
|
2022-02-26 11:51:04 +00:00
|
|
|
查看 [clickhouse-client](../../interfaces/cli.md).
|