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

HDFS

Not supported in ClickHouse Cloud

该引擎通过允许在 ClickHouse 中管理 HDFS 上的数据,为 Apache Hadoop 生态系统提供集成。该引擎类似于 FileURL 引擎,但提供了 Hadoop 特有的功能。

该功能不受 ClickHouse 工程师支持,并且已知质量不稳定。如遇任何问题,请自行修复并提交拉取请求。

使用方法

引擎参数

  • URI - HDFS 上完整文件的 URI。URI 的路径部分可以包含通配符。在这种情况下,表将为只读。
  • format - 指定可用文件格式之一。要执行 SELECT 查询,格式必须支持输入;要执行 INSERT 查询,格式必须支持输出。可用格式在 Formats 部分列出。
  • [PARTITION BY expr]

PARTITION BY

PARTITION BY — 可选。在大多数情况下,您不需要分区键,如果需要,一般情况下,分区键不应比按月更详细。分区不会加速查询(与 ORDER BY 表达式相反)。您应该永远不要使用过于细粒度的分区。不要按照客户端标识符或名称对数据进行分区(相反,让客户端标识符或名称成为 ORDER BY 表达式中的第一列)。

按月分区时,使用 toYYYYMM(date_column) 表达式,其中 date_columnDate 类型的日期列。此处的分区名称为 "YYYYMM" 格式。

示例:

1. 设置 hdfs_engine_table 表:

2. 填充文件:

3. 查询数据:

实现细节

  • 读取和写入可以并行进行。

  • 不支持:

    • ALTERSELECT...SAMPLE 操作。
    • 索引。
    • 零拷贝 复制是可能的,但不推荐。
    零拷贝复制不适用于生产环境

    在 ClickHouse 版本 22.8 及更高版本中,默认禁用零拷贝复制。 不推荐在生产中使用此功能。

路径中的通配符

多个路径组件可以包含通配符。为了进行处理,文件必须存在并且与整个路径模式匹配。文件的列出在 SELECT 时确定(而不是在 CREATE 时确定)。

  • * — 替代任意数量的任意字符(不包括 /),也可以替代空字符串。
  • ? — 替代任意单个字符。
  • {some_string,another_string,yet_another_one} — 替代字符串 'some_string', 'another_string', 'yet_another_one' 中的任意一个。
  • {N..M} — 替代范围内的任意数字,从 N 到 M(包括两个边界)。

带有 {} 的结构与 remote 表函数类似。

示例

  1. 假设我们在 HDFS 上有多个 TSV 格式的文件,其 URI 如下:

    • 'hdfs://hdfs1:9000/some_dir/some_file_1'
    • 'hdfs://hdfs1:9000/some_dir/some_file_2'
    • 'hdfs://hdfs1:9000/some_dir/some_file_3'
    • 'hdfs://hdfs1:9000/another_dir/some_file_1'
    • 'hdfs://hdfs1:9000/another_dir/some_file_2'
    • 'hdfs://hdfs1:9000/another_dir/some_file_3'
  2. 可以通过几种方法创建一个包含所有六个文件的表:

另一种方式:

表包含两个目录中的所有文件(所有文件应满足查询中描述的格式和模式):

备注

如果文件列表包含带前导零的数字范围,请为每个数字单独使用花括号结构或使用 ?

示例

创建一个文件名为 file000, file001, ... , file999 的表:

配置

类似于 GraphiteMergeTree,HDFS 引擎支持使用 ClickHouse 配置文件的扩展配置。有两个可以使用的配置键:全局 (hdfs) 和用户级别 (hdfs_*)。首先应用全局配置,然后应用用户级别的配置(如果存在)。

配置选项

libhdfs3 支持的

参数默认值
rpc_client_connect_tcpnodelaytrue
dfs_client_read_shortcircuittrue
output_replace-datanode-on-failuretrue
input_notretry-another-nodefalse
input_localread_mappedfiletrue
dfs_client_use_legacy_blockreader_localfalse
rpc_client_ping_interval10 * 1000
rpc_client_connect_timeout600 * 1000
rpc_client_read_timeout3600 * 1000
rpc_client_write_timeout3600 * 1000
rpc_client_socket_linger_timeout-1
rpc_client_connect_retry10
rpc_client_timeout3600 * 1000
dfs_default_replica3
input_connect_timeout600 * 1000
input_read_timeout3600 * 1000
input_write_timeout3600 * 1000
input_localread_default_buffersize1 * 1024 * 1024
dfs_prefetchsize10
input_read_getblockinfo_retry3
input_localread_blockinfo_cachesize1000
input_read_max_retry60
output_default_chunksize512
output_default_packetsize64 * 1024
output_default_write_retry10
output_connect_timeout600 * 1000
output_read_timeout3600 * 1000
output_write_timeout3600 * 1000
output_close_timeout3600 * 1000
output_packetpool_size1024
output_heartbeat_interval10 * 1000
dfs_client_failover_max_attempts15
dfs_client_read_shortcircuit_streams_cache_size256
dfs_client_socketcache_expiryMsec3000
dfs_client_socketcache_capacity16
dfs_default_blocksize64 * 1024 * 1024
dfs_default_uri"hdfs://localhost:9000"
hadoop_security_authentication"simple"
hadoop_security_kerberos_ticket_cache_path""
dfs_client_log_severity"INFO"
dfs_domain_socket_path""

HDFS 配置参考 可能会解释某些参数。

ClickHouse 附加参数

参数默认值
hadoop_kerberos_keytab""
hadoop_kerberos_principal""
libhdfs3_conf""

限制

  • hadoop_security_kerberos_ticket_cache_pathlibhdfs3_conf 只能为全局,不能针对用户

Kerberos 支持

如果 hadoop_security_authentication 参数的值为 kerberos,ClickHouse 通过 Kerberos 进行身份验证。 参数在 这里,并且 hadoop_security_kerberos_ticket_cache_path 可能会有帮助。 请注意,由于 libhdfs3 的限制,仅支持传统方法, 数据节点通信不受 SASL 保护(HADOOP_SECURE_DN_USER 是这种安全方法的可靠指标)。使用 tests/integration/test_storage_kerberized_hdfs/hdfs_configs/bootstrap.sh 作为参考。

如果指定了 hadoop_kerberos_keytabhadoop_kerberos_principalhadoop_security_kerberos_ticket_cache_path,将使用 Kerberos 身份验证。在这种情况下,hadoop_kerberos_keytabhadoop_kerberos_principal 是必需的。

HDFS Namenode HA 支持

libhdfs3 支持 HDFS Namenode HA。

  • 从 HDFS 节点复制 hdfs-site.xml/etc/clickhouse-server/
  • 将以下内容添加到 ClickHouse 配置文件中:
  • 然后使用 hdfs-site.xml 中的 dfs.nameservices 标签值作为 HDFS URI 中的 Namenode 地址。例如,将 hdfs://[email protected]:8020/abc/ 替换为 hdfs://appadmin@my_nameservice/abc/

虚拟列

  • _path — 文件的路径。类型:LowCardinality(String)
  • _file — 文件名。类型:LowCardinality(String)
  • _size — 文件大小(以字节为单位)。类型:Nullable(UInt64)。如果大小未知,则值为 NULL
  • _time — 文件的最后修改时间。类型:Nullable(DateTime)。如果时间未知,则值为 NULL

存储设置

另见