Hive基础知识(十六):Hive-SQL分区表使用与优化

news2024/9/24 23:10:54

1. 分区表

分区表实际上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过 WHERE 子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多

2. 分区表基本操作

1)引入分区表(需要根据日期对日志进行管理,通过部门信息模拟)

2)创建分区表语法

hive (hive3)> create table dept_par(deptno int , dname string, loc string) partitioned by (day string) row format delimited fields terminated by'';
OK
Time taken: 2.547 seconds

注意:分区字段不能是表中已经存在的数据,可以将分区字段看作表的伪列。

3)加载数据到分区表中

hive (hive3)> load data local inpath '/home/zzdq/hive/dept_20200401.log' into table dept_par partition(day='20200401');
Loading data to table hive3.dept_par partition (day=20200401)
OK
Time taken: 1.763 seconds
hive (hive3)> load data local inpath '/home/zzdq/hive/dept_20200402.log' into table dept_par partition(day='20200402');
Loading data to table hive3.dept_par partition (day=20200402)
OK
Time taken: 0.962 seconds
hive (hive3)> load data local inpath '/home/zzdq/hive/dept_20200403.log' into table dept_par partition(day='20200403');
Loading data to table hive3.dept_par partition (day=20200403)
OK
Time taken: 0.869 seconds

4)查找全部数据(可以看到多了个分区字段,但这个字段不是放在表中,而是放在目录上,所以条件查分区的效率会高很多)

hive (hive3)> select * from  dept_par;
OK
dept_par.deptno dept_par.dname  dept_par.loc  dept_par.day
10  ACCOUNTING 1700 20200401
20  RESEARCH 1800 20200401
30  SALES 1900 20200402
40  OPERATIONS 1700 20200402
50  TEST 2000 20200403
60  DEV 1900 20200403
NULL  NULL  NULL 20200403
Time taken: 3.684 seconds, Fetched: 7 row(s)

5)条件查询

hive (hive3)> select * from  dept_par where day = 20200401;
OK
dept_par.deptno dept_par.dname  dept_par.loc  dept_par.day
10  ACCOUNTING 1700 20200401
20  RESEARCH 1800 20200401
Time taken: 2.749 seconds, Fetched: 2 row(s)

另外:分区的信息也存放在mysql的partition表中。

3. 分区的增删改查

单分区查询

hive (default)> select * from dept_partition where day='20200401';

多分区联合查询

hive (default)> select * from dept_partition where day='20200401'
 union
 select * from dept_partition where day='20200402'
 union
 select * from dept_partition where day='20200403';
hive (default)> select * from dept_partition where day='20200401' or day='20200402' or day='20200403';

5)增加分区

创建单个分区

hive (default)> alter table dept_partition add partition(day='20200404');

创建多个分区

hive (hive3)> alter table dept_par add partition(day='20200404') partition(day='20200405') partition(day='20200406');
OK
Time taken: 0.844 seconds

6)删除分区

删除单个分区

hive (default)> alter table dept_partition drop partition (day='20200406');

同时删除多个分区

hive (hive3)> alter table dept_par drop partition(day='20200404'),partition(day='20200405'),partition(day='20200406');
Dropped the partition day=20200404
Dropped the partition day=20200405
Dropped the partition day=20200406
OK
Time taken: 0.981 seconds

7)查看分区表有多少分区

hive (hive3)> show partitions dept_par;
OK
partition
day=20200401
day=20200402
day=20200403
Time taken: 0.296 seconds, Fetched: 3 row(s)

8)查看分区表结构

hive (hive3)> desc formatted dept_par;
# Partition Information 
# col_name  data_type  comment 
day  string 

4. 二级分区

思考: 如何一天的日志数据量也很大,如何再将数据拆分?

1)创建二级分区表

hive (hive3)> create table dept_par2(deptno int , dname string , loc string) partitioned by (day string , hour string) row format delimited fields terminated by '';
OK
Time taken: 0.172 seconds

2)正常的加载数据

hive (hive3)> load data local inpath "/home/zzdq/hive/dept_20200401.log" into table dept_par2 partition(day='001',hour='401');
Loading data to table hive3.dept_par2 partition (day=001, hour=401)
OK
Time taken: 1.221 seconds
hive (hive3)> load data local inpath "/home/zzdq/hive/dept_20200402.log" into table dept_par2 partition(day='001',hour='402');
Loading data to table hive3.dept_par2 partition (day=001, hour=402)
OK
Time taken: 0.899 seconds
hive (hive3)> load data local inpath "/home/zzdq/hive/dept_20200403.log" into table dept_par2 partition(day='001',hour='403');
Loading data to table hive3.dept_par2 partition (day=001, hour=403)
OK
Time taken: 1.011 seconds
hive (hive3)> load data local inpath "/home/zzdq/hive/dept_20200403.log" into table dept_par2 partition(day='002',hour='404');
Loading data to table hive3.dept_par2 partition (day=001, hour=404)
OK
Time taken: 0.878 seconds

查询

hive (hive3)> select * from dept_par2 where day = '001' and hour = '404';
OK
dept_par2.deptno  dept_par2.dname dept_par2.loc  dept_par2.day  dept_par2.hour
50  TEST 2000 001 404
60  DEV 1900 001 404
NULL  NULL  NULL 001 404
Time taken: 0.692 seconds, Fetched: 3 row(s)

3)把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式

(1)方式一:上传数据后修复

上传数据

hive (default)> dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=13;
hive (default)> dfs -put /opt/module/datas/dept1.txt /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=13;

查询数据(查询不到刚上传的数据)

hive (default)> select * from dept_partition2 where day='20200401' and hour='13';

让分区信息产生关联的操作:执行修复命令

hive> msck repair table dept_partition2;

再次查询数据

hive (default)> select * from dept_partition2 where day='20200401' and hour='13';

(2)方式二:上传数据后添加分区

上传数据

hive (default)> dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=14; 
hive (default)> dfs -put /opt/module/hive/datas/dept_20200401.log /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=14; 

执行添加分区

hive (default)> alter table dept_partition2 add  partition(day='201709',hour='14'); 

查询数据

hive (default)> select * from dept_partition2 where day='20200401' and  hour='14';

(3)方式三:创建文件夹后,使用load

load 数据到分区创建目录

hive (default)> dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=15; 

上传数据

hive (default)> load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table dept_partition2 partition(day='20200401',hour='15'); 

查询数据

hive (default)> select * from dept_partition2 where day='20200401' and  hour='15';

5. 动态分区调整

关系型数据库中,对分区表 Insert 数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中,Hive 中也提供了类似的机制,即动态分区(Dynamic Partition),只不过,使用 Hive 的动态分区,需要进行相应的配置。

1)开启动态分区参数设置

(1)开启动态分区功能(默认 true,开启)

hive.exec.dynamic.partition=true

(2)设置为非严格模式(动态分区的模式,默认 strict,表示必须指定至少一个分区为静态分区,nonstrict 模式表示允许所有的分区字段都可以使用动态分区。)

hive (hive3)> set hive.exec.dynamic.partition.mode=nonstrict;

(3)在所有执行 MR 的节点上,最大一共可以创建多少个动态分区。默认1000

hive.exec.max.dynamic.partitions=1000

(4)在每个执行 MR 的节点上,最大可以创建多少个动态分区。该参数需要根据实际的数据来设定。比如:源数据中包含了一年的数据,即 day 字段有365 个值,那么该参数就需要设置成大于365,如果使用默认值100,则会报错。(4)在每个执行 MR 的节点上,最大可以创建多少个动态分区。该参数需要根据实际的数据来设定。比如:源数据中包含了一年的数据,即 day 字段有365 个值,那么该参数就需要设置成大于365,如果使用默认值100,则会报错。

hive.exec.max.dynamic.partitions.pernode=100

(5)整个 MR Job 中,最大可以创建多少个 HDFS 文件。默认100000

hive.exec.max.created.files=100000

(6)当有空分区生成时,是否抛出异常。一般不需要设置。默认 false

hive.error.on.empty.partition=false

2)案例实操

需求:将 dept 表中的数据按照地区(loc 字段),插入到目标表 dept_partition 的相应分区中。

(1)创建目标分区表

hive (hive3)> create table dept_no_par(dname string, loc string) partitioned by (deptno int) row format delimited fields terminated by '';
OK
Time taken: 0.185 seconds

(2)设置动态分区

hive (hive3)> set hive.exec.dynamic.partition.mode=nonstrict;

(3)导入数据

hive (hive3)>insert into table dept_no_par partition(deptno) select dname,loc,deptno from dept;
OK
Time taken: 0.062 seconds
Query ID = atguigu_20211219145313_ad874c4a-da69-43f2-a0bb-6e0e9ea0ce6c
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
2021-12-19 14:53:33,924 Stage-1 map = 0%, reduce = 0%
2021-12-19 14:53:41,638 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 2.93 sec
2021-12-19 14:53:51,240 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 4.7 sec
MapReduce Total cumulative CPU time: 4 seconds 700 msec
Ended Job = job_1639880318289_0007
MapReduce Jobs Launched: 
Stage-Stage-1: Map: 1  Reduce: 1  Cumulative CPU: 4.7 sec  HDFS Read: 14680 HDFS Write: 773 SUCCESS
Total MapReduce CPU Time Spent: 4 seconds 700 msec
OK
dname  loc  deptno
Time taken: 42.654 seconds

(4)查看目标分区表的分区情况

hive (hive3)>  show partitions dept_no_par;
OK
partition
deptno=10
deptno=20
deptno=30
deptno=40
deptno=70
Time taken: 0.39 seconds, Fetched: 5 row(s)

动态分区也可以这么写(3.0新增):

hive (hive3)> create table dept_no_par2(dname string, loc string) partitioned by (deptno int) row format delimited fields terminated by '';
OK
Time taken: 0.188 seconds
hive (hive3)> insert into table dept_no_par2 select dname,loc,deptno from dept;
hive (hive3)>  show partitions dept_no_par2;
OK
partition
deptno=10
deptno=20
deptno=30
deptno=40
Time taken: 0.137 seconds, Fetched: 4 row(s)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1387706.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

通过myBatis将sql语句返回的值自动包装成一个java对象(2)

1.之前我们是如何执行一个sql语句自动包装成一个java对象呢? 1.创建一个mapper.xml,定义 执行的语句名字 和 包装成什么类 2.在总的配置文件里申明这个mapper 3.在java里通过sqlSession执行mapper里定义好的内容 我们还可以使用另一种方法实现第三步。现…

小程序中使用微信同声传译插件实现语音识别、语音合成、文本翻译功能----文本翻译(三)

官方文档链接:https://mp.weixin.qq.com/wxopen/plugindevdoc?appidwx069ba97219f66d99&token370941954&langzh_CN#- 要使用插件需要先在小程序管理后台的设置->第三方设置->插件管理中添加插件,目前该插件仅认证后的小程序。 文本翻译…

Vue项目 css下载字体并引入使用

1.下载字体 下载字体:字体下载,字体大全,免费字体下载,在线字体|字客网字客网是全球知名的字体下载与分享网站,齐全的中文,日文,韩文,英文,图标,美术设计,毛笔,钢笔,手写,书法字体大全,提供找字体,字体识别,字体下载,在线字体预览,字体转换,字体设计等服务。…

华为设备端口镜像设置

核心代码: observe-port int 编号 int 编号 mirror to observe-port both | inbound | outbound #both:将镜像端口的入和出流量同时复制到观察者端口 #inbound:将镜像端口的入流量复制到观察者端口 #outbound:将镜像端口的出流量复制到观察者端口配置后可使出入端口…

新手必看:腾讯云服务器购买详细图文教程

腾讯云服务器购买流程很简单,有两种购买方式,直接在官方活动上购买比较划算,在云服务器CVM或轻量应用服务器页面自定义购买价格比较贵,但是自定义购买云服务器CPU内存带宽配置选择范围广,活动上购买只能选择固定的活动…

基于SSM的项目监管系统设计与实现

末尾获取源码 开发语言:Java Java开发工具:JDK1.8 后端框架:SSM 前端:采用JSP技术开发 数据库:MySQL5.7和Navicat管理工具结合 服务器:Tomcat8.5 开发软件:IDEA / Eclipse 是否Maven项目&#x…

基于SSM的驾校信息管理系统设计与实现

末尾获取源码 开发语言:Java Java开发工具:JDK1.8 后端框架:SSM 前端:Vue、HTML 数据库:MySQL5.7和Navicat管理工具结合 服务器:Tomcat8.5 开发软件:IDEA / Eclipse 是否Maven项目:是…

自动化测试成本高效果差,意义在哪?

自动化测试的成本高效果差?首先这个结论就太过武断了一些。 任何技术都需要放到适合的地方去使用,否则一定是达不到理想的效果的。举例大炮打蚊子,同样是成本高效果差,难道大炮就没有存在的意义了吗? 当然不是&#…

3.0.0 网络安全技术

一、端口安全 1、端口隔离 1.1 简介 以太交换网络中为了实现报文之间的二层隔离,用户通常将*不同的端口*加入*不同的VLAN*,实现二层广播域的隔离。只通过VLAN实现报文二层隔离,会浪费有限的VLAN资源,同时也只能实现基础的隔离操…

金和OA jc6 Upload 任意文件上传漏洞复现

0x01 产品简介 金和OA协同办公管理系统软件(简称金和OA),本着简单、适用、高效的原则,贴合企事业单位的实际需求,实行通用化、标准化、智能化、人性化的产品设计,充分体现企事业单位规范管理、提高办公效率的核心思想,为用户提供一整套标准的办公自动化解决方案,以帮助…

移动端开发进阶之蓝牙通讯(二)

移动端开发进阶之蓝牙通讯(二) 蓝牙广播是一种无线通讯技术,通过无线电波传输数据; 在蓝牙低功耗(BLE)协议中,广播通信是其重要组成部分,主要有两类使用场景: 单一方向的…

基本BGP配置试验 :配置 IBGP 和 EBGP

一、预习: BGP:Border Gateway Protocol 没有精妙的算法,但能承载大量的路由,它不生产路由,它是路由的搬运工 使用TCP做为传输层协议,端口号179,使用触发式路由更新 1. BGP路由…

喜讯!矩阵起源子公司通过“国家高新技术企业”认定,引领数据库行业科技创新!

近日,全国高新技术企业认定管理工作领导小组办公室,公布了《上海市认定机构2023年认定报备的第二批高新技术企业备案公示名单》,矩阵起源子公司矩智原力(上海)信息科技有限公司(以下简称“矩智原力”&#…

2024年【北京市安全员-C3证】复审考试及北京市安全员-C3证证考试

题库来源:安全生产模拟考试一点通公众号小程序 北京市安全员-C3证复审考试考前必练!安全生产模拟考试一点通每个月更新北京市安全员-C3证证考试题目及答案!多做几遍,其实通过北京市安全员-C3证模拟考试题很简单。 1、【多选题】《…

vue前端开发自学,插槽练习,同时渲染父子组件的数据信息

vue前端开发自学,插槽练习,同时渲染父子组件的数据信息! 如果想在slot插槽出口里面,同时渲染出来,来自父组件的数据,和子组件自身的数据呢。又有点绕口了。vue官方给的解决办法是。需要借助于,父组件的自定义属性。 …

利用低代码技术,企业怎样开拓数字化转型新路径?

近年来,随着技术的发展和市场竞争的加剧,企业数字化转型已成为一种趋势。许多企业已经完成了线上协作办公的初步转型,这主要得益于像钉钉、企微等发展完善的平台,只需将员工全部拉入这些平台,就能实现线上协作办公。 然…

2024java开发学习路线

文章目录 第一阶段【JAVA基础】第二阶段【数据库jdbc】第三阶段【JavaWeb】第四阶段【框架】第五阶段【微服务】第六阶段【常用中间件】第七阶段【查缺补漏】 别问,问就是Java已死!这是在2023年疫情解封后,市场经济低迷的情况下,有…

编码技巧:如何在Golang中高效解析和生成XML

编码技巧:如何在Golang中高效解析和生成XML 引言Golang中的XML基础解析XML文件生成XML文件错误处理和调试高级技巧和最佳实践总结 引言 在当今数据驱动的编程世界中,有效地处理各种数据格式是每个开发人员必备的技能之一。其中,XML&#xff…

快速高效处理长图:按指定高度切长图的方法,提升设计品质

在现代视觉传达设计中,长图作为一种常见的表现形式,被广泛应用于各种场景。如何快速高效地处理长图,使其符合设计要求和用户体验,成为设计师们面临的一大挑战。现在来看“办公提效工具”如何按指定高度切长图,提升设计…

Python: locals()详细解释

locals() 是一个内置函数,用于返回当前局部作用域的字典。这个字典包含了当前函数或模块中所有局部变量的名称和值。具体来说: locals()返回值:一个字典,包含了当前局部作用域的所有局部变量。 这个函数在不同的上下文中有不同的…