我想在其中有多个文件的多个目录上运行count配置单元查询。文件路径如下所示
'2011/01/01/file20110101_01.csv
2011/01/01/file20110101_02.csv
2011/01/02/file20110201_01.csv
2011/01/02/file20110201_02.csv'
等等。
我用以下分区创建了一个外部表
'create external table table1(col1,col2...)
partitioned by (year string,month string)
STORED AS TEXTFILE'
并尝试添加分区直到月。
'ALTER TABLE partition_test_production1 ADD PARTITION(year='2011', month='01')
LOCATION 'blob path/2011/01/*/file201101*.csv';'
试过这个查询
'select count(1) from table1 where year='2011' AND month='01';'
但是计数显示为零。有关如何执行此操作的任何建议?
最佳答案
您不必单独添加所有文件,但必须单独添加所有底层目录。当您添加目录时,Hive会读取该目录中的所有文件,但不会读取子目录中的任何文件。例如:
create external table table1(col1,col2...)
partitioned by (year string, month string, day string)
STORED AS TEXTFILE
ALTER TABLE table1 ADD PARTITION(year='2011', month='01', day='01')
LOCATION 'hdfs:///path/2011/01/01/';
ALTER TABLE table1 ADD PARTITION(year='2011', month='01', day='02')
LOCATION 'hdfs:///path/2011/01/02/';
etc
通常,您会使用bash脚本或执行某些操作。循环遍历hdfs中的所有目录并生成hive语句以添加该分区。我不是bash专家,但举个例子:
hadoop fs -ls hdfs:///path/*/* | while read line; do
year="$(echo "$line" | awk -F/ '{print $(NF-2)}')"
month="$(echo "$line" | awk -F/ '{print $(NF-1)}')"
day="$(echo "$line" | awk -F/ '{print $(NF)}')"
hive -e "alter table table1 add partition(year='$year', month='$month', day='$day') location 'hdfs:///path/$year/$month/$day'"
done
似乎有一些旧的jira票证关于使表/分区位置更灵活,但是它们都尚未解决。
关于sql - 如何在具有多个文件的多个目录上运行配置单元查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23550115/