此引擎支持通过 ClickHouse 管理 HDFS 上的数据,从而与 Apache Hadoop 生态系统集成。该引擎与 File 表引擎 和 URL 引擎类似,但提供了 Hadoop 特有的功能。
此功能不受 ClickHouse engineers 支持,且已知质量较差。如果遇到任何问题,请自行修复并提交拉取请求。
用法
ENGINE = HDFS(URI, format)引擎参数
URI- HDFS 中文件的完整 URI。URI的路径部分可以包含通配符。在这种情况下,该表将为只读。格式- 指定一种可用的文件格式。要执行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 │
└──────┴───────┘实现细节
- 读取和写入可以并行进行。
- 不支持:
ALTER和SELECT...SAMPLE操作。- 索引。
- 支持零拷贝复制,但不建议使用。
路径中的通配符
路径中的多个组成部分都可以使用通配符。待处理的文件必须存在,并且要匹配完整的路径模式。文件列表是在执行 SELECT 时确定的 (而不是在 CREATE 时) 。
*— 匹配任意数量的任意字符,但不包括/,也包括空字符串。?— 匹配任意单个字符。{some_string,another_string,yet_another_one}— 匹配字符串'some_string'、'another_string'、'yet_another_one'中的任意一个。{N..M}— 匹配从 N 到 M 范围内的任意数字,包括两个端点。
带有 {} 的写法与 remote table function 类似。
示例
-
假设我们在 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'
-
可以通过多种方式创建一个包含这六个文件的表:
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')该表包含两个目录中的所有文件 (所有文件都应符合查询中描述的 格式 和 schema) :
CREATE TABLE table_with_asterisk (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/{some,another}_dir/*', 'TSV')示例
创建一个表,其中包含名为 file000、file001、…、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>clickuser@TEST.CLICKHOUSE.TECH</hadoop_kerberos_principal>
<hadoop_security_authentication>kerberos</hadoop_security_authentication>
</hdfs>
<!-- Configuration specific for user "root" -->
<hdfs_root>
<hadoop_kerberos_principal>root@TEST.CLICKHOUSE.TECH</hadoop_kerberos_principal>
</hdfs_root>配置选项
libhdfs3 支持的配置项
| 参数 | 默认值 |
|---|---|
| rpc_client_connect_tcpnodelay | true |
| dfs_client_read_shortcircuit | true |
| output_replace-datanode-on-failure | true |
| input_notretry-another-node | false |
| input_localread_mappedfile | true |
| dfs_client_use_legacy_blockreader_local | false |
| rpc_client_ping_interval | 10 * 1000 |
| rpc_client_connect_timeout | 600 * 1000 |
| rpc_client_read_timeout | 3600 * 1000 |
| rpc_client_write_timeout | 3600 * 1000 |
| rpc_client_socket_linger_timeout | -1 |
| rpc_client_connect_retry | 10 |
| rpc_client_timeout | 3600 * 1000 |
| dfs_default_replica | 3 |
| input_connect_timeout | 600 * 1000 |
| input_read_timeout | 3600 * 1000 |
| input_write_timeout | 3600 * 1000 |
| input_localread_default_buffersize | 1 * 1024 * 1024 |
| dfs_prefetchsize | 10 |
| input_read_getblockinfo_retry | 3 |
| input_localread_blockinfo_cachesize | 1000 |
| input_read_max_retry | 60 |
| output_default_chunksize | 512 |
| output_default_packetsize | 64 * 1024 |
| output_default_write_retry | 10 |
| output_connect_timeout | 600 * 1000 |
| output_read_timeout | 3600 * 1000 |
| output_write_timeout | 3600 * 1000 |
| output_close_timeout | 3600 * 1000 |
| output_packetpool_size | 1024 |
| output_heartbeat_interval | 10 * 1000 |
| dfs_client_failover_max_attempts | 15 |
| dfs_client_read_shortcircuit_streams_cache_size | 256 |
| dfs_client_socketcache_expiryMsec | 3000 |
| dfs_client_socketcache_capacity | 16 |
| dfs_default_blocksize | 64 * 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_path和libhdfs3_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_keytab、hadoop_kerberos_principal 或 hadoop_security_kerberos_ticket_cache_path,则会使用 Kerberos 身份验证。在这种情况下,必须指定 hadoop_kerberos_keytab 和 hadoop_kerberos_principal。
支持 HDFS NameNode HA
libhdfs3 支持 HDFS NameNode 高可用 (HA) 。
- 将
hdfs-site.xml从某个 HDFS 节点复制到/etc/clickhouse-server/。 - 在 ClickHouse 配置文件中添加以下内容:
<hdfs>
<libhdfs3_conf>/etc/clickhouse-server/hdfs-site.xml</libhdfs3_conf>
</hdfs>- 然后使用
hdfs-site.xml中dfs.nameservices的值作为 HDFS URI 中的 NameNode 地址。例如,将hdfs://appadmin@192.168.101.11:8020/abc/替换为hdfs://appadmin@my_nameservice/abc/。
虚拟列
_path— 文件路径。类型:LowCardinality(String)。_file— 文件名。类型:LowCardinality(String)。_size— 文件大小 (单位为字节) 。类型:Nullable(UInt64)。如果大小未知,则值为NULL。_time— 文件的最后修改时间。类型:Nullable(DateTime)。如果时间未知,则值为NULL。
_etag— 文件的 ETag。类型:LowCardinality(String)。HDFS 没有原生 ETag,因此该值会根据文件的修改时间和大小合成,格式为<mtime_seconds>_<size>。由于libhdfs3仅以秒级精度提供修改时间,在同一秒内重写且文件大小不变时,会产生相同的值;因此,该合成 ETag 不会用作内容缓存键。
存储设置
- hdfs_truncate_on_insert - 允许在向文件插入数据前先将其截断。默认禁用。
- hdfs_create_new_file_on_insert - 如果 格式 带有 suffix,则允许在每次插入时创建新文件。默认禁用。
- hdfs_skip_empty_files - 允许在读取时跳过空文件。默认禁用。
另请参见