2021-06-03 大数据技术之 Hive

第 1 章 Hive 基本概念
1.1 什么是 Hive

Hive:由 Facebook 开源用于解决海量结构化日志的数据统计。 Hive 是基于 Hadoop
的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并 提供类 SQL 查询功能。
本质是:将 HQL 转化成 MapReduce 程序
1)Hive 处理的数据存储在 HDFS
2)Hive 分析数据底层的默认实现是 MapReduce
3)执行程序运行在 Yarn 上

1.2 Hive 的优缺点
1.2.1 优点

  1. 操作接口采用类 SQL 语法,提供快速开发的能力(简单、容易上手)。
  2. 避免了去写 MapReduce,减少开发人员的学习成本。
  3. Hive 的执行延迟比较高,因此 Hive 常用于数据分析,对实时性要求不高的场合。
  4. Hive 优势在于处理大数据,对于处理小数据没有优势,因为 Hive 的执行延迟比较 高。
  1. Hive 支持用户自定义函数,用户可以根据自己的需求来实现自己的函数。
    1.2.2 缺点

1.Hive 的 HQL 表达能力有限
(1)迭代式算法无法表达
(2)数据挖掘方面不擅长
2.Hive 的效率比较低
(1)Hive自动生成的 MapReduce 作业,通常情况下不够智能化
(2)Hive 调优比较困难,粒度较粗

第 2 章 Hive 安装
2.1 Hive 安装地址
1.Hive 官网地址

http://hive.apache.org/

2.文档查看地址

https://cwiki.apache.org/confluence/display/Hive/GettingStarted

3.下载地址

http://archive.apache.org/dist/hive/

2.2 Hive 安装部署
1.Hive 安装及配置

(1)把 apache-hive-1.2.1-bin.tar.gz 上传到 linux 的/opt/software 目录下
(2)解压apache-hive-1.2.1-bin.tar.gz 到/opt/module/目录下面
[atguigu@hadoop102 software]$ tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt/module/
(3)修改 apache-hive-1.2.1-bin.tar.gz 的名称为 hive
[atguigu@hadoop102 module]$ mv apache-hive-1.2.1-bin/ hive
(4)修改/opt/module/hive/conf目录下的 hive-env.sh.template 名称为 hive-env.sh
[atguigu@hadoop102 conf]$ mv hive-env.sh.template hive-env.sh
(5)配置 hive-env.sh 文件
(a)配置HADOOP_HOME 路径
export HADOOP_HOME=/opt/module/hadoop-2.7.2
(b)配置 HIVE_CONF_DIR 路径
export HIVE_CONF_DIR=/opt/module/hive/conf

2.Hadoop 集群配置
(1)必须启动 hdfs 和 yarn

[atguigu@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
[atguigu@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh

(2)在 HDFS 上创建/tmp 和/user/hive/warehouse 两个目录并修改他们的同组权限可写
(可不操作,系统会自动创建)

[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -mkdir /tmp
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -mkdir -p /user/hive/warehouse
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /tmp
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /user/hive/warehouse

3.Hive 基本操作
(1)启动 hive

[atguigu@hadoop102 hive]$ bin/hive

(2)查看数据库

hive> show databases;

(3)打开默认数据库

hive> use default;

(4)显示 default 数据库中的表

hive> show tables;

(5)创建一张表

hive> create table student(id int, name string);

(6)显示数据库中有几张表

hive> show tables;

(7)查看表的结构

hive> desc student;

(8)向表中插入数据

hive> insert into student values(1000,"ss");

(9)查询表中数据

hive> select * from student;

(10)退出 hive

hive> quit;

2.3 将本地文件导入 Hive 案例
需求

将本地/opt/module/data/student.txt 这个目录下的数据导入到 hive 的 student(id int,name string)表中。

1.数据准备
在/opt/module/data 这个目录下准备数据
(1)在/opt/module/目录下创建 data

[atguigu@hadoop102 module]$ mkdir data

(2)在/opt/module/datas/目录下创建 student.txt 文件并添加数据

[atguigu@hadoop102 datas]$ touch student.txt
[atguigu@hadoop102 datas]$ vi student.txt
1001 zhangshan
1002 lishi
1003 zhaoliu

2.Hive 实际操作
(1)启动 hive

[atguigu@hadoop102 hive]$ bin/hive

(2)显示数据库

hive> show databases;

(3)使用 default 数据库

hive> use default;

(4)显示 default 数据库中的表

hive> show tables;

(5)删除已创建的 student 表

hive> drop table student;

(6)创建 student 表, 并声明文件分隔符’\t’

hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

(7)加载/opt/module/data/student.txt 文件到 student 数据库表中。

hive> load data local inpath '/opt/module/data/student.txt' into table student;

(8)Hive 查询结果

hive> select * from student;
OK
1001 zhangshan
1002 lishi
1003 zhaoliu
Time taken: 0.266 seconds, Fetched: 3 row(s)

2.5 Hive 元数据配置到 MySql
2.5.1 驱动拷贝
1.在/opt/software/mysql-libs 目录下解压 mysql-connector-java-5.1.27.tar.gz 驱动

[root@hadoop102 mysql-libs]# tar -zxvf mysql-connector-java-5.1.27.tar.gz

2.拷贝 mysql-connector-java-5.1.27-bin.jar 到/opt/module/hive/lib/

[root@hadoop102 mysql-connector-java-5.1.27]# cp /opt/software/mysql-libs/mysql-connector-java-5.1.27/mysql-connector-java-5.1.27-bin.jar/opt/module/hive/lib/

2.5.2 配置 Metastore 到 MySql
1.在/opt/module/hive/conf 目录下创建一个 hive-site.xml

[atguigu@hadoop102 conf]$ touch hive-site.xml
[atguigu@hadoop102 conf]$ vi hive-site.xml

2.根据官方文档配置参数,拷贝数据到 hive-site.xml 文件中

https://cwiki.apache.org/confluence/display/Hive/AdminManual+MetastoreAdmin

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
 <name>javax.jdo.option.ConnectionURL</name>
 
<value>jdbc:mysql://hadoop102:3306/metastore?createDatabaseI
fNotExist=true</value>
 <description>JDBC connect string for a JDBC 
metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
 <value>com.mysql.jdbc.Driver</value>
 <description>Driver class name for a JDBC 
metastore</description>
</property>
<property>
 <name>javax.jdo.option.ConnectionUserName</name>
 <value>root</value>
 <description>username to use against metastore 
database</description>
</property>
<property>
 <name>javax.jdo.option.ConnectionPassword</name>
 <value>000000</value>
 <description>password to use against metastore 
database</description>
</property>
</configuration>

3.配置完毕后,如果启动 hive 异常,可以重新启动虚拟机。(重启后,别忘了启
动 hadoop 集群)
2.6 Hive 其他命令操作
1.在 hive cli 命令窗口中如何查看 hdfs 文件系统

hive> dfs -ls /;

2.在 hive cli 命令窗口中如何查看本地文件系统

hive> ! ls /opt/module/datas;

3.查看在 hive 中输入的所有历史命令
(1)进入到当前用户的根目录/root 或/home/atguigu
(2)查看. hivehistory 文件

[atguigu@hadoop102 ~]$ cat .hivehistory

3 分区表

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

1.引入分区表(需要根据日期对日志进行管理)

/user/hive/warehouse/log_partition/20170702/20170702.log
/user/hive/warehouse/log_partition/20170703/20170703.log
/user/hive/warehouse/log_partition/20170704/20170704.log

2.创建分区表语法

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

3.加载数据到分区表中

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition 
partition(month='201709');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition 
partition(month='201708');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition 
partition(month='201707’);

在这里插入图片描述
4.查询分区表中数据
单分区查询

hive (default)> select * from dept_partition where month='201709';

5.增加分区
创建单个分区

hive (default)> alter table dept_partition add partition(month='201706') ;

同时创建多个分区

hive (default)> alter table dept_partition add partition(month='201705') partition(month='201704');

6.删除分区
删除单个分区

hive (default)> alter table dept_partition drop partition (month='201704');

同时删除多个分区

hive (default)> alter table dept_partition drop partition (month='201705'), partition (month='201706');

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

hive> show partitions dept_partition;

8.查看分区表结构

hive> desc formatted dept_partition;

4.实操案例
(1)查询表结构

hive> desc dept_partition;

(2)添加列

hive (default)> alter table dept_partition add columns(deptdesc string);

(3)查询表结构

hive> desc dept_partition;

(4)更新列

hive (default)> alter table dept_partition change column deptdesc desc int;

(5)查询表结构

hive> desc dept_partition;

(6)替换列

hive (default)> alter table dept_partition replace columns(deptno string, dname
string, loc string);

(7)查询表结构

hive> desc dept_partition;

(8)删除表

hive (default)> drop table dept_partition;

5.1.1 向表中装载数据(Load)
实操案例
(0)创建一张表

hive (default)> create table student(id string, name string) row format delimited fields terminated by '\t';

(1)加载本地文件到 hive

hive (default)> load data local inpath '/opt/module/datas/student.txt' into table default.student;

(2)加载 HDFS 文件到 hive 中上传文件到 HDFS

hive (default)> dfs -put /opt/module/datas/student.txt /user/atguigu/hive;

加载 HDFS 上数据

hive (default)> load data inpath '/user/atguigu/hive/student.txt' into table default.student;

(3)加载数据覆盖表中已有的数据
上传文件到 HDFS

hive (default)> dfs -put /opt/module/datas/student.txt /user/atguigu/hive;

加载数据覆盖表中已有的数据

hive (default)> load data inpath '/user/atguigu/hive/student.txt' overwrite into table default.student;

5.1.2 通过查询语句向表中插入数据(Insert)
1.创建一张分区表

hive (default)> create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';

2.基本插入数据

hive (default)> insert into table student partition(month='201709') values(1,'wangwu');

3.基本模式插入(根据单张表查询结果)

hive (default)> insert overwrite table student partition(month='201708') select id, name from student where month='201709';

4.多插入模式(根据多张表查询结果)

hive (default)> from student
 insert overwrite table student 
partition(month='201707')
 select id, name where month='201709'
 insert overwrite table student 
partition(month='201706')
 select id, name where month='201709';

5.1.4 创建表时通过 Location 指定加载数据路径
1.创建表,并指定在 hdfs 上的位置

hive (default)> create table if not exists student5(id int, name string) row format delimited fields terminated by '\t' location '/user/hive/warehouse/student5';

2.上传数据到 hdfs 上

hive (default)> dfs -put /opt/module/datas/student.txt  /user/hive/warehouse/student5;

3.查询数据

hive (default)> select * from student5;

5.2 数据导出
5.2.1 Insert 导出
1.将查询的结果导出到本地

hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
 select * from student;

2.将查询的结果格式化导出到本地

hive(default)>insert overwrite local directory '/opt/module/datas/export/student1'
 ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;

3.将查询的结果导出到 HDFS 上(没有 local)

hive (default)> insert overwrite directory '/user/atguigu/student2' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;

5.2.2 Hadoop 命令导出到本地

hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0
/opt/module/datas/export/student3.txt;

5.2.3 Hive Shell 命令导出
基本语法:(hive -f/-e 执行语句或者脚本 > file)

[atguigu@hadoop102 hive]$ bin/hive -e 'select * from default.student;' >/opt/module/datas/export/student4.txt;

5.2.4 Export 导出到 HDFS 上

(defahiveult)> export table default.student to '/user/hive/warehouse/export/student';

6.1 常用函数
1.求总行数(count)

hive (default)> select count(*) cnt from emp;

2.求工资的最大值(max)

hive (default)> select max(sal) max_sal from emp;

3.求工资的最小值(min)

hive (default)> select min(sal) min_sal from emp;

4.求工资的总和(sum)

hive (default)> select sum(sal) sum_sal from emp; 

5.求工资的平均值(avg)

hive (default)> select avg(sal) avg_sal from emp;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值