黑猴子的家:Hive 分桶及分桶抽样查询

您所在的位置:网站首页 hive分桶大小 黑猴子的家:Hive 分桶及分桶抽样查询

黑猴子的家:Hive 分桶及分桶抽样查询

2024-07-14 17:32:42| 来源: 网络整理| 查看: 265

1、分桶表数据存储

概念

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

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

(1)数据准备 student.txt

1001 ss1 1002 ss2 1003 ss3 1004 ss4 1005 ss5 1006 ss6 1007 ss7 1008 ss8 1009 ss9 1010 ss10 1011 ss11 1012 ss12 1013 ss13 1014 ss14 1015 ss15 1016 ss16

(2)创建分桶表

hive (default)> create table stu_buck(id int, name string) clustered by(id) into 4 buckets row format delimited fields terminated by '\t';

(3)查看表结构

hive (default)> desc formatted stu_buck; Num Buckets: 4

(4)导入数据到分桶表中

hive (default)> load data local inpath '/opt/module/datas/student.txt' into table stu_buck;

尖叫提示:查看创建的分桶表中是否分成4个桶,发现并没有分成4个桶。是什么原因呢?我们继续方式二。

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

(1)先建一个普通的stu表

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

(2)向普通的stu表中导入数据

hive (default)> load data local inpath '/opt/module/datas/student.txt' into table stu;

(3)清空stu_buck表中数据

hive (default)> truncate table stu_buck; hive (default)> select * from stu_buck;

(4)导入数据到分桶表,通过子查询的方式

hive (default)> insert into table stu_buck select id, name from stu cluster by(id);

(5)发现还是只有一个分桶,需要设置一下属性,再来一遍

hive (default)>set hive.enforce.bucketing=true; hive (default)> set mapreduce.job.reduces=-1; hive (default)> truncate table stu_buck; hive (default)>insert into table stu_buck select id, name from stu cluster by(id);

(6)查询分桶的数据

hive (default)> select * from stu_buck; OK stu_buck.id stu_buck.name 1001 ss1 1005 ss5 1009 ss9 1012 ss12 1016 ss16 1002 ss2 1006 ss6 1013 ss13 1003 ss3 1007 ss7 1010 ss10 1014 ss14 1004 ss4 1008 ss8 1011 ss11 1015 ss15 2、分桶抽样查询

对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。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开始抽取。例如,table总bucket数为4,tablesample(bucket 4 out of 4),表示总共抽取(4/4=)1个bucket的数据,抽取第4个bucket的数据。 注意:x的值必须小于等于y的值,否则

FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck 3、数据块抽样

Hive提供了另外一种按照百分比进行抽样的方式,这种是基于行数的,按照输入路径下的数据块百分比进行的抽样。

hive (default)> select * from stu tablesample(0.1 percent) ;

尖叫提示:这种抽样方式不一定适用于所有的文件格式。另外,这种抽样的最小抽样单元是一个HDFS数据块。因此,如果表的数据大小小于普通的块大小128M的话,那么将会返回所有行。



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭