学习Hive(三)Hive参数、动态分区、分桶

一、Hive变量

1、以,hive --service metastore 开启服务 时:hive --hiveconf hive.cli.print.header=true;开启服务端。

通过设置这个参数(临时参数,此方法当前进程有效,配置文件修改永久有效)开启。

效果:

2、也可以在正常进入客户端后,使用set设置:

效果同上。

3、客户端参数初始化:在家目录下  .hiverc(没有的话创建一个) 写入配置,set xxx=xxx;这样即可

在开启客户端时会自动加载。

二、hive动态分区。

1、支持动态分区设置:

2、怎样加载数据(使用动态分区)。

前提hdfs已上传数据文件:

首先创建数据表(将文件数据全部载入):

create  table psn22
(
id int,
name string,
age int,
sex string,
likes array<string>,
address map<string,string>
)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':';

加载文件数据到数据表:load data  inpath '/usr/test' into table psn22; 

根据需求创建分区表:

create table psn23
(
id int,
name string,
likes array<string>,
address map<string,string>
)
partitioned by(age int,sex string)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':';

加载数据表中的数据到分区表:


from psn22
insert into psn23 partition(age,sex)
select id,name,likes,address,age,sex;

注意:在查看时发现已经不是按照id排序的了,因为会依次打开各自分区加载数据,说以从显示来说 分区间有序

 

三、hive分桶。

1、分桶是对列值取hash值的方式将数据放在不同的文件存储。

2、hive中的每一个表、分区都可以进行分桶。

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

应用场景:抽样、map-join

1】分桶支持设置:

 

2】分桶查询:

例子:前提是已经设置了分桶支持:set hive.enforce.bucketing=true;

1、首先准备数据文件。

2、创建数据表(用于从文件拉取数据):

create table psn24
(
id int,
name string,
age int
)
row format delimited
fields terminated by ',';

3、拉取数据:

load data local inpath '/usr/buckets' into table psn24;

4、创建分通表:

create table psn25
(
id int,
name string,
age int
)
clustered by (age) into 4 buckets
row format delimited
fields terminated by ',';

5,拉取数据到分通表:

insert into psn25 select id,name,age from psn24;

完毕:看起来数据没有变化:

但是,来看看目录:分成了四个文件!

抽样查询:select * from psn26 tablesample(bucket x out of y);

x 表是从哪个桶开始读,如图中x=2,又因为

所以x=2代表行数余桶数为1(从0开始的,第二个是1)。桶数为4,所以数据为元数据中行数,3、7的行。

注意y 必须为桶数的因子或倍数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值