使用load命令将数据加载到hive静态分区表

9ceoxa92  于 2021-06-02  发布在  Hadoop
关注(0)|答案(3)|浏览(363)

请不要介意这是一个非常基本的:

测试.txt

1拉维100液压
2克里希纳200液压
3 fff 300秒
我在hive中创建了一个表,在city上有分区,并加载了如下数据:

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”?
我这边有什么问题吗?
提前谢谢!!!
tnkciper

tnkciper1#

我希望分区不能与单个文件的load语句一起正常工作。
相反,我们需要写入temp表( stat_parti )在hive中,从那里我们需要另一个分区表( 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';

并能给出静态或动态分区。

1) 静态分区

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';

2) 动态分区

这里我们需要启用

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;
e4eetjau

e4eetjau2#

你的问题是:

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');

是的,不太舒服,但是静态分区是这样工作的。

1aaf6o9v

1aaf6o9v3#

您在hdfs路径中复制的数据文件test.txt-'/testing/temp/city=hyd/test.txt'所有数据将进入分区-'city=hyd'
配置单元使用目录名来检索值。因此,字段city名称来自目录名hyd。

相关问题