hive分桶表的使用及分桶抽样查询

分桶表数据存储

 分区针对的是数据的存储路径;分桶针对的是数据文件。分区提供一个隔离数据和优化查询的便利方式。不过,并非所有的数据集都可形成合理的分区,特别是之前所提到过的要确定合适的划分大小这个疑虑。  分桶是将数据集分解成更容易管理的若干部分的另一个技术。

1.先创建分桶表,通过直接导入数据文件的方式

准备数据

001     s1
002     s2
003     s3
004     s4
005     s5
006     s6
007     s7
008     s8
009     s9
010     s10
011     s11
012     s12
013     s13
014     s14

创建分桶表:

hive (default)> create table buckets(id int,name string)
              > clustered by(id)
              > into 4 buckets
              > row format delimited fields terminated by '\t';
clustered by(id)是通过id进行分桶,into 4 buckets是分四个桶

我们用传统的load进行放入数据

hive (default)>load data local inpath '/root/hivetext/buckets.txt' into table buckets;

查看创建的分桶表中是否分成 4 个桶

我们发现并没有分为四个桶,为什么呢?因为load数据相当于往里put数据,不会分桶,必须经过mapreduce才会分桶

2.创建分桶表时,数据通过子查询的方式导入

首先我们必须设置属性

开启分桶功能
hive (default)> set hive.enforce.bucketing=true;
设置reduces值为-1,默认就是-1,没改过的话不用设置
hive (default)> set mapreduce.job.reduces=-1;

 创建普通的buckets_test表

hive (default)> create table buckets_test(id int,name string)
              > row format delimited fields
              > terminated by '\t';

向普通表buckets_test中导入数据

hive (default)> load data local inpath '/root/hivetext/buckets.txt' into table buckets_test;

清空buckets表中数据

hive (default)> truncate table buckets;

 通过子查询方式导入到buckets表中

 

 查询分桶数据,会根据id进行分桶

 

分桶抽样查询

对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive 可以通过对表进行抽样来满足这个需求。查询表 stu_buck 中的数据。

 

hive (default)> select * from stu_buck tablesample(bucket 1 out of 4 on id);

 

注:tablesample 是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y) 。

y 必须是 table 总 bucket 数的倍数或者因子。hive 根据 y 的大小,决定抽样的比例。例

如,table 总共分了 4 份,当 y=2 时,抽取(4/2=)2 个 bucket 的数据,当 y=8 时,抽取(4/8=)1/2 个 bucket 的数据。

x 表示从哪个 bucket 开始抽取,如果需要取多个分区,以后的分区号为当前分区号加上 y。例如,table 总 bucket 数为 4,tablesample(bucket 1 out of 2),表示总共抽取(4/2=)2 个 bucket 的数据,抽取第 1(x)个和第 3(x+y)个 bucket 的数据。

注意:x 的值必须小于等于 y 的值,否则

FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值