首页 > 其他分享 >Hive(七)分区表和分桶表

Hive(七)分区表和分桶表

时间:2024-10-10 10:33:44浏览次数:1  
标签:分桶 default partition hive dept 分区表 Hive day

分区表

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

分区表基本操作

引入分区表

dept_20200401.log
dept_20200402.log
dept_20200403.log

创建分区表

hive (default)> create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (day string)
row format delimited fields terminated by '\t';
  • 分区字段不能是表中已经存在的数据,可以将分区字段看作表的伪列

加载数据到分区表中

  • 数据准备

dept_20200401.log

10 ACCOUNTING 1700
20 RESEARCH 1800

dept_20200402.log

30 SALES 1900
40 OPERATIONS 1700

dept_20200403.log

50 TEST 2000
60 DEV 1900
  • 加载数据
hive (default)> load data local inpath 
'/opt/module/hive/datas/dept_20200401.log' into table dept_partition 
partition(day='20200401');
hive (default)> load data local inpath 
'/opt/module/hive/datas/dept_20200402.log' into table dept_partition 
partition(day='20200402');
hive (default)> load data local inpath 
'/opt/module/hive/datas/dept_20200403.log' into table dept_partition 
partition(day='20200403');

分区表加载数据时,必须指定分区

查询分区表中数据

  • 单分区查询
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';

增加分区

  • 创建单个分区
hive (default)> alter table dept_partition add partition(day='20200404');
  • 同时创建多个分区
hive (default)> alter table dept_partition add partition(day='20200405') partition(day='20200406');

删除分区

  • 删除单个分区
hive (default)> alter table dept_partition drop partition(day='20200406');
  • 同时删除多个分区
hive (default)> alter table dept_partition drop partition(day='20200404'), partition(day='20200405');

查看分区表有多少分区

hive> show partitions dept_partition;

查看分区表结构

hive> desc formatted dept_partition;

二级分区

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

创建二级分区表

hive (default)> create table dept_partition2(
             deptno int, dname string, loc string
             )
             partitioned by (day string, hour string)
             row format delimited fields terminated by '\t';

正常的加载数据

  • 加载数据到二级分区表中
hive (default)> load data local inpath 
'/opt/module/hive/datas/dept_20200401.log' into table dept_partition2 partition(day='20200401', hour='12');
  • 查询分区数据
hive (default)> select * from dept_partition2 where day='20200401' and hour='12';

分桶表

  • 对于一张表或者分区,Hive可以进一步组织成桶,也就是更为细粒度的数据范围
    划分
  • 分桶是将数据集分解成更容易管理的若干部分的另一个技术
  • 分区针对的是数据的存储路径;分桶针对的是数据文件

创建分桶表

  • 数据准备
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
  • 创建分桶表
create table stu_buck(id int, name string)
clustered by(id) 
into 4 buckets
row format delimited fields terminated by '\t';
  • 查看表结构
hive (default)> desc formatted stu_buck;
Num Buckets: 4
  • 导入数据到分桶表中,load的方式
hive (default)> load data inpath '/student.txt' into table stu_buck;
  • 查询分桶的数据
hive(default)> select * from stu_buck;
  • 分桶规则:Hive的分桶采用对分桶字段的值进行哈希,然后除以桶的个数求余的方式决定该条记录存放在哪个桶当中

分桶表操作需要注意的事项

  • reduce的个数设置为-1,让Job自行决定需要用多少个reduce 或者将reduce的个
    数设置为大于等于分桶表的桶数
  • 从hdfs中load数据到分桶表中,避免本地文件找不到问题
  • 不要使用本地模式

标签:分桶,default,partition,hive,dept,分区表,Hive,day
From: https://www.cnblogs.com/shihongpin/p/18454646

相关文章

  • Hive(六)JSON函数
    概念什么是JSONJSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成JSON是存储和交换文本信息的语法,类似XMLJSON比XML更小、更快,更易解析JSON语法数据在名称/值对中数据由,分开使用斜杠\来转义字符大括号{}保存对象......
  • Hive(五)常用函数
    Hive常用函数字符串函数返回值函数描述stringconcat(string/binaryA,string/binaryB…)对二进制字节码或字符串按次序进行拼接intinstr(stringstr,stringsubstr)查找字符串str中子字符串substr出现的位置intlength(stringA)返回字符串的长度int......
  • 基于springboot的Hive的网络电视剧收视率分析系统
    本网络电视剧收视率分析系统依托Java与SpringBoot技术,并结合Hive数据仓库,致力于为电视剧行业提供精准、全面的收视率分析服务。在系统设计上,充分考虑数据的海量性和复杂性。Java语言确保了系统各个模块的稳定运行和高效执行。SpringBoot框架则为系统提供了便捷......
  • 基于Python+Scrapy的高校岗位招聘和分析平台(源码+vue+hadoop+hive+部署文档+可视化大
    收藏关注不迷路!!......
  • python基于深度学习的短视频内容理解与推荐系统(源码+vue+hadoop+hive+部署文档+可视
    收藏关注不迷路!!......
  • SparkSQL与Hive查询不一致问题
    文章目录1.Hive版本2.问题背景3.问题现象4.原因分析1).分析原因可能是缓存2).发现文件存储特点3).子文件夹出现原因5.解决方式1).方法1修改配置2).方法2修改脚本6.总结1.Hive版本Hive1.2.1000.2.6.5.0-2922.问题背景交付项目上基本所有的脚本任务,都是使用h......
  • Hive3.1.3 环境搭建之初始化数据到Oracle
    1、新建配置文件hive-site.xml<?xmlversion="1.0"encoding="UTF-8"standalone="no"?><?xml-stylesheettype="text/xsl"href="configuration.xsl"?><configuration><property> <name>jav......
  • Hive数仓操作(七)
    一、Hive动态分区表1.动态分区与静态分区的区别分区定义:静态分区:在插入数据时,需要手动指定分区字段的值。动态分区:分区字段的值是根据数据中的某个字段自动生成的,用户只需指定分区字段的类型。数据加载方式:静态分区:可以通过LOADDATA和INSERT...SELECT加载数......
  • 基于springboot hive旅游数据的分析与应用
    基于springboothive旅游数据的分析与应用【085】代码代写程序代做代编网页爬虫脚本自动化安装调试项目设计技术和工具环境: 主用技术:JAVA、SpringBoot、MySQL、MyBatis、Vue、Nodejs等开发工具:IDEA、VisualStudioCode,没有vscode的也可以命令行运行。(运行前端)、Na......
  • HIVE优化系列之数据倾斜
    数据倾斜在hive表中进行一系列join关联时经常会出现数据倾斜问题,可以通过hint将小表进行广播,从而提高查询的执行效率。第一种hint方法:/*+BROADCAST(small_table)*/SELECT/*+BROADCAST(small_table)*/*FROMlarge_tableJOINsmall_tableONlarge_table.id=s......