现在的位置: 首页 > 综合 > 正文

Hive动态分区

2018年06月05日 ⁄ 综合 ⁄ 共 1426字 ⁄ 字号 评论关闭

Hive默认是静态分区,我们在插入数据的时候要手动设置分区,如果源数据量很大的时候,那么针对一个分区就要写一个insert,比如说,我们有很多日志数据,我们要按日期作为分区字段,在插入数据的时候我们不可能手动的去添加分区,那样太麻烦了。还好,Hive提供了动态分区,动态分区简化了我们插入数据时的繁琐操作。

使用动态分区的时候必须开启动态分区(动态分区默认是关闭的),语句如下:

set hive.exec.hynamic.partition=true;

下面我们再介绍几个参数:

set hive.exec.dynamic.partition.mode=nonstrict

注:这个属性默认是strict,即限制模式,strict是避免全分区字段是动态的,必须至少一个分区字段是指定有值即静态的,且必须放在最前面。设置为nonstrict之后所有的分区都可以是动态的了。

set hive.exec.max.dynamic.partitions.pernode=10000

注:这个属性表示每个节点生成动态分区的最大个数,默认是100

set hive.exec.max.dynamic.partitions=100000

注:这个属性表示一个DML操作可以创建的最大动态分区数,默认是1000

set hive.exec.max.created.files=150000

注:这个属性表示一个DML操作可以创建的最大文件数,默认是100000

下面通过一个示例来说明:

创建一个带分区的test表:

hive> create table test(
    > id int, name string
    > ,tel string)
    > partitioned by
    > (age int)
    > ROW FORMAT DELIMITED
    > FIELDS TERMINATED BY '\t'
    > STORED AS TEXTFILE;
OK
Time taken: 0.261 seconds

我们这里有有一张test_data表,数据如下:

hive (hive)> select * from wyp;
OK
id	name	age	tel
1	lavimer	23	13878789088
2	liaozhongmin	24	13787896578
3	liaozemin	25	13409876785
Time taken: 1.704 seconds
hive (hive)> 

现在我们从test_data表中查询出数据插入到test表中,第一种方式是指定分区:

hive (hive)> insert into table test
           > partition (age='23')
           > select id,name,tel
           > from test_data;

如果每一个年龄我们都要指定一个分区的话,就非常的麻烦,还好Hive提供了动态分区,如下:

hive (hive)> set hive.exec.dynamic.partition=true;
hive (hive)> set hive.exec.dynamic.partition.mode=nonstrict;
hive (hive)> insert into table test
           > partition (age)
           > select id,name,tel,age
           > from test_data;

上述语句中,select查询出来的age会作为分区的值,对应partition后面的age。

注:使用动态分区的时候一定要开启动态分区。另外如果是strict模式,则至少要有一个静态分区且在最前面。nonstrict模式下无要求。

抱歉!评论已关闭.