hive分区和分桶的示例分析

技术hive分区和分桶的示例分析这篇文章主要为大家展示了“hive分区和分桶的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“hive分区和分桶的示例分析”这篇文章吧。

本文主要向您展示“蜂巢分区和屈曲的实例分析”。内容简单易懂,条理清晰。希望能帮你解开疑惑。让边肖带领你学习《蜂巢分区与屈曲实例分析》一文。

1.为什么要分区??

当单个表中的数据量变得越来越大时,hive查询通常会扫描整个表,这会浪费很多我们不关心的时间和数据扫描。由此蜂巢导致了分区概念的划分。

00-1010看具体业务,只是把一堆数据拆分成多堆数据。id、年、月、日、地区、省份、蜂巢分区和mysql分区有什么区别?mysql分区字段采用的表内字段。Hive的分区字段使用表外字段。

2.怎么分区??

1.分区的本质是在这个表下创建一个对应的目录。2.分区名称不区分大小写,建议不要使用中文。3.您可以查询分区信息。然而,我们的分区字段相当于一个伪字段,它存在于元数据中,但并不真正存在于数据内容中。

4.加载数据时指定分区。

00-1010创建一级分区表:

如果不存在,则创建表day_part(

uid int,

uname字符串

)

分区依据(整年)

以“\t”结尾的行格式分隔字段

将本地in path '/root/Desktop/student . txt '中的数据加载到table day_part分区中(年份=2017);

将数据本地inpath '/root/Desktop/score.txt '加载到table day_part分区中(年份=2016);

显示分区日部分;

二级分区

如果不存在,则创建表day_part1(

uid int,

uname字符串

)

分区依据(年整数,月整数)

以“\t”结尾的行格式分隔字段

将本地in path '/root/Desktop/student . txt '中的数据加载到day_part1分区的表中(年=2017,月=04);

将数据本地inpath '/root/Desktop/score.txt '加载到day_part1分区表中(年=2017,月=03);

三级分区:

如果不存在,则创建表day_part2(

uid int,

uname字符串

)

分区依据(年整数、月整数、日整数)

以“\t”结尾的行格式分隔字段

对分区进行操作:显示分区:

显示分区日部分;

新分区:空

alter table day_part1添加分区(年=2017,月=2);

alter table day_part1添加分区(年=2017,月=1)分区(年=2016,月=12);

添加新分区并加载数据:

alter table day_part1添加分区(年=2016,月=11)location '/user/hive/warehouse/qf 1603 . db/day _ part 1/year=2017/month=2 ';

修改分区对应的存储路径:

# #路径必须从hdfs写入。

alter table day_part1分区(年=2016,月=11)设置位置' HDFS ://Linux 1:9000/user/hive/warehouse/qf 1603 . db/day _ part 1/year=2017/month=3 ';

删除分区:删除分区会删除对应的分区目录(数据)。

# #删除分区

alter table day _ part 1 drop partition(年=2017,月=2);

# #删除多个

br/>alter table day_part1 drop partition(year=2017,month=3),partition(year=2017,month=4);

静态分区、动态分区、混合分区 静态分区:新增分区或者是加载分区数据时,已经指定分区名。 动态分区:新增分区或者是加载分区数据时,分区名未知。 混合分区:静态分区和动态分区同时存在。

动态分区的相关属性: hive.exec.dynamic.partition=true :是否允许动态分区 hive.exec.dynamic.partition.mode=strict :分区模式设置nostrict strict:最少需要有一个是静态分区 nostrict:可以全部是动态分区 hive.exec.max.dynamic.partitions=1000 :允许动态分区的最大数量 hive.exec.max.dynamic.partitions.pernode =100 :单个节点上的mapper/reducer允许创建的最大分区

创建临时表:

##创建临时表
create table if not exists tmp(
uid int,
commentid bigint,
recommentid bigint,
year int,
month int,
day int
)
row format delimited fields terminated by '\t';
##加载数据
load data local inpath '/root/Desktop/comm' into table tmp;

创建动态分区:

##创建动态分区表
create table if not exists dyp1(
uid int,
commentid bigint,
recommentid bigint
)
partitioned by(year int,month int,day int)
row format delimited fields terminated by '\t'
;

为动态分区加载数据:

##严格模式
insert into table dyp1 partition(year=2016,month,day)
select uid,commentid,recommentid,month,day from tmp;
##非严格模式
##设置非严格模式动态分区
set hive.exec.dynamic.partition.mode=nostrict;
##创建动态分区表
create table if not exists dyp2(
uid int,
commentid bigint,
recommentid bigint
)
partitioned by(year int,month int,day int)
row format delimited fields terminated by '\t';
##为非严格模式动态分区加载数据
insert into table dyp2 partition(year,month,day)
select uid,commentid,recommentid,year,month,day from tmp;

hive提供我们一个严格模式:为了阻止用户不小心提交恶意hql hive.mapred.mode=nostrict : strict
如果该模式值为strict,将会阻止以下三种查询: 1、对分区表查询,where中过滤字段不是分区字段。 2、笛卡尔积join查询,join查询语句,不带on条件 或者 where条件。

select
stu.id,
stu.name,
score.grade
from student stu
join score
;

可以:

select
stu.id,
stu.name,
score.grade
from student stu
join score
where stu.id = score.uid
;

3、对order by查询,有order by的查询不带limit语句。

select
student.*
from student
order by student.id desc
;

注意: 1、尽量不要是用动态分区,因为动态分区的时候,将会为每一个分区分配reducer数量,当分区数量多的时候,reducer数量将会增加,对服务器是一种灾难。 2、动态分区和静态分区的区别,静态分区不管有没有数据都将会创建该分区,动态分区是有结果集将创建,否则不创建。 3、hive动态分区的严格模式和hive提供的hive.mapred.mode的严格模式。

分桶

1.为什么要分桶??

分区数据依然很大,对分区数据或者表数据更加细粒度的管理。 分桶关键字: clustered by(uid) into n buckets 、bucket 、 分桶使用表内字段 怎么分桶?? 对分桶字段进行hash值,然后将hash值模于总的桶数,然后得到桶数

2.分桶的意义:

1、快速抽样查询。tablesample 2、减少查询扫描数据量,提高查询效率。

##创建分桶表,设置4个分桶
create table if not exists bucket1(
uid int,
uname String
)
clustered by(uid) into 4 buckets
row format delimited fields terminated by '\t'
;

3.分桶的操作:

为分桶表加载数据: 分桶不能使用load方式来加载数据,而需要iinsert into方式来加载 并且需要设置属性:

##设置分桶启用
hive> set hive.enforce.bucketing=true;
##错误的加载数据方式
load data local inpath '/root/Desktop/student' into table bucket1;
##创建分桶表,设置4个分桶
create table if not exists bucket7(
uid int,
uname String
)
clustered by(uid) into 4 buckets
row format delimited fields terminated by '\t'
;
##为分桶表加载数据
insert into table bucket7
select id,name from student
;

分桶查询:tablesample(bucket x out of y on uid) 注意:x不能大于y x:所取桶的起始位置, y:所取桶的总数,y是总桶数的因子。y大于源总桶数相当于拉伸,y小于源总桶数相当于压缩 1 out of 2 1 1+4/2 2 out of 2 2 2+4/2

1 out of 4 1 1+4

select * from bucket7;
select * from bucket7 tablesample(bucket 1 out of 4 on uid);
select * from bucket7 tablesample(bucket 2 out of 4 on uid);
select * from bucket7 tablesample(bucket 1 out of 2 on uid);
select * from bucket7 tablesample(bucket 2 out of 2 on uid);
select * from bucket7 tablesample(bucket 3 out of 2 on uid);
select * from bucket7 tablesample(bucket 1 out of 8 on uid);
select * from bucket7 tablesample(bucket 5 out of 8 on uid);

分区+分桶:(qfstu) uid,uname,class,master gender分区 分桶uid 基偶分桶 查询女生中的学号为基数??

##创建表
create table if not exists qftmp(
uid int,
uname string,
class int,
gender int)
row format delimited fields terminated by '\t';
##加载数据
load data local inpath '/home/qf' into table qftmp;
##创建动态分区分桶表
create table if not exists qf(
uid int,
uname string,
class int)
partitioned by(gender int)
clustered by(uid) into 2 buckets
row format delimited fields terminated by '\t';
##为动态分区分桶表加载数据
insert into table qf partition(gender)
select uid,uname,class,gender from qftmp;

查询女生中的学号为基数?????

select * from qf where gender = 2 and uid%2 != 0;
select * from qf tablesample(bucket 2 out of 2 on uid) where gender = 2;

分桶使用内部关键字,分区使用的是外部字段。 两者都是对hive的一个优化。 分区和分桶的数量都要合理设置,不是越多越好。

抽样:

select * from student order by rand() limit 3;
select * from student limit 3;
select * from student tablesample(3 rows);
select * from student tablesample(20B); ##最小单位是B
select * from student tablesample(20 percent);##百分比

以上是“hive分区和分桶的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/142073.html

(0)

相关推荐

  • css中如何改变元素的内填充

    技术css中如何改变元素的内填充本篇内容主要讲解“css中如何改变元素的内填充”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“css中如何改变元素的内填充”吧!

    攻略 2021年12月9日
  • C#内存泄露问题分析

    技术C#内存泄露问题分析这篇文章主要介绍“C#内存泄露问题分析”,在日常操作中,相信很多人在C#内存泄露问题分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#内存泄露问题分析”的疑惑有

    攻略 2021年11月29日
  • vue3中watch和watchEffect怎么使用

    技术vue3中watch和watchEffect怎么使用这篇文章主要讲解了“vue3中watch和watchEffect怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学

    攻略 2021年11月30日
  • FireMonkey3D之中国象棋程序,二)制定规则

    技术FireMonkey3D之中国象棋程序,二)制定规则 FireMonkey3D之中国象棋程序(二)制定规则声明:本程序设计参考象棋巫师源码(开发工具dephi 11,建议用delphi 10.3以上

    礼包 2021年12月13日
  • 威力登激光雷达连接报套接字绑定失败该怎么办

    技术velodyne lidar连接报socket bind failed该怎么办今天就跟大家聊聊有关velodyne lidar连接报socket bind failed该怎么办,可能很多人都不太了解,为了让大家更加了

    攻略 2021年12月23日
  • 20191227甘泞与第六章学习笔记

    技术20191227甘泞与第六章学习笔记 20191227甘泞与第六章学习笔记第6章 信号和信号处理
    6.1信号和中断
    信号:发给进程的请求,将进程从正常执行转移到中断处理。
    中断:是从I/O设备或协处

    礼包 2021年11月14日