Hive之分桶表

  • 分区针对的是数据的存储路径,分桶针对的是数据文件

表创建

# 创建分桶表
create table stu_buck (id int, name string) clustered by(id) into 4 buckets row format delimited fields terminated by '\t';
# 导入数据到分桶表中
load data local inpath '/opt/module/datas/student.txt' into table stu_buck;
  • 查看创建的分桶表中是否分成4个桶,发现并没有分成4个桶。是什么原因呢? 是因为 创建分桶表时,数据通过子查询的方式导入
# 先建一个普通的stu表
create table stu(id int, name string) row format delimited fields terminated by '\t';
# 向普通的stu表中导入数据
load data local inpath '/opt/module/datas/student.txt' into table stu;
# 导入数据到分桶表,通过子查询的方式
insert into table stu_buck select id, name from stu;
#  如果发现还是只有一个分桶,需要设置一个属性
set hive.enforce.bucketing=true;
set mapreduce.job.reduces=-1;
insert into table stu_buck select id,name from stu;
# 这时就发现是4个分桶了

分桶抽样查询

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

查询表stu_buck中的数据
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 2out of 4), 表示总共抽取(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

猜你喜欢

转载自blog.csdn.net/qq_41725214/article/details/86569072
今日推荐