请不要介意它是否是一个非常基本的:
1 ravi 100长度 2克里希纳200长度 3 fff 300秒
我在hive中创建了一个带有城市分区的表,并加载了如下数据:
create external table temp(id int, name string, sal int)
partitioned by(city string)
location '/testing';
load data inpath '/test.txt' into table temp partition(city='hyd');
在HDFS中,结构是/testing/temp/city=hyd/test.txt
当我查询表为“select * from temp”时;
temp.id temp.name temp.sal temp.city
1 ravi 100 hyd
2 krishna 200 hyd
3 fff 300 hyd
这里我的问题是为什么第三行中“sec”的城市名称在输出中变为“hyd”?
我这边有什么不对吗?
提前致谢 !!!
你的问题是这样的:
load data inpath '/test.txt' into table temp partition(city='hyd');
您加载到此分区的所有数据都使用city ='hyd'。如果您正在进行静态分区,则您有责任将正确的值放入分区。
只需从txt文件中删除最后一行,将其放入test2.txt并执行:
load data inpath '/test.txt' into table temp partition(city='hyd');
load data inpath '/test2.txt' into table temp partition(city='sec');
是的,不是那么舒服,但静态分区以这种方式工作。
我希望分区对于单个文件的load语句不能正常工作。
相反,我们需要写入hive中的临时表(stat_parti
),然后我们需要另一个分区表(stat_test
)
例如:
create external table stat_test(id int, name string, sal int)
partitioned by(city string)
row format delimited fields
terminated by ' '
location '/user/test/stat_test';
并且可以提供静态或动态分区。
insert into table stat_test partition(city='hyd') select id,name,sal from stat_parti where city='hyd';
insert into table stat_test partition(city='sec') select id,name,sal from stat_parti where city='sec';
在这里我们需要启用
set hive.exec.dynamic.partition=true
set hive.exec.dynamic.partition.mode=nonstrict
insert overwrite table stat_test partition(city) select id,name,sal from stat_parti;
您已经在HDFS路径中复制的数据文件test.txt - '/ testing / temp / city = hyd / test.txt'所有数据都将转到分区 - 'city = hyd'
和Hive使用目录名称来检索值。因此,字段城市名称来自目录名称hyd。