跳到主要内容
跳到主要内容

输入和输出数据的格式

输入和输出数据的格式

ClickHouse 支持大多数已知的文本和二进制数据格式。这使得几乎可以轻松集成到任何工作数据管道中,以利用 ClickHouse 的优势。

输入格式

输入格式用于:

  • 解析提供给 INSERT 语句的数据
  • 从文件支持的表(如 FileURLHDFS)中执行 SELECT 查询
  • 读取字典

选择合适的输入格式对于在 ClickHouse 中高效地进行数据摄取至关重要。支持70多种格式,选择性能最优的选项可以显著影响插入速度、CPU和内存使用,以及整体系统效率。为了帮助导航这些选择,我们对各种格式的摄取性能进行了基准测试,得出了以下主要结论:

  • Native 格式是最高效的输入格式,提供最佳的压缩率、最低的资源使用和最小的服务器端处理开销。
  • 压缩至关重要 - LZ4 以最低的 CPU 成本减少数据大小,而 ZSTD 提供更高的压缩率,但会增加 CPU 使用。
  • 预排序影响适中,因为 ClickHouse 已经高效地进行了排序。
  • 批量处理显著提高效率 - 较大的批次减少插入开销并提高吞吐量。

要深入了解结果和最佳实践,请阅读完整的 基准分析。要查看完整的测试结果,请探索 FastFormats 在线仪表板。

输出格式

支持的输出格式用于:

  • 排列 SELECT 查询的结果
  • 在文件支持的表中执行 INSERT 操作

格式概述

支持的格式有:

格式输入输出
TabSeparated
TabSeparatedRaw
TabSeparatedWithNames
TabSeparatedWithNamesAndTypes
TabSeparatedRawWithNames
TabSeparatedRawWithNamesAndTypes
Template
TemplateIgnoreSpaces
CSV
CSVWithNames
CSVWithNamesAndTypes
CustomSeparated
CustomSeparatedWithNames
CustomSeparatedWithNamesAndTypes
SQLInsert
Values
Vertical
JSON
JSONAsString
JSONAsObject
JSONStrings
JSONColumns
JSONColumnsWithMetadata
JSONCompact
JSONCompactStrings
JSONCompactColumns
JSONEachRow
PrettyJSONEachRow
JSONEachRowWithProgress
JSONStringsEachRow
JSONStringsEachRowWithProgress
JSONCompactEachRow
JSONCompactEachRowWithNames
JSONCompactEachRowWithNamesAndTypes
JSONCompactEachRowWithProgress
JSONCompactStringsEachRow
JSONCompactStringsEachRowWithNames
JSONCompactStringsEachRowWithNamesAndTypes
JSONCompactStringsEachRowWithProgress
JSONObjectEachRow
BSONEachRow
TSKV
Pretty
PrettyNoEscapes
PrettyMonoBlock
PrettyNoEscapesMonoBlock
PrettyCompact
PrettyCompactNoEscapes
PrettyCompactMonoBlock
PrettyCompactNoEscapesMonoBlock
PrettySpace
PrettySpaceNoEscapes
PrettySpaceMonoBlock
PrettySpaceNoEscapesMonoBlock
Prometheus
Protobuf
ProtobufSingle
ProtobufList
Avro
AvroConfluent
Parquet
ParquetMetadata
Arrow
ArrowStream
ORC
One
Npy
RowBinary
RowBinaryWithNames
RowBinaryWithNamesAndTypes
RowBinaryWithDefaults
Native
Null
XML
CapnProto
LineAsString
Regexp
RawBLOB
MsgPack
MySQLDump
DWARF
Markdown
Form

您可以通过 ClickHouse 设置控制某些格式处理参数。有关更多信息,请阅读 设置 部分。

TabSeparated

请参见 TabSeparated

TabSeparatedRaw

请参见 TabSeparatedRaw

TabSeparatedWithNames

请参见 TabSeparatedWithNames

TabSeparatedWithNamesAndTypes

请参见 TabSeparatedWithNamesAndTypes

TabSeparatedRawWithNames

请参见 TabSeparatedRawWithNames

TabSeparatedRawWithNamesAndTypes

请参见 TabSeparatedRawWithNamesAndTypes

Template

请参见 Template

TemplateIgnoreSpaces

请参见 TemplateIgnoreSpaces

TSKV

请参见 TSKV

CSV

请参见 CSV

CSVWithNames

请参见 CSVWithNames

CSVWithNamesAndTypes

请参见 CSVWithNamesAndTypes

CustomSeparated

请参见 CustomSeparated

CustomSeparatedWithNames

请参见 CustomSeparatedWithNames

CustomSeparatedWithNamesAndTypes

请参见 CustomSeparatedWithNamesAndTypes

SQLInsert

请参见 SQLInsert

JSON

请参见 JSON

JSONStrings

请参见 JSONStrings

JSONColumns

请参见 JSONColumns

JSONColumnsWithMetadata

请参见 JSONColumnsWithMetadata

JSONAsString

请参见 JSONAsString

JSONAsObject

请参见 JSONAsObject

JSONCompact

请参见 JSONCompact

JSONCompactStrings

请参见 JSONCompactStrings

JSONCompactColumns

请参见 JSONCompactColumns

JSONEachRow

请参见 JSONEachRow

PrettyJSONEachRow

请参见 PrettyJSONEachRow

JSONStringsEachRow

请参见 JSONStringsEachRow

JSONCompactEachRow

请参见 JSONCompactEachRow

JSONCompactStringsEachRow

请参见 JSONCompactStringsEachRow

JSONEachRowWithProgress

请参见 JSONEachRowWithProgress

JSONStringsEachRowWithProgress

请参见 JSONStringsEachRowWithProgress

JSONCompactEachRowWithNames

请参见 JSONCompactEachRowWithNames

JSONCompactEachRowWithNamesAndTypes

请参见 JSONCompactEachRowWithNamesAndTypes

JSONCompactEachRowWithProgress

类似于 JSONEachRowWithProgress,但以紧凑形式输出 row 事件,就像 JSONCompactEachRow 格式中一样。

JSONCompactStringsEachRowWithNames

请参见 JSONCompactStringsEachRowWithNames

JSONCompactStringsEachRowWithNamesAndTypes

请参见 JSONCompactStringsEachRowWithNamesAndTypes

JSONObjectEachRow

请参见 JSONObjectEachRow

JSON 格式设置

请参见 JSON 格式设置

BSONEachRow

请参见 BSONEachRow

Native

请参见 Native

Null

请参见 Null

Pretty

请参见 Pretty

PrettyNoEscapes

请参见 PrettyNoEscapes

PrettyMonoBlock

请参见 PrettyMonoBlock

PrettyNoEscapesMonoBlock

请参见 PrettyNoEscapesMonoBlock

PrettyCompact

请参见 PrettyCompact

PrettyCompactNoEscapes

请参见 PrettyCompactNoEscapes

PrettyCompactMonoBlock

请参见 PrettyCompactMonoBlock

PrettyCompactNoEscapesMonoBlock

请参见 PrettyCompactNoEscapesMonoBlock

PrettySpace

请参见 PrettySpace

PrettySpaceNoEscapes

请参见 PrettySpaceNoEscapes

PrettySpaceMonoBlock

请参见 PrettySpaceMonoBlock

PrettySpaceNoEscapesMonoBlock

请参见 PrettySpaceNoEscapesMonoBlock

RowBinary

请参见 RowBinary

RowBinaryWithNames

请参见 RowBinaryWithNames

RowBinaryWithNamesAndTypes

请参见 RowBinaryWithNamesAndTypes

RowBinaryWithDefaults

请参见 RowBinaryWithDefaults

Values

请参见 Values

Vertical

请参见 Vertical

XML

请参见 XML

CapnProto

请参见 CapnProto

Prometheus

请参见 Prometheus

Protobuf

请参见 Protobuf

ProtobufSingle

请参见 ProtobufSingle

ProtobufList

请参见 ProtobufList

Avro

请参见 Avro

AvroConfluent

请参见 AvroConfluent

Parquet

请参见 Parquet

ParquetMetadata

请参见 ParquetMetadata

Arrow

请参见 Arrow

ArrowStream

请参见 ArrowStream

ORC

请参见 ORC

One

请参见 One

Npy

请参见 Npy

LineAsString

请参见:

Regexp

请参见 Regexp

RawBLOB

请参见 RawBLOB

Markdown

请参见 Markdown

MsgPack

请参见 MsgPack

MySQLDump

请参见 MySQLDump

DWARF

请参见 Dwarf

Form

请参见 Form

格式模式

包含格式模式的文件名由设置 format_schema 设置。 在使用 Cap'n ProtoProtobuf 格式时必须设置此设置。 格式模式是文件名与该文件中的消息类型名称的组合,由冒号分隔,例如 schemafile.proto:MessageType。 如果文件具有该格式的标准扩展名(例如,.proto 用于 Protobuf), 则可以省略该扩展名,在这种情况下,格式模式看起来像 schemafile:MessageType

如果您通过 客户端 以交互模式输入或输出数据,则格式模式中指定的文件名 可以包含绝对路径或相对于客户端当前目录的路径。 如果您在 批处理模式 中使用客户端,由于安全原因,模式的路径必须是相对的。

如果您通过 HTTP 接口 输入或输出数据,则格式模式中指定的文件名 应位于服务器配置中指定的 format_schema_path 目录中。

跳过错误

某些格式,如 CSVTabSeparatedTSKVJSONEachRowTemplateCustomSeparatedProtobuf 可以在发生解析错误时跳过损坏的行并继续从下一行的开头进行解析。请参见 input_format_allow_errors_numinput_format_allow_errors_ratio 设置。 限制:

  • 在解析错误的情况下,JSONEachRow 跳过所有数据,直到新行(或EOF),因此行必须由 \n 分隔以正确计算错误。
  • TemplateCustomSeparated 在最后一列之后和行之间使用分隔符来查找下一行的开头,因此只有其中至少一个不为空时才跳过错误。