博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

【Hive】动态分区插入

使用动态分区插入数据时,无需指定分区键值,系统根据插入的数据,自动分配分区。
动态分区需注意以下几点:

需有一个同构的普通表做为源表;
分区键值和源表字段之间是根据位置来判断的,而不是根据命名来匹配的,分区键值一般对应SELECT后的最后一个字段;
动态分区默认是关闭的,使用前要设置相关参数;
下面是一个动态分区的例子:

 1 # 创建分区表和普通表
 2 create table myhive.student_dynamic_partition
 3 (
 4 stu_no int,
 5 stu_name string
 6 ) partitioned by (class_no int)
 7 row format delimited fields terminated by ' ';
 8 
 9 create table myhive.student
10 (
11 stu_no int,
12 stu_name string,
13 class_no int
14 )
15 row format delimited fields terminated by ' ';
16 
17 # 上传数据文件到HDFS
18 [hadoop@node01 hiveData]$ hdfs dfs -put student.txt /
19 [hadoop@node01 hiveData]$ hdfs dfs -cat /student.txt
20 1001 john 1
21 1002 susan 1
22 1003 smith 2
23 1004 tom 2
24 1005 simen 3
25 
26 # 普通表导入数据
27 hive (myhive)> load data inpath '/student.txt' overwrite into table student;
28 hive (myhive)> select * from student;
29 student.stu_no student.stu_name student.class_no
30 1001 john 1
31 1002 susan 1
32 1003 smith 2
33 1004 tom 2
34 1005 simen 3
35 
36 # 使用动态分区插入数据到分区表中
37 hive (myhive)> set hive.exec.dynamic.partition=true; #打开动态分区
38 hive (myhive)> set hive.exec.dynamic.partition.mode=nonstrict; #动态分区模式设置为非严格
39 hive (myhive)> set hive.exec.max.dynamic.partitions.pernode=1000; #设置每个mapper或reducer的最大动态分区个数
40 
41 hive (myhive)> insert overwrite table student_dynamic_partition
42 > partition (class_no)
43 > select stu_no,stu_name,class_no
44 > from student;
45 
46 hive (myhive)> select * from student_dynamic_partition;
47 student_dynamic_partition.stu_no student_dynamic_partition.stu_name student_dynamic_partition.class_no
48 1001 john 1
49 1002 susan 1
50 1003 smith 2
51 1004 tom 2
52 1005 simen 3

 


————————————————
版权声明:本文为CSDN博主「NextAction」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/NextAction/article/details/106843776

posted @ 2022-01-11 10:36  CHANG_09  阅读(1079)  评论(0)    收藏  举报