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

HDFS

ClickHouse Cloud 中不支持

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

ClickHouse 工程师不支持此功能,已知质量不稳定。如果出现任何问题,请自行修复并提交 pull request。

用法

ENGINE = HDFS(URI, format)

引擎参数

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

PARTITION BY

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

对于按月分区,请使用 toYYYYMM(date_column) 表达式,其中 date_column 是类型为 Date 的日期列。此处的分区名称具有 "YYYYMM" 格式。

示例

1. 设置 hdfs_engine_table

CREATE TABLE hdfs_engine_table (name String, value UInt32) ENGINE=HDFS('hdfs://hdfs1:9000/other_storage', 'TSV')

2. 填充文件

INSERT INTO hdfs_engine_table VALUES ('one', 1), ('two', 2), ('three', 3)

3. 查询数据

SELECT * FROM hdfs_engine_table LIMIT 2
┌─name─┬─value─┐
│ one │ 1 │
│ two │ 2 │
└──────┴───────┘

实现细节

  • 读取和写入可以是并行的。

  • 不支持

    • ALTERSELECT...SAMPLE 操作。
    • 索引。
    • 零拷贝 复制是可能的,但不建议使用。
    零拷贝复制尚未准备好用于生产环境

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

路径中的 Glob 模式

多个路径组件可以具有 glob 模式。为了被处理,文件应该存在并匹配整个路径模式。文件列表在 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. 有几种方法可以创建一个包含所有六个文件的表

CREATE TABLE table_with_range (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/{some,another}_dir/some_file_{1..3}', 'TSV')

另一种方法

CREATE TABLE table_with_question_mark (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/{some,another}_dir/some_file_?', 'TSV')

表由两个目录中的所有文件组成(所有文件都应满足查询中描述的格式和模式)

CREATE TABLE table_with_asterisk (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/{some,another}_dir/*', 'TSV')
注意

如果文件列表包含带有前导零的数字范围,请对每个数字分别使用大括号构造,或使用 ?

示例

创建表,文件名为 file000file001、...、file999

CREATE TABLE big_table (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/big_dir/file{0..9}{0..9}{0..9}', 'CSV')

配置

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

  <!-- Global configuration options for HDFS engine type -->
<hdfs>
<hadoop_kerberos_keytab>/tmp/keytab/clickhouse.keytab</hadoop_kerberos_keytab>
<hadoop_kerberos_principal>[email protected]</hadoop_kerberos_principal>
<hadoop_security_authentication>kerberos</hadoop_security_authentication>
</hdfs>

<!-- Configuration specific for user "root" -->
<hdfs_root>
<hadoop_kerberos_principal>[email protected]</hadoop_kerberos_principal>
</hdfs_root>

配置选项

由 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://127.0.0.1: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 的限制,仅支持老式方法,datanode 通信不受 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>
<libhdfs3_conf>/etc/clickhouse-server/hdfs-site.xml</libhdfs3_conf>
</hdfs>
  • 然后使用 hdfs-site.xmldfs.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

存储设置

参见