目录
1.hive简介
hive是构建在hadoop上的一个数据分析工具,底层hdfs存储数据,将数据映射成一张张数据表,本质是将hql转换成mapreduce
2.hive和数据库的区别
数据库是存储在本地文件系统,hive是存在hdfs,
数据库执行方式是Executor,hive是mapreduce
hive执行延迟和可扩展性高,能处理的数据规模更大
3.应用场景
日志分析:统计PV,UV
离线分析
4.hive架构
写hql通过客户端(hive shell,beline cli,jdbc,odbc,webui)方式(其中beline cli 转接到spark,安全性更高,如果直连metastore不安全,hiveserver2的方式安全)到Driver
Driver解析:校验表明列明是否存在
编译:编译成可执行的语法树
优化:RBO(基于规则的优化),CBO(基于代价的优化)
执行:把逻辑计划转换成物理计划,即mr
CBO:优化目标是在编译阶段计算出产生中间结果少的高效join顺序,从而减少查询时间和资源消耗
元数据存储在derby或者mysql中,hive表和hdfs文件的映射关系,hdfs的元数据存储在内存中,即nn
5.hive工作原理
表和hdfs文件路径会保存到metastore,建立表和数据的映射关系,当数据加载表时,hive会将其转换成mr或者spark程序,提交到yarn执行
6.hive的三种安装方式
内嵌deby,本地,远程模式(其他软件都可以通过metastore访问hive)
7.配置文件和启动
配置:hive-env.sh,hive-site core-site mysql驱动
启动:zkServer.sh start start-all.sh jobhistory
metastore前台启动:hive --service metastor 后台启动:nohup hive --service metastore >/dev/null 2>&1 &
hiveserver2前台启动:hiveserver2 后台:nohup hiveserver2> /dev/null 2>&1 &
客户端连接方式:hive(不常用) ,hiveserver2(不常用)
beline -u jdbc:hive2://node01:10000 -n root(beline方式最常用)
8.hive基础
drop database school if not exists school cascade 强制删除
创建外部表
create external table if not exists school
两种载入hdfs数据的方式:
location '路径'
load data inpath 路径 into table 表名
根据字符分割:
row format delimited fields terminated by '字符'
换行
LINES TERMINATD BY '\n'
hive只支持单字节分隔符加载文本数据
||会出现空列,用mr或者正则
row format serde 'org......RegexSerDe'
with serdeproperties(正则)
9.内外部表
内部表:hive管理的表,和hdfs数据关联,删除表会删除hdfs目录和文件,可以分区分桶
外部表:逻辑表,不会删hdfs文件,不支持分区分桶
10.载入导出数据
载入本地数据
load data local inpath '本地路径' (overwrite) into table 表名
overwrite:覆盖,不加overwrite为追加数据
导出查询结果数据到本地
insert overwrite local directory '本地路径' select * from t_user
创建hdfs目录 hdfs dfs -mkdir -p /yjx/export/user
导出数据到hdfs
insert overwrite directory 'hdfs路径' row format delimited fields terminated by ',' select * from t_user
表结构和数据同时导出
export table t_person to 'hdfs路径'
导出数据了,删除表后可以恢复
drop table t_person inport from 'hdfs路径'
11.hive高级-分区分桶
数据量大,要减少查询压力,所以分区分桶
分区针对的是数据的存储路径,分桶针对的是数据文件
静态分区-手动指定(数据不能出错)
创建表加上 partition by(grade int) 载入表加上partition(grade=1)
添加/查看/删除分区
添加分区
alter table t_student add if not exists partition(grade=1)
查看分区
show partition t_student
删除分区
alter table t_student drop partition(grade=1)
多分区
partition by(grade,int,clazz int)
动态分区
分桶
Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记录存放哪个桶
优势:方便抽样,提高join查询效率
设置参数
SET hive.enforce.bucketing=true;
SET mapred.reduce.tasks=-1;
clustered by(idcard) sorted by (username DESC)
INTO 分桶数 BUCKETS
载入数据(查询载入)
insert overwrite table t_citizen_bucket select * from t_citizen_e
12.数据抽样,事务,视图
块抽样
分桶抽样 随机且速度快
tablesample(BUCKET 1 OUT OF 64 ON idcard) ;
随机抽样
物化视图,真正创建表
create materialized view 表名 as 视图查询语句
13.一行多行互转
1一行变多行
select explode(split(types,"-")) from t_movie1;
侧视图
lateral view explode
(split(types,"-")) movie_type as type
2多行变一行
collect_set和collect_list list可重复 set去重
select id,name,
concat_ws(":",collect_set(type)) as type_set
from t_movie2
group by id,name;
14.窗口函数(移动,分析,取值)
定义:用于分析的函数,sum,count无法与列一起查询,故用窗口函数
partition by:OVER(PARTITION BY deptno) AS avgsal 部门分组,求薪资平均数
order by :RANK() OVER(PARTITIONBY deptno ORDERBY sal)AS salorder
按照部门分组,求平均薪资,组内薪资排序
order by 和sum一起用:
移动
分析
RANK() 间断 重复 12225
DENSE_RANK 不间断,重复12223
ROW_NUMBER() 不间断,不重复 12345(2,3可能同值)
取值
15.压缩存储
OLTP联机事务处理
OLAP联机分析处理
ORC,支持事务
Parquet 和spark兼容性好
16.hive优化
以后再写
17hive的GUI
hive可以连接idea