hive根据现有数据表创建分区表,进行动态或静态分区插入数据

1:现有数据表结构定义:

CREATE TABLE `tab_user`(
  `name` string, 
  `age` int, 
  `sex` string, 
  `addr` string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY ',' 
  LINES TERMINATED BY '\n' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://node:9000/user/hive/warehouse/daxin.db/tab_user'

2:现有数据表的数据:

daxin	18	male	beijing
mali	28	female	shandong
wangsan	34	male	beijing
lisi	45	male	liaoning
liwu	58	female	beijing
maoliu	43	male	anhui
zhouba	62	female	beijing

3:对现有用户表数据按照位置信息进行分区,创建新的用户分区表:

CREATE TABLE `user_partition_tab`(
`name` string, 
`age` int, 
`sex` string)  PARTITIONED BY(addr STRING);

4:插入用户数据:

insert overwrite table ptab PARTITION (addr)  select name,age,sex,addr from user_partition_tab;

执行上面代码会报错:

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

拓展:

对于hive分区表插入数据时候,对于分区字段可以自行指定一个静态字段或者根据分区字段的具体值进行插入分区表,对于前者指定一个分区值的插入则成为静态分区插入,而后者根据分区字段的具体值插入则成为动态分区插入。

a:静态插入:

insert overwrite table ptab PARTITION (addr='qiqihaer')  select name,age,sex from tab_user;

指定分区字段addr的值为qiqihaer,如果表中该分区不存在的话则创建该分区。

b:动态插入:

insert overwrite table ptab PARTITION (addr)  select name,age,sex,addr from tab_user;

该分区字段是根据select出来的具体值进行动态分区,因此就需要开启:set hive.exec.dynamic.partition.mode=nonstrict。

Configuration property

Default

Note

hive.exec.dynamic.partition

true

Needs to be set to true to enable dynamic partition inserts

hive.exec.dynamic.partition.mode

strict

In strict mode, the user must specify at least one static partition in case the user accidentally overwrites all partitions, in nonstrict mode all partitions are allowed to be dynamic

hive.exec.max.dynamic.partitions.pernode

100

Maximum number of dynamic partitions allowed to be created in each mapper/reducer node

hive.exec.max.dynamic.partitions

1000

Maximum number of dynamic partitions allowed to be created in total

hive.exec.max.created.files

100000

Maximum number of HDFS files created by all mappers/reducers in a MapReduce job

hive.error.on.empty.partition

false

Whether to throw an exception if dynamic partition insert generates empty results

hive.exec.dynamic.partition.mode默认是strict,必须制定一个分区进行插入数据,以避免覆盖所有的分区数据;但是如果需要动态分区插入数据就必须设置nonstrict,nonstrict表示不是严格的必须指定一个静态分区,言外之意就是动态分区插入。其他属性容易理解不解释。

参考:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML#LanguageManualDML-DynamicPartitionInserts

 

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
对于Hive分区表插入数据,有两种方式:动态分区插入静态分区插入动态分区插入是指在插入数据时,根据数据中的某个列的值来动态创建分区,并将数据插入到相应的分区中。要使用动态分区插入,需要先设置以下两个参数: set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=strict; 然后使用以下语句进行插入操作: insert into table <table_name> partition(<partition_column>) select <columns> from <source_table>; 其中,<table_name>是目标的名称,<partition_column>是用于分区的列名,<columns>是要插入的列,可以是源的列或其他计算得到的值,<source_table>是源数据表的名称。 静态分区插入是指在插入数据时,直接指定分区的值,并将数据插入到指定的分区中。要使用静态分区插入,需要先设置以下两个参数: set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict; 然后使用以下语句进行插入操作: insert into table <table_name> partition(<partition_columns>) select <columns> from <source_table>; 其中,<table_name>是目标的名称,<partition_columns>是用于分区的列名及其对应的值,<columns>是要插入的列,可以是源的列或其他计算得到的值,<source_table>是源数据表的名称。 这是Hive分区表插入数据的常用方法,可以根据实际需求选择使用动态分区插入还是静态分区插入

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值