首页 > 解决方案 > Hive 仅从 hdfs 导入某些文件类型

问题描述

我正在使用 Serde 创建一个外部表

org.apache.hive.hcatalog.data.JsonSerde

hdfs 文件夹位置有多种文件格式,我想只导入json文件类型。我试着用

**SERDEPROPERTIES (“input.regex” = “*.json”)** 

但似乎不起作用

标签: hivehdfshiveqlhive-serdehiveddl

解决方案


Hive 具有名为 INPUT__FILE__NAME 的虚拟列,您可以在 where 子句中对其进行过滤。

select * from --do everything else on this level
(select * from your_table --do filtering in the subquery wrapper
where INPUT__FILE__NAME rlike '\\.json$' --ends with .json
)s

我不确定它是否会有效工作,因为不幸的是这些相关的 Jiras 尚未实现:Add file pruning into Hive and Selectively include EXTERNAL TABLE source files via REGEX

如果此解决方案会因为映射器读取文件而运行缓慢,您可能需要将所需的文件复制到单独的文件夹并在其上构建一个表。

找到了另一种您可以使用的解决方案:SymlinkTextInputFormat

使用包含所有所需文件列表的文件创建一些新位置,并在此位置之上构建一个表。

例如,表的位置是"/user/hive/mytable"。有一个名为“/user/hive/mytable/myfile.txt”的文件。在文件中,有 2 行,"/user/myname/textfile1.txt"并且"/user/myname/textfile2.txt"

我们可以做的:

CREATE TABLE mytable (...) STORED AS INPUTFORMAT 'org.apache.hadoop.hive.io.SymlinkTextInputFormat' LOCATION '/user/hive/mytable';
SELECT * FROM mytable;

这将返回 2 个文件的内容:“/user/myname/textfile1.txt”和“/user/myname/textfile2.txt”


推荐阅读