Review Hive 概念、意义、特性、缺点、内部组成架构、安装部署、访问方式、数据库的基本操作、数据表基本操作、自定义函数、UDF、修改表信息、表数据加载、数据导出等等

Hive基本概念

是一个基于hadoop的数据仓库工具,可以将结构化数据映射成一张数据表,并提供类SQL的查询功能。

Hive的意义是什么

背景:hadoop是个好东西,但是学习难度大,成本高,坡度陡。
意义(目的):降低程序员使用hadoop的难度。降低学习成本。
 

Hive可以对数据进行存储与计算

存储使用HDFS存储
计算使用MapReduce进行计算。

Hive的特性

1、扩展性 : Hive的扩展性与集群的扩展性相同
2、延展性:Hive支持用户自定义函数,编写符合自己业务需求的函数。
3、容错性:节点出现问题(宕机/断电)SQL仍可完成执行

Hive缺点

每次的执行开销较大,任务运行时间较长,延时较高。

Hive的内部组成架构

             

元数据:描述数据的数据(属性)
表名称、字段名,字段的数据类型。
内部执行流程
解释器 -> 编译器 -> 优化器 -> 执行器

数据格式:Hive中没有定义专门的数据格式 数据格式部分自定义:

列分隔符(通常为空格、”\t”、”\x001″)
行分隔符(”\n”)
读取文件数据的方法(Hive 中默认有三个文件格式 TextFile,SequenceFile 以及 RCFile)。

Hive在加载数据的过程就是拷贝数据的过程,不需要特定的转化。不会对数据本身进行任何修改,甚至不会对数据 进行扫描。

Hive 中不支持对数据的改写和添加(对文本内数据的添加)

Hive 在加载数据的过程中不会对数据中的某些 Key 建立索引。所以Hive不适合在线数据查询(要求相应速度快)

总结:hive具有sql数据库的外表,但应用场景完全不同,hive只适合用来做批量数据统计分析。

hive支持的数据格式

可支持Text,
SequenceFile,
ParquetFile,
ORC格式
RCFILE等

Hive元数据

DB、数据库
Table,内部表
External Table,外部表
Partition,分区
Bucket,分桶
 

Hive安装部署

1、derby版hive直接使用
每个节点自己维护自己的元数据库(derby),导致多个节点之间元数据不共享。
2、mysql共享hive元数据
2.1安装mysql
mysq不推荐使用RPM安装,建议使用yum进行安装
yum install -y mysql mysql-server mysql-devel
/etc/init.d/mysqld start
配置远程连接(root 只用123456密码)
grant all privileges on . to 'root'@'%' identified by '123456' with grant option;
配置mysql用户 root 密码是123456
update user set password=password('123456') where user='root';
2.2 安装HIve
现在第一个节点解压hive的安装包。
在解压后的目录conf中修改配置文件
hive-env.sh
配置HADOOP_HOME
配置HIVE_CONF_DIR
hive-site.xml
添加连接Mysql的配置
将解压后的文件夹拷贝到其他节点
配置环境变量
将mysql-connector-java-5.1.38.jar 上传到HIVE的lib目录下。
 

Hive的访问方式

1、在Hive客户端,配置hive到环境变量的前提下,在节点的任意位置 直接数据hive + 回车

2、
启动hiveserver2 服务
hive --service hiveserver2
进行连接
进入beelin的shell窗口
连接hiveserver2服务
!connect jdbc:hive2://node001:10000
输入root 和密码 123456
0: jdbc:hive2://node001:10000> 0: jdbc:hive2://node001:10000>
0: jdbc:hive2://node001:10000> show databases;
+----------------+--+ | database_name | +----------------+--+ | default | | myhive | +----------------+--+

Hive传选项

hive -e '操作命令'
hive -e 'show databases;'
hive -f 文件名(文件内是操作命令)
hive -f test.sql
 

数据库 的基本操作

1、数据库的增删改查
增 : create database [ if not exists ] myhive ;
删 : drop database myhive ; (数据库内没有表可以删除,有表不能删除)
改 :数据库不允许修改
查 :show databases;
查看详细信息:
desc database myhive2;
desc database extended myhive2;
数据库的切换:
use myhive(数据库名);

hive的数据库、表、分区在HDFS的表现形式是文件夹

数据库的默认路径:/user/hive/warehouse

自定义hive数据库的路径:create database myhive2 location '/myhive2

数据 的基本操作(增删改查)

创建基本数据表(内部表):


create table tableName(字段名称 字段类型,字段名称 字段类型)


create table tableName(字段名称 字段类型,字段名称 字段类型)
ROW FORMAT DELIMITED IELDS TERMINATED BY char (char分隔符)
指定数据中字段与字段的分隔符 ‘\t’ 或 ',' 或 '|' 或其他


创建外部数据表:


create EXTERNAL table tableName(字段名称 字段类型,字段名称 字段类型)


建外部表需要指定数据的存储路径。通过LOCATION进行指定。

内部表与外部表的区别:

在删除内部表时:内部表删除将表的元数据和数据同时删除。
在删除外部表时:外部表的元数据被删除,数据本身不删除。

删除表

drop table tablename;

修改表

alter tablename ***

查询表

show tables;

desc tablename;

Hive的数据类型

基本数据类型
INT BIGINT FLOAT DOUBLE DEICIMAL STRING VARCHAR CHAR BINARY TIMESTAMP DATE INTERVAL ARRAY
复杂数据类型
MAP STRUCT UNION

create table stu3 as select * from stu2; 复制数据复试表结构

create table stu4 like stu2; 不复制数据复试表结构。

加载数据

从linux中加载数据到hive
load data local inpath ‘数据路径’ into table 表名;
从hdfs中加载数据到hive,并覆盖
load data inpath ‘数据路径’ overwrite into table 表名;

外部表

create external table techer (t_id string,t_name string) row format delimited fields terminated by '\t';
加载数据
load data local inpath '/export/servers/hivedatas/techer .csv' into table techer ;
在hdfs查看表中的数据
hadoop fs -ls /user/hive/warehouse/myhive.db/techer
在hive中查询
select * from techer
删除数据表techer
drop table techer;
再次查看
hadoop fs -ls /user/hive/warehouse/myhive.db/techer(数据依然存在)

内部表

create table student(t_id string,t_name string) row format delimited fields terminated by '\t';
加载数据
load data local inpath '/export/servers/hivedatas/student .csv' into table student;
在hdfs查看表中的数据
hadoop fs -ls /user/hive/warehouse/myhive.db/student
在hive中查询
select * from student
删除数据表techer
drop table student;
再次查看
hadoop fs -ls /user/hive/warehouse/myhive.db/student(数据不存在)

分区表

常见的分区规则:按天进行分区(一天一个分区)


创建分区表的语句
create table score(s_id string,c_id string,s_score int) partitioned by (month string) row format delimited
fieldsterminated by '\t';
create table score2 (s_id string,c_id string,s_score int) partitioned by (year string,month string,day string)
row formatdelimited fields terminated by '\t';
数据加载
load data local inpath '/opt/hive/score.csv' into table score partition (month='201806');
load data local inpath '/opt/hive/score.csv' into table score2 partition(year='2018',month='06',day='02');

特别强调:

分区字段绝对不能出现在数据表以有的字段中。

作用:

将数据按区域划分开,查询时不用扫描无关的数据,加快查询速度。

分桶表

是在已有的表结构之上新添加了特殊的结构
开启hive的桶表功能
set hive.enforce.bucketing=true;
设置桶(reduce)的个数
set mapreduce.job.reduces=3;
建分桶表
create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format
delimited fields terminated by '\t';
创建基本表
create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by
'\t';
基本表添加数据
load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;
在基本表中查询数据插入到分桶表
insert overwrite table course select * from course_common cluster by(c_id);
确认分桶内的数据
[root@node01 hive]# hadoop fs -cat /user/hive/warehouse/course/000000_0 03 英语 03 [root@node01 hive]#
hadoop fs -cat /user/hive/warehouse/course/000001_0 01 语文 02 [root@node01 hive]# hadoop fs -cat
/user/hive/warehouse/course/000002_0 02 数学 01
 

特别强调:

分桶字段必须是表中的字段。

分桶逻辑:

对分桶字段求哈希值,用哈希值与分桶的数量取余,余几,这个数据就放在那个桶内。

分桶的作用和好处

1、对于join的需求,能够起到优化加速的作用。(前提是,join字段设置为分桶字段)

2、用于数据取样(获取/提取数据样本)

将数据编号作为分桶字段。这样每个桶内各种“级别”的数据都有。

Hive的自定义函数

函数查看
show functions;
show function 函数名 ;
desc function extended upper;
UDF UDAF UDTF
一进一出 多进一出 一进多出

UDF函数

1、创建一个class 继承UDF
2 编写evaluate函数,在这里编写业务需求需要的代码
3 打成jar包,并上传
4 将jar包添加到hive
在hive shell 内 add jar 路径+jar包
5 创建临时函数(永久的函数将temporary 删掉)
create temporary function 新的函数的名称 as 业务代码所在的包名-类名;
6 调用 验证;

Hive通过reflect调用java方法

1、使用纯java代码编写业务逻辑,并打包上传
2、将jar包添加到hive
在hive shell 内 add jar 路径+jar包
3、调用
select reflect (‘参数一’,‘参数二’,‘参数三’)
参数一 包名-类名
参数而 方法名
参数三 需要计算的数据
 

修改表信息

修改表明
alter table old_table_name rename to new_table_name;
查看表结构
desc tablename;
添加列
alter table score5 add column (mycol string, mysco string);
 

Hive表数据加载

五种情况
1、直接向分区表中插入数据
insert into table score3 partition(month ='201807') values ('001','002','100');
2、通过查询插入数据
(linux ) load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score
partition(month='201806');
(HDFS) load data inpath '/export/servers/hivedatas/score.csv' overwrite into table score
partition(month='201806');
3、多插入模式
from score
insert overwrite table score_first partition(month='201806') select s_id,c_id
insert overwrite table score_second partition(month = '201806') select c_id,s_score;
4、查询语句中创建表并加载数据(as select)
create table score5 as select * from score;
5、创建表时通过location指定加载数据路径
create external table score6 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t'
location '/myscore6';
 

Hive数据的导出

7种方法
1、将查询的结果导出到本地
insert overwrite local directory '/export/servers/exporthive/a' select * from score;
2、将查询的结果格式化导出到本地
insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by '\t'
collection items terminated by '#' select * from student;
3、将查询的结果导出到HDFS上(没有local)
insert overwrite directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection
items terminated by '#' select * from score;
4、Hadoop命令导出到本地
dfs -get /export/servers/exporthive/000000_0 /export/servers/exporthive/local.txt;
5 、 hive shell 命令导出
bin/hive -e "select * from yhive.score;" > /export/servers/exporthive/score.txt
6、export导出到HDFS上(全表导出)
export table score to '/export/exporthive/score';
7、SQOOP导出(以后会有介绍)

清空hive数据表

truncate table score5

Hive查询语句

like
select * from tablename where name like '张%'
Rlike匹配正则
select * from tablename where name rlike '[]'
通配符
% 多个 100
_ 一个 1
 

Hive修改配置文件优先级

1、通过配置文件及逆行设置
2、hive进入shell 时,加参数 -hiveconf(命令行参数)
hive -hiveconf hive.root.logger=INFO;
3、在Hiveshell 内进行设置(参数声明)
set mapred.reduce.tasks=100;


参数声明 > 命令行参数 > 配置文件

压缩算法

Hive支持的数据存储格式

可支持Text,
SequenceFile,
ParquetFile,
ORC格式

RCFILE等
在实际的项目开发当中,hive表的数据存储格式一般选择:orc或parquet。压缩方式一般选择snappy。

Hive的优化

1、 Fetch抓取

设置属性 set hive.fetch.task.conversion=none; 所有的查询语句都要转化成MR程序。
set hive.fetch.task.conversion=more; 简单的查询不会转化成MR程序(select * from T,select id from T limit
N,)


什么是本地计算

数据存储到HDFS后,编写分析代码实现计算程序,程序在进行分发时,优先分发放到这个程序所使用到的数据所在
的节点上。


2、本地模式

任务在提交SQL语句的节点上“本地”执行,任务不会分配到集群。
作用或好处:在小数据量的前提下,提高查询效率

3、数据倾斜

当一个key数据过大时就会数据倾斜。
开启局(Map)部聚和功能,开启局部聚和后,hive会创建两个MR,程序,第一个进行数据的局部聚和,第二个进行
数据的最终汇总。

4、Count(distinct)

    先去重,再求总数量
SELECT count(DISTINCT id) FROM bigtable;
替换方案 SELECT count(id) FROM (SELECT id FROM bigtable GROUP BY id) a;

尽量避免笛卡尔积,在join时加on 条件

5、分区剪裁、列剪裁

分区剪裁: 只拿需要的分区,用什么那什么。
列剪裁: 只拿需要的列,用什么那什么。

先关联再过滤

SELECT a.id FROM bigtable a LEFT JOIN ori b ON a.id = b.id
WHERE b.id <= 10;
优化方案 先过滤再关联
1、SELECT a.id FROM ori a LEFT JOIN bigtable b
ON (b.id <= 10 AND a.id = b.id);
2、SELECT a.id FROM bigtable a RIGHT JOIN (SELECT id FROM ori WHERE id <= 10 ) b ON a.id = b.id;

6、动态分区调整

以第一个表的分区规则,来对应第二个表的分区规则,将第一个表的所有分区,全部拷贝到第二个表中来,第二个 表在加载数据的时候,不需要指定分区了,直接用第一个表的分区即可

7、数据倾斜

如何解决数据倾斜问题思路:众人拾柴火焰高(将一个大任务拆分成多个小任务,再次执行)
前提:设置reduce数量10或更多
1、distribute by (字段)
2、distribute by rand()

8、影响Map的数量

1、文件很小时,影响map数量的因素是文件的数量
2、文件很大时,影响map数量的因素是块的数量

9、影响reduce的数量

参数1、每个Reduce处理的数据量
参数2、每个任务最大的reduce数
计算reducer数的公式
reduce数量=min(参数2,总输入数据量/参数1)

10、并行执行

HIVE将SQL转化成MapReduce,需要有 Map 阶段,Reduce 阶段,抽样阶段、合并阶段、limit阶段。
开启并行执行,使得多个没有依赖关系的任务同时执行。一起到提高查询效率的作用。


11、严格模式


不允许执行以下操作
1、 不允许扫描所有分区
2、使用了order by语句的查询,要求必须使用limit语句
3、限制笛卡尔积的查询


12、jvm重用


允许多个task使用一个jvm
好处:降低任务启动的开销,提高任务的执行效率。
不足:整个任务结束前jvm 不释放,长时间占用。导致资源不足时,资源(占用而不是用)浪费。


13、 推测执行


MapReduce的推测执行

发布了241 篇原创文章 · 获赞 300 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/bbvjx1314/article/details/105446465
今日推荐