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

Avro

输入输出别名

描述

Apache Avro 是一个行导向的数据序列化框架,在 Apache 的 Hadoop 项目中开发。 ClickHouse 的 Avro 格式支持读取和写入 Avro 数据文件

数据类型匹配

The table below shows all data types supported by the Apache Avro format, and their corresponding ClickHouse data types in INSERT and SELECT queries.

Avro 数据类型 INSERTClickHouse 数据类型Avro 数据类型 SELECT
boolean, int, long, float, doubleInt(8\16\32), UInt(8\16\32)int
boolean, int, long, float, doubleInt64, UInt64long
boolean, int, long, float, doubleFloat32float
boolean, int, long, float, doubleFloat64double
bytes, string, fixed, enumStringbytesstring *
bytes, string, fixedFixedString(N)fixed(N)
enumEnum(8\16)enum
array(T)Array(T)array(T)
map(V, K)Map(V, K)map(string, K)
union(null, T), union(T, null)Nullable(T)union(null, T)
union(T1, T2, …) **Variant(T1, T2, …)union(T1, T2, …) **
nullNullable(Nothing)null
int (date) ***Date, Date32int (date) ***
long (timestamp-millis) ***DateTime64(3)long (timestamp-millis) ***
long (timestamp-micros) ***DateTime64(6)long (timestamp-micros) ***
bytes (decimal) ***DateTime64(N)bytes (decimal) ***
intIPv4int
fixed(16)IPv6fixed(16)
bytes (decimal) ***Decimal(P, S)bytes (decimal) ***
string (uuid) ***UUIDstring (uuid) ***
fixed(16)Int128/UInt128fixed(16)
fixed(32)Int256/UInt256fixed(32)
recordTuplerecord

* bytes 是默认值,通过设置 output_format_avro_string_column_pattern 控制。

** Variant类型 隐式接受 null 作为字段值,因此,例如 Avro union(T1, T2, null) 将转换为 Variant(T1, T2)。因此,在从 ClickHouse 生成 Avro 时,我们必须始终将 null 类型包含在 Avro union 类型集中,因为我们不知道在模式推理过程中任何值是否实际为 null

*** Avro 逻辑类型

不支持的 Avro 逻辑数据类型:

  • time-millis
  • time-micros
  • duration

示例用法

插入数据

要从 Avro 文件插入数据到 ClickHouse 表中:

输入的 Avro 文件的根架构必须是 record 类型。

要找到表列和 Avro 架构字段之间的对应关系,ClickHouse 会比较它们的名称。 这个比较是区分大小写的,未使用的字段会被跳过。

ClickHouse 表列的数据类型可以与插入的 Avro 数据的字段不同。在插入数据时,ClickHouse 会根据上表解释数据类型,然后 转换 数据到相应的列类型。

在导入数据时,当在架构中未找到字段且设置 input_format_avro_allow_missing_fields 已启用时,将使用默认值而不是抛出错误。

选择数据

要从 ClickHouse 表中选择数据到 Avro 文件:

列名必须:

  • [A-Za-z_] 开头
  • 后续只能包含 [A-Za-z0-9_]

输出的 Avro 文件压缩和同步间隔可以通过设置 output_format_avro_codecoutput_format_avro_sync_interval 分别配置。

示例数据

使用 ClickHouse 的 DESCRIBE 函数,可以快速查看 Avro 文件的推断格式,如以下示例所示。 该示例包含 ClickHouse S3 公共存储桶中一个可以公开访问的 Avro 文件的 URL:

格式设置

设置描述默认
input_format_avro_allow_missing_fields对于 Avro/AvroConfluent 格式:当架构中未找到字段时使用默认值而不是错误0
input_format_avro_null_as_default对于 Avro/AvroConfluent 格式:在 null 和非 Nullable 列的情况下插入默认值0
format_avro_schema_registry_url对于 AvroConfluent 格式:Confluent Schema Registry URL。
output_format_avro_codec输出所使用的压缩编解码器。可能的值:'null', 'deflate', 'snappy', 'zstd'。
output_format_avro_sync_interval以字节为单位的同步间隔。16384
output_format_avro_string_column_pattern对于 Avro 格式:选择为 AVRO 字符串的字符串列的正则表达式。
output_format_avro_rows_in_file文件中的最大行数(如果存储允许的话)1