本文共 2895 字,大约阅读时间需要 9 分钟。
分区的值由数据来确定,而不是说load数据的时候自行制定
前提条件
-- 属性说明:hive.exec.dynamic.partition=true:是否支持动态分区操作hive.exec.dynamic.partition.mode=strict/nonstrict: 严格模式/非严格模式 严格模式:动态分区导入数据时,必须指定至少一个静态值。 非严格模式:动态分区导入数据时,可以不指定静态值hive.exec.max.dynamic.partitions=1000: 总共允许创建的动态分区的最大数量 动态导入数据时,每个表支持的最大动态分区数量hive.exec.max.dynamic.partitions.pernode=100:in each mapper/reducer node 每个MapTask/reduceTask最多同时处理的分区数量
创建分区表.创建的时候跟静态一样的,无所谓动态静态
create external table dy_part1( sid int, name string, gender string, age int, academy string) partitioned by (dt string) row format delimited fields terminated by ',';
为了给动态分区表导入数据,创建一个临时表并用数据填充
create external table tmp_part1(sid int,name string,gender string,age int,academy string,dat string)row format delimited fields terminated by ',';load data local inpath '/data/student2.txt' into table tmp_part1;95001,李勇,男,20,CS,2017-8-3195002,刘晨,女,19,IS,2017-8-3195003,王敏,女,22,MA,2017-8-3195004,张立,男,19,IS,2017-8-3195005,刘刚,男,18,MA,2018-8-3195006,孙庆,男,23,CS,2018-8-3195007,易思玲,女,19,MA,2018-8-3195008,李娜,女,18,CS,2018-8-3195009,梦圆圆,女,18,MA,2018-8-3195010,孔小涛,男,19,CS,2017-8-3195011,包小柏,男,18,MA,2019-8-3195012,孙花,女,20,CS,2017-8-3195013,冯伟,男,21,CS,2019-8-3195014,王小丽,女,19,CS,2017-8-3195015,王君,男,18,MA,2019-8-3195016,钱国,男,21,MA,2019-8-3195017,王风娟,女,18,IS,2019-8-3195018,王一,女,19,IS,2019-8-3195019,邢小丽,女,19,IS,2018-8-3195020,赵钱,男,21,IS,2019-8-3195021,周二,男,17,MA,2018-8-3195022,郑明,男,20,MA,2018-8-31
接下来该插入数据了. 分区依据的是临时表的最后一个字段,默认就是这样的!
hive> insert into dy_part1 partition(dt) select * from tmp_part1;FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict
报错,那修改一下吧
启动插入,这个走了MR,会比较慢.hive> set hive.exec.dynamic.partition.mode=nonstrict;hive> insert into dy_part1 partition(dt) select * from tmp_part1;Automatically selecting local only mode for queryWARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.Query ID = root_20201128223704_8e1db866-a4ee-413e-9e5d-a192bbfad7eeTotal jobs = 3
hive> select * from dy_part1;OK95001 李勇 男 20 CS 2017-8-3195002 刘晨 女 19 IS 2017-8-3195003 王敏 女 22 MA 2017-8-3195004 张立 男 19 IS 2017-8-31...95016 钱国 男 21 MA 2019-8-3195017 王风娟 女 18 IS 2019-8-3195018 王一 女 19 IS 2019-8-3195020 赵钱 男 21 IS 2019-8-31hive> show partitions dy_part1;dt=2017-8-31dt=2018-8-31dt=2019-8-31
可以的,只要你指定分区就可以.不指定分区是不行的!
比如load data local inpath '/data/student2.txt' into table dy_part1 partition (dt='2020-1-1');
动态分区的字段,不能不同值太多,否则就是灾难, 会在hdfs上生成大量小文件,大量小文件MR搞起来很慢! 所以,动态分区用的时候,要看时机
转载地址:http://nvsmz.baihongyu.com/