Hive动态分区和分桶

15 篇文章 0 订阅
11 篇文章 0 订阅

Hive动态分区和分桶

1、Hive动态分区

1、hive的动态分区介绍

​ hive的静态分区需要用户在插入数据的时候必须手动指定hive的分区字段值,但是这样的话会导致用户的操作复杂度提高,而且在使用的时候会导致数据只能插入到某一个指定分区,无法让数据散列分布,因此更好的方式是当数据在进行插入的时候,根据数据的某一个字段或某几个字段值动态的将数据插入到不同的目录中,此时,引入动态分区。

2、hive的动态分区配置
--hive设置hive动态分区开启
	set hive.exec.dynamic.partition=true;
	默认:true
--hive的动态分区模式
	set hive.exec.dynamic.partition.mode=nostrict;
	默认:strict(至少有一个分区列是静态分区)
--每一个执行mr节点上,允许创建的动态分区的最大数量(100)
	set hive.exec.max.dynamic.partitions.pernode;
--所有执行mr节点上,允许创建的所有动态分区的最大数量(1000)	
	set hive.exec.max.dynamic.partitions;
--所有的mr job允许创建的文件的最大数量(100000)	
	set hive.exec.max.created.files;
3、hive动态分区语法
--Hive extension (dynamic partition inserts):
	INSERT OVERWRITE TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) 		select_statement FROM from_statement;
	INSERT INTO TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) 			select_statement FROM from_statement;
-- 案例(如果像是自动按照字段分区,就用指定分区值,否则数据会全部存到指定分区值目录下)
INSERT INTO TABLE psnforage_p PARTITION(age,sex) select id, name, likes, address,age,sex from psnforage;

2、Hive分桶

1、Hive分桶的介绍
	Bucketed tables are fantastic in that they allow much more efficient sampling than do non-bucketed tables, and they may later allow for time saving operations such as mapside joins. However, the bucketing specified at table creation is not enforced when the table is written to, and so it is possible for the table's metadata to advertise properties which are not upheld by the table's actual layout. This should obviously be avoided. Here's how to do it right.

​ 注意:

​ 1、Hive分桶表是对列值取hash值得方式,将不同数据放到不同文件中存储

​ 2、对于hive中每一个表、分区都可以进一步进行分桶

​ 3、由列的hash值除以桶的个数来决定每条数据划分在哪个桶中

2、Hive分桶的配置
--设置hive支持分桶
	set hive.enforce.bucketing=true;
3、创建分桶
CREATE TABLE user_info_bucketed(user_id BIGINT, firstname STRING, lastname STRING)
COMMENT 'A bucketed copy of user_info'
PARTITIONED BY(ds STRING)
CLUSTERED BY(user_id) INTO 256 BUCKETS
--设置hive支持分桶
	set hive.enforce.bucketing=true;
3、Hive分桶的抽样查询
--案例
	select * from bucket_table tablesample(bucket 1 out of 4 on columns)
--TABLESAMPLE语法:
	TABLESAMPLE(BUCKET x OUT OF y)
		x:表示从哪个bucket开始抽取数据(桶从1开始)
		y:必须为该表总bucket数的倍数或因子
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新鲜氧气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值