hdfs
从 HDFS 中的文件创建表。此表函数类似于 url 和 file 函数。
hdfs(URI, format, structure)
输入参数
URI
— HDFS 中文件的相对 URI。文件路径支持以下只读模式的 glob:*
、?
、{abc,def}
和{N..M}
,其中N
、M
— 数字,'abc'、'def'
— 字符串。format
— 文件的 格式。structure
— 表的结构。格式'column1_name column1_type, column2_name column2_type, ...'
。
返回值
具有指定结构的表,用于在指定文件中读取或写入数据。
示例
来自 hdfs://hdfs1:9000/test
的表以及从中选择的前两行
SELECT *
FROM hdfs('hdfs://hdfs1:9000/test', 'TSV', 'column1 UInt32, column2 UInt32, column3 UInt32')
LIMIT 2
┌─column1─┬─column2─┬─column3─┐
│ 1 │ 2 │ 3 │
│ 3 │ 2 │ 1 │
└─────────┴─────────┴─────────┘
路径中的 Glob
路径可以使用 globbing。文件必须匹配整个路径模式,而不仅仅是后缀或前缀。
*
— 表示任意多个字符,除了/
但包括空字符串。**
— 递归地表示文件夹内的所有文件。?
— 表示任意单个字符。{some_string,another_string,yet_another_one}
— 替换字符串'some_string'
、'another_string'
、'yet_another_one'
中的任何一个。字符串可以包含/
符号。{N..M}
— 表示任何数字>= N
且<= M
。
带有 {}
的构造类似于 remote 和 file 表函数。
示例
- 假设我们在 HDFS 上有几个具有以下 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’
- 查询这些文件中的行数
SELECT count(*)
FROM hdfs('hdfs://hdfs1:9000/{some,another}_dir/some_file_{1..3}', 'TSV', 'name String, value UInt32')
- 查询这两个目录中所有文件的行数
SELECT count(*)
FROM hdfs('hdfs://hdfs1:9000/{some,another}_dir/*', 'TSV', 'name String, value UInt32')
注意
如果您的文件列表包含带有前导零的数字范围,请为每个数字分别使用大括号构造或使用 ?
。
示例
从名为 file000
、file001
、...、file999
的文件中查询数据
SELECT count(*)
FROM hdfs('hdfs://hdfs1:9000/big_dir/file{0..9}{0..9}{0..9}', 'CSV', 'name String, value UInt32')
虚拟列
_path
— 文件路径。类型:LowCardinality(String)
。_file
— 文件名。类型:LowCardinality(String)
。_size
— 文件大小(字节)。类型:Nullable(UInt64)
。如果大小未知,则值为NULL
。_time
— 文件的最后修改时间。类型:Nullable(DateTime)
。如果时间未知,则值为NULL
。
Hive 风格分区
当设置 use_hive_partitioning
设置为 1 时,ClickHouse 将检测路径中的 Hive 风格分区 (/name=value/
),并允许在查询中使用分区列作为虚拟列。这些虚拟列将具有与分区路径中相同的名称,但以 _
开头。
示例
使用使用 Hive 风格分区创建的虚拟列
SELECT * from HDFS('hdfs://hdfs1:9000/data/path/date=*/country=*/code=*/*.parquet') where _date > '2020-01-01' and _country = 'Netherlands' and _code = 42;
存储设置
- hdfs_truncate_on_insert - 允许在插入前截断文件。默认禁用。
- hdfs_create_new_file_on_insert - 如果格式带有后缀,则允许在每次插入时创建一个新文件。默认禁用。
- hdfs_skip_empty_files - 允许在读取时跳过空文件。默认禁用。
- ignore_access_denied_multidirectory_globs - 允许忽略多目录 glob 的权限拒绝错误。
另请参阅