栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hive的架构设计和SQL语句复习总结

Hive的架构设计和SQL语句复习总结

这里写目录标题
  • 一、Hive的架构设计和SQL语句复习总结
    • 1.1. 数据仓库
    • 1.2. Hive概念
    • 1.3. Hive架构
    • 1.3. 元数据
    • 1.4. 支持的SQL语法总结
  • 二、Hive的环境搭建和语法详解
    • 2.1. 环境搭建
      • 2.1.1. 依赖环境
      • 2.1.2. 版本选择
      • 2.1.3. 自带Derby版本
      • 2.1.4. 外置MySQL版本
      • 2.1.5. HiveServer2服务
      • 1.6. Hiveserver2 Web UI
    • 2.2. Hive SQL语法全操作
      • 2.2.1. 数据库操作
      • 2.2.2. 添加表
      • 2.2.3. 删除表
      • 2.2.4. 对表进行重命名
      • 2.2.5. 修改表
      • 2.2.6. hive分区表
      • 2.2.7. 各种常用查询显示命令
      • 2.2.8. load方式导入数据
      • 2.2.9. 利用insert关键字往表中插入数据
      • 2.2.10. like关键字使用
      • 2.2.11. 利用insert导出数据到本地或者hdfs
      • 2.2.12. 清空数据库表
      • 2.2.13. 查询数据
      • 2.2.14. 五种链接查询
    • 2.3. Hive的四种表
      • 2.3.1. 内部表
      • 2.3.2. 外部表
      • 2.3.3. 分区表
      • 2.3.4. 分桶表

一、Hive的架构设计和SQL语句复习总结 1.1. 数据仓库

数据仓库之父比尔•恩门(Bill Inmon)在1991年出版的"Building the Data Warehouse"(《建立数据仓库》)一书中所提出的定义被广泛接受。(简称:DW)(DM)

  • 数据仓库(Data Warehouse)是一个面向主题的(Subject Oriented)、集成的(Integrated)、相对稳定的(Non-Volatile)、反映历史变化(Time Variant)的数据集合,用于支持管理决策(Decision Making Support)。

  • 面向主题的(Subject Oriented): 组织数据的方式,按主题来进行组织(多张事实表 + 多张维度表)

  • 集成的(Integrated):把数据按照主题的方式,放置在一个数据仓库相对稳定的(Non-Volatile):基本不会改变的, 一出生就被固定了。所以数据仓库没有update和delete的需求反映历史变化(Time Variant):历史数据,每一条记录都是表示过去某个时刻的一条事实

  • 数据仓库的价值:用于支持管理决策(Decision Making Support)。

  • 管理多个不同主题的数据:一个主题就可以理解成是一张表,或者一类关系紧密的表

  • 数据仓库:存储数据,不在乎数据是什么格式,读模式数据存储系统。

  • 相对稳定:一般来说,往数据仓库Hive中存储的数据,都是一些log数据,都是不会再去做修改的数据

  • 支持管理决策:查询分析得到的结果具有很大的价值

  • 数据仓库:放数据的,放历史数据的,标准化的结构数据,文本数据,进行查询分析, 支持SQL中的select语法!

1.2. Hive概念

官网解释:http://hive.apache.org

1. Hive由Facebook实现并开源(google, yahoo),hive是facebook(mapreuce开发效率低)
2. Hive是基于Hadoop的一个数据仓库工具
3. Hive存储的数据其实底层存储在HDFS上
4. Hive将HDFS上的结构化的数据映射为一张数据库表,类似于excel或者msyql的表
5. Hive提供HQL(Hive SQL)查询功能
6. Hive的本质是将SQL语句转换为MapReduce任务运行,使不熟悉MapReduce的用户很方便地利用HQL处理和计算HDFS上的结构化的数据,适用于离线的批量数据计算(提高了开发效率)
7. Hive使用户可以极大简化分布式计算程序的编写,而将精力集中于业务逻辑

总结
Hive依赖于HDFS存储数据,Hive将HQL转换成MapReduce执行,所以说Hive是基于Hadoop的一个数据仓库工具,实质就是一款基于HDFS的MapReduce计算框架,对存储在HDFS中的数据进行分析和管理。

1.3. Hive架构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tfm4FJ70-1633923115698)(assets/image-20210818122452585.png)]

Hive内部四大组件:Driver驱动器,编译器Compiler,优化器Optimizer,执行器Executor

Driver组件完成HQL查询语句从词法分析,语法分析,编译,优化,以及生成逻辑执行计划的生成。生成的逻辑执行计划存储在HDFS中,并随后由MapReduce调用执行。

1.3. 元数据

HDFS中的元数据:(大文件的存储文件:分散存储 + 冗余存储)

  1. 目录树空间
  2. 每个大文件都对应到那些小文件呢
  3. 一个数据块有3个副本,那么这个数据块的三个副本到底存储在哪三个节点呢?

Hive的元数据:(把存储在HDFS上的结构化数据,想象成一张二维表格)

  1. 表名(表和数据文件之间的映射关系)
  2. 字段定义(由那些字段组成的)
  3. 行列分隔符(既然把文件想象成二维表格,那么行与行之间的界限,列与列的界限)
  4. 表中的数据(存储在HDFS上的文件,到底那张表对应到文件呢?)

Hive的元数据的管理,是借助于一个关系型数据库!(企业最佳实践:MySQL) hive自带一个嵌入型的小型RDBM:derby

1.4. 支持的SQL语法总结

支持的语法:

1、select * from db.table1
2、select count(distinct uid) from db.table1
3、支持select、union all、join(left、right、full join)、like、where、having、limit等标准语法
4、支持各种普通函数,聚合函数、表格函数
5、支持json解析
6、支持函数自定义:UDF(User Defined Function)/ UDAF/UDTF
7、不支持update和delete,不管新老版本都不支持,而且没有支持的必要!
8、hive虽然支持in/exists(老版本不支持),但是hive推荐使用semi join的方式来代替实现,而且效率更高。
9、支持case … when …

不支持的语法:

1、支持 and多条件join,不支持 or 多条件join
    select a.*, b.* from a join b on a.id = b.id and a.name = b.name; √√√√√
    select a.*, b.* from a join b on a.id = b.id or a.name = b.name;  xxxxx
    
2、默认情况下,不支持笛卡尔积
    select a.*, b.* from a, b;

你的使用原则:绝大部分的SQL,常用的SQL语法,都是支持的!

二、Hive的环境搭建和语法详解
  1. 必须安装好 hadoop 集群! 不管是单机的,伪分布式的,分布式的, HA的, 联邦的。
  2. 还要安装一个MySQL(只要有一个能用的能链接上的MySQL就可以了。不管在哪里)
    • 权限问题: access denied
    • 解决方案: 远程连接权限的问题
2.1. 环境搭建 2.1.1. 依赖环境

Hive不能独立运行!需要依赖于一个RDBMS(帮它存储元数据)和一个文件系统(帮它存储真实数据)

  • 必须要准备一个RDBMS(也可以是hive自带的derby内嵌数据库,不适用于生产环境,推荐使用MySQL)
  • 必须要准备一个Hadoop(其实也不必,也可以直接使用linux文件系统来存储就行了)
2.1.2. 版本选择

选择任何技术的版本要考虑哪些方面:功能,稳定性,可维护性,兼容性…

策略:要考虑不新不旧的稳定版本

当前 Hive 的主流版本有两个

  • hive-1.x,其中使用比较多的是hive-1.2.x的版本,最后一个稳定版本是hive-1.2.2,如果使用这个版本,你的hive将来没法集成spark。
  • hive-2.x,现行主流的hive使用版本,现行稳定的hive-2.x版本中的,我们选择使用hive-2.3.6
    • apache-hive-2.3.6-bin.tar.gz 安装包
    • apache-hive-2.3.6-src.tar.gz 源码包
2.1.3. 自带Derby版本

用途:测试,学习,快速使用

1、上传安装包

put -r apache-hive-2.3.6-bin.tar.gz

2、解压安装包

[bigdata@bigdata05 ~]$ tar -zxvf ~/soft/apache-hive-2.3.6-bin.tar.gz -C ~/apps/

3、进入到bin目录,运行hive脚本

[bigdata@bigdata05 ~]$ cd apps/apache-hive-2.3.6-bin/bin
[bigdata@bigdata05 bin]$ ./hive

4、测试使用

show databases;
create database testdb;
use testdb;
create table student(id int, name string);
insert into table student values ('1','huangbo'), ('2','xuzheng'),('3','wangbaoqiang');
select * from student;
2.1.4. 外置MySQL版本

用途:适用于企业级生产环境

第一步:准备好MySQL 和 Hadoop

请参考官网安装,或者自行安装MySQL,或者一个可用的MySQL。不管你的MySQL在哪里,只要Hive能连接上即可。如果Hive和MySQL没有安装在同一个服务器节点中,通常企业环境也都不是安装在同一个节点,一定要记得给Hive开启MySQL的远程链接权限。

要点:
1、有一个能用的MySQL就行
2、给这个MySQL配置远程连接权限

第二步:上传安装包

将 apache-hive-2.3.6-bin.tar.gz 到安装Hive的服务器节点中。

使用你知道的方式上传这个安装包到你的服务器上。或者使用wget命令直接现在去下载也可。或者给centos安装lrzsz命令通过自行rz命令来进行上传,或者通过filezilla这种ftp工具也行。

第三步:解压安装包到对应的Hive安装目录中

[bigdata@bigdata05 ~]$ tar -zxvf ~/soft/apache-hive-2.3.6-bin.tar.gz -C ~/apps/

第四步:修改配置文件

[bigdata@bigdata05 ~]$ cd ~/apps/apache-hive-2.3.6-bin/conf
[bigdata@bigdata05 ~]$ touch hive-site.xml

注意:该文件默认是没有的。一般自己创建。当然也可以从hive-default.xml更改而来,不推荐。

[bigdata@bigdata05 ~]$ vi hive-site.xml

在这个新创建的配置文件中加入如下截图中的内容即可:


    
        javax.jdo.option.ConnectionURL
        jdbc:mysql://bigdata02:3306/myhivemetadb236?createDatabaseIfNotExist=true&verifyServerCertificate=false&useSSL=false
        
        JDBC connect string for a JDBC metastore
        
        
    
    
        javax.jdo.option.ConnectionDriverName
        com.mysql.jdbc.Driver
        
        Driver class name for a JDBC metastore
    
    
        javax.jdo.option.ConnectionUserName
        root
        
        username to use against metastore database
    
    
        javax.jdo.option.ConnectionPassword
        QWer_1234
        
        password to use against metastore database
    
    
        hive.metastore.warehouse.dir
        /user/hive236/warehouse236
        hive default warehouse, if nessecory, change it
        
    

第五步:加入MySQL驱动

一定要记得加入MySQL驱动包 mysql-connector-java-5.1.40-bin.jar 该jar包放置在hive的安装根路径下的lib目录中,毕竟hive要读写MySQL。

[bigdata@bigdata05 ~]$ cp ~/soft/mysql-connector-java-5.1.40-bin.jar ~/apps/apache-hive-2.3.6-bin/lib

第六步:复制Hadoop集群的配置文件

一定要记得把Hadoop集群中的 core-site.xml 和 hdfs-site.xml 两个配置文件都放置在Hive安装目录下conf目录中。

[bigdata@bigdata05 ~]$ cp $HADOOP_HOME/etc/hadoop/core-site.xml ~/apps/apachehive-2.3.6-bin/conf
[bigdata@bigdata05 ~]$ cp $HADOOP_HOME/etc/hadoop/hdfs-site.xml ~/apps/apachehive-2.3.6-bin/conf

第七步: 安装完成,配置环境变量

vi ~/.bashrc

添加以下两行内容:

export HIVE_HOME=/home/bigdata/apps/apache-hive-2.3.6-bin
export PATH=$PATH:$HIVE_HOME/bin

最后不要忘记让环境变量配置生效:

[bigdata@bigdata05 ~]$ source ~/.bashrc

第八步:验证Hive安装

[bigdata@bigdata05 ~]$ hive --service version

第九步:初始化元数据库

注意:当使用的hive是1.x之前的版本,不做初始化也是OK的,当Hive第一次启动的时候会自动进行初始化,只不过会不会生成足够多的元数据库中的表。在使用过程中会慢慢生成。但最后进行初始化。如果使用的2.x版本的Hive,那么就必须手动初始化元数据库。使用命令:

[bigdata@bigdata05 ~]$ schematool -dbType mysql -initSchema

切记:初始化操作只需要做一次。如果你使用了一段时间,再次执行了这个命令,那么你就又得到了一个全新的Hive。

执行成功的日志:

[bigdata@bigdata04 hadoop]$ schematool -dbType mysql -initSchema
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/bigdata/apps/apache-hive-2.3.6-bin/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/bigdata/apps/hadoop2.7.7/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
metastore connection URL: jdbc:mysql://bigdata02:3306/hivemetadb2003?createDatabaseIfNotExist=true&verifyServerCertificate=false&useSSL=false
metastore Connection Driver : com.mysql.jdbc.Driver
metastore connection User: root
Starting metastore schema initialization to 2.3.0
Initialization script hive-schema-2.3.0.mysql.sql
Initialization script completed
schemaTool completed

第十步:启动Hive 客户端

[bigdata@bigdata05 ~]$ hive
[bigdata@bigdata05 ~]$ hive --service cli

第十一步:退出Hive

hive> quit;
hive> exit;

第十二步:注意事项

在使用 Hive 的时候一定要确保三件事:

  1. 确保 RDBMS元数据 启动好了。
  2. 确保 HDFS 启动好了。能正常运行
  3. 确保 YARN 集群启动好了,能正常运行
2.1.5. HiveServer2服务

部署方法如下:

第一:修改hadoop集群的hdfs-site.xml配置文件:加入一条配置信息,表示启用webhdfs


    dfs.webhdfs.enabled
    true

第二:修改hadoop集群的core-site.xml配置文件:加入两条配置信息:表示设置hadoop集群的代理用户


    hadoop.proxyuser.bigdata.hosts
    *


    hadoop.proxyuser.bigdata.groups
    *

修改的Hadoop集群中的 core-site.xml 和hdfs-site.xml,并且一定要记得,所有节点都的修改。重启Hadoop集群。

第三:启动Hiveserver2服务

[bigdata@bigdata05 ~]$ nohup hiveserver2 1>/home/bigdata/logs/hiveserver.log 2>/home/bigdata/logs/hiveserver.err &

第四步:启动beeline客户端

beeline -u jdbc:hive2://bigdata03:10000 -n bigdata
1.6. Hiveserver2 Web UI

Hive从2.0 版本开始,为 HiveServer2 提供了一个简单的 WEB UI 界面,界面中可以直观的看到当前链接的会话、历史日志、配置参数以及度量信息。

修改: $HIVE_HOME/conf/hive-site.xml 配置文件:


    hive.server2.webui.host
    bigdata03


    hive.server2.webui.port
    10002

可以参考官网:https://cwiki.apache.org/confluence/display/Hive/Setting+Up+HiveServer2#SettingUpHiveServer2-WebUIforHiveServer2

重启 Hiveserver2,访问 Web UI:http://bigdata05:10002

2.2. Hive SQL语法全操作 2.2.1. 数据库操作

创建数据库:

create database mydb;
create databse if not exists mydb comment 'create my db named dbname' with dbproperties ('a'='aaa','b'='bbb')
show create database mydb; 				-- 查看创建语句
desc database extended mydb;			-- 查询数据库的详细信息
show databases;							-- 列出所有的数据库
drop database mydb;						-- 删除数据库
drop database if exists mydb cascade;	-- 删除数据库
use mydb;								-- 使用数据库
show tables;							-- 查看数据库的数据表
show tables in mydb;					-- 查看数据库的数据表
2.2.2. 添加表

创建内部表(Managered_Table)

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

创建外部表(External_Table)

create external table mingxing(id int, name string, sex string, age int, department string) 
row format delimited fields 
terminated by ',' 
location '/root/hivedata'; 
-- 注意:创建外部表的时候指定location的位置必须是目录,不能是单个文件  

创建分区表(Partition_Table)

create table mingxing(id int, name string, sex string, age int, department string) 
partitioned by (city string) 
row format delimited fields 
terminated by ',';
-- 注意:partition里的字段不是能是表中声明的字段  

创建分桶表(Bucket_Table)

create table mingxing(id int, name string, sex string, age int, department string) 
clustered by(id) 
sorted by(age desc) into 4 buckets 
row format delimited 
fields terminated by ',';
2.2.3. 删除表
drop table [if exists] mingxing;
2.2.4. 对表进行重命名
alter table mingxing rename to student;
2.2.5. 修改表
alter table mingxing add columns (province string);						-- 增加字段
alter table mingxing add columns (province string, xxx bigint);			-- 增加字段
drop(不支持),替代方案可以使用replace										-- 删除字段
alter table mingxing change age newage string;							-- 修改字段
alter table mingxing change age newage string first|after id;			-- 修改字段
alter table mingxing replace columns(id int, name string, sex string);	-- 替换字段 
2.2.6. hive分区表
alter table mingxing add partition(city='beijing');											-- 增加分区
alter table mingxing add partition(city='beijing') partition(city='tianjin');				-- 增加分区
alter table mingxing drop if exists partition(city='beijing');								-- 删除分区
alter table mingxing partition(city='beijing') set location '/home/hadoop/data/beijing';	-- 修改分区数据路径
2.2.7. 各种常用查询显示命令
show databases; 			-- 查看库:
show create database mydb;	-- 查看建库语句
show tables;				-- 查看表
show tables in mydb;		-- 查看表
show create table mingxing;	-- 查看建表语句
show functions;				-- 查看内置函数库
show partitions mingxing;	-- 查看表的字段
desc extended mingxing;		-- 查看表的详细信息
desc formatted mingxing;	-- 查看表的格式化了之后的详细信息
2.2.8. load方式导入数据
load data local inpath './student.txt' into table mingxing;								-- 导入本地相对路径的数据
load data local inpath './student.txt' overwrite into table mingxing;					-- 导入本地相对路径的数据 覆盖导入
load data local inpath '/root/hivedata/student.txt' into table mingxing;				-- 导入本地绝对路径数据
load data inpath '/root/hivedata/student.txt' into table mingxing;						-- 导入HDFS上的简便路径数据
load data inpath 'hdfs://hadoop01:9000/root/hivedata/student.txt' into table mingxing;	-- 导入HDFS上的全路径模式下的数据
2.2.9. 利用insert关键字往表中插入数据
insert into table mingxing values(001,'huangbo','male',50,'MA');	-- 单条数据插入

单重插入模式

insert into table student select id,name,sex,age,department from mingxing; -- 注意:查询出的字段必须是student表中存在的字段

多重插入模式

from mingxing
insert into table student1 select id,name,sex,age
insert into table student2 select id,department;
from mingxing2
insert into table student1 partition(department='MA') select id,name,sex ,age where department='MA'
insert into table student1 partition(department='CS') select id,name,sex ,age where department='CS';

静态分区插入

load data local inpath '/root/hivedata/student.txt' into table student partition(city='henan');

动态分区插入

create table student(name string, department string) partitioned by (id int)		-- student表字段:name,department, 分区字段是id
.....
insert into table student partition(id) select name,department,id from mingxing2;	-- 查询字段是:name,department,id,分区字段
-- 注意:动态分区插入的分区字段必须是查询语句当中出现的字段中的最后一个  

CTAS(create table … as select …) 直接把查询出来的结果存储到新建的一张表里

create table student as select id,name,age,department from mingxing;
-- 注意:自动新建的表中的字段和查询语句出现的字段的名称,类型,注释一模一样
2.2.10. like关键字使用
create table student like mingxing;		-- 仿造表
2.2.11. 利用insert导出数据到本地或者hdfs

单模式导出数据到本地

insert overwrite local directory '/root/outputdata' select id,name,sex,age,department from mingxing;

多模式导出数据到本地

from mingxing
insert overwrite local directory '/root/outputdata1' select id, name
insert overwrite local directory '/root/outputdata2' select id, name,age

简便路径模式导出到hdfs

insert overwrite directory '/root/outputdata' select id,name,sex,age,department from mingxing;

全路径模式查询数据到hdfs

insert overwrite directory 'hdfs://hadoop01:9000/root/outputdata1' select id,name,sex,age,department from mingxing;
2.2.12. 清空数据库表
truncate table mingxing;
2.2.13. 查询数据

基本查询

select * from mingxing join student where ... group by ... order by ... limit...  

查询全局有序数据

select * from mingxing order by age desc , id asc;

如果数据量过大,我们采用局部排序的方式:

set mapred.reduce.tasks=3;
set mapreduce.job.reduces=3;
select * from mingxing sort by id asc;

分桶查询:

set hive.enforce.bucketing = true;
select * from mingxing distribute by sex;

查询排序的分桶数据

select * from mingxing cluster by id sort by id desc, age asc;
2.2.14. 五种链接查询

内连接inner join:

select student.*, mingxing.* from student join mingxing on student.id = mingxing.id
select student.*, mingxing.* from student inner join mingxing on student.id = mingxing.id

左外链接left outer join:

select student.*, mingxing.* from student left join mingxing on student.id = mingxing.id
select student.*, mingxing.* from student left outer join mingxing on student.id = mingxing.id

右外链接right outer join

select student.*, mingxing.* from student right join mingxing on student.id = mingxing.id
select student.*, mingxing.* from student right outer join mingxing on student.id = mingxing.id

全外链接full outer join

select student.*, mingxing.* from student full join mingxing on student.id = mingxing.id
select student.*, mingxing.* from student full outer join mingxing on student.id = mingxing.id

in/exists的hive高效实现left semi join:

select student.*, mingxing.* from student left semi join mingxing on student.id = mingxing.id;

等同于:

select student.* from student where student.id in(select distinct id from mingxing);

数据格式

95001,李勇,男,20,CS
95002,刘晨,女,19,IS
95003,王敏,女,22,MA
95004,张立,男,19,IS
95005,刘刚,男,18,MA
95006,孙庆,男,23,CS
95007,易思玲,女,19,MA
95008,李娜,女,18,CS
95009,梦圆圆,女,18,MA
95010,孔小涛,男,19,CS
95011,包小柏,男,18,MA

SQL语句:

create database if not exists exercise_db;
use exercise_db;
drop table if exists exercise_student;
create table exercise_student(id int, name string, sex string, age int, department string) row format delimited fields terminated by ',';
load data local inpath "/home/bigdata/students.txt" into table exercise_student;
select * from exercise_student;
2.3. Hive的四种表

hive表有一个映射关系:一张表可能存储和管理了很多个数据文件!

hive的表和 数据文件到底是怎么映射的? select count(*) fro table;

table 和HDFS的目录

数据仓库的默认路径:/user/hive/warehouse/
库路径:nxde1.db
表路径:nx_student
数据文件:stduent.txt

当前表的数据存储目录的完整路径:
/user/hive/warehouse/nxde1.db/nx_student/stduent.txt

映射关系:
表:nx_student
HDFS目录:/user/hive/warehouse/nxde1.db/nx_student/
2.3.1. 内部表
create table nx_student()

你创建表的时候,默认该表的数据,就使用默认的组织方式,来存储在默认的仓库路径

2.3.2. 外部表

你创建表的时候,其实,数据已经在HDFS了,但是不是在你的默认目录,需要在创建表的时候指定目录:

create external table nx_student() row .... location "/aa/bb/ccc"

删除表的时候:

  1. 内部表:删除元数据 也删除数据文件
  2. 外部表:删除元数据, 不删除数据文件
2.3.3. 分区表
数据仓库的默认路径:/user/hive/warehouse/
库路径:nxde1.db
表路径:nx_student
分区:city=beijing city=shanghai
数据文件:stduent.txt

当前表的数据存储目录的完整路径:
/user/hive/warehouse/nxde1.db/nx_student/city=shanghai/stduent.txt
create table student_ptn(id int, name string) partitioned by(age int)

要点:分区字段一定不能和表一样,但是在使用的时候,分区字段和表字段没有不同!

以前,数据直接放在表目录中,现在,在表目录中,再创建文件夹,每个文件夹包管了这个表的一部分数据。也就意味着:一张表的数据,可以被分门别类的进行管理

select * from student_ptn where city = beijing;
select * from student_ptn where date > 2020-08-21; 效果:扫描3个分区即可

生产环境中,绝大部分的事实表,都是分区表(大部分的分区字段,也都是 date 类型:按天分区的概率是最大的)

2.3.4. 分桶表

分区是按照文件夹的形式来组织区分

分桶是按照文件的形式来组织区分(分桶的规则:和默认的mapreduce的HashPartitioner 一模一样!)

create table student_bck(id int, name string, age int) clustered by (name)
sorted by (age desc) into 3 buckets;

这张表中的数据,按照name字段划分成三个文件来进行管理,每个文件中的数据会按照age降序排序规则就是跟MR中的默认hash散列组件的逻辑一样的

作用:

1、分区表:用来提交查询效率的。
    企业最佳实践:
    1、如果遇见大量的SQL中,会按照某个字段来进行过滤。那么建表的时候。就应该要按照这个字段来作为分区字段字段建表
    2、如果你的数据基本上,只会分析增量数据,不会分析全量数据,那么就应该按照时间来进行分区

1、分桶表:也是用来提高查询效率 + 用来进行抽样
    企业最佳实践:
    1、如果某一张表的某个字段经常被当做是一个join查询的条件,就应该要建分桶表进行优化
    select a.*, b.* from a join b on a.id = b.id;
    a表和b表都要分桶,而且分桶规则要一样。(分桶字段一致,分桶个数成倍数关系)

数仓:事实表 和 维度表

查询: 事实表 join 维度表

不管是分区还是分桶,都会产生数据倾斜!

  1. hive中的数据倾斜 3-5
  2. spark中的数据倾斜 11种

选择机制:

  1. 如果你现在还选择使用hive,默认你是接受使用MR作为引擎的
  2. Hive On Spark 不是想象中那么稳定
  3. SparkSQL是完全兼容hive的。

hive + sparkSQL

DAG执行引擎: spark tez

引进:OLAP引擎

  • clickhouse kylin druid kudu doris …>
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312689.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号