Hive实战

573 阅读10分钟

Hadoop生态圈:

  • hdfs分布式存
  • yarn:只存的话就跟yarn一点关系都没有。
  • hive:数据分析 分析的话也可基于spark、基于内存的、基于机器学习的、基于离线hive的
  • 分析完的结果用sqoop进行导出,比如说导出到mysql里
  • 然后springboot进行访问

开机启动:

启动爬虫程序:

film_spider-1.0-SNAPSHOT-jar-with-dependencies.ja

启动hdfs:

start-dfs.sh

启动flume

nohup ./start_flume_to_hdfs.sh &

启动yarn

start-yarn.sh

image-20210123200538238

要分析这个数据

HDFS:分布式存

YARN:资源调度

HIVE:数据分析

Hive概述

- facebook贡献的一个处理结构化数据的工具
- 官网:https://hive.apache.org/
https://cwiki.apache.org/confluence/display/Hive/LanguageManual
- 架构在hadoop框架之上
- 实现数据分析
- Hive QL语句(HQL)
- HQL会转为MapReduce程序执行
- Hive是构建与Hadoop之上的数据仓库软件,能够有效的读取、写入和管理大型数据集合,并且支持通过
SQL
查询分析数据。(Hive是基于Hadoop的,Hadoop数据处理任务本质上是MapReduce,所以HiveSQL执
行本
质上都是MapReduce任务)


- Hive使用场景:
1)Hive可构建基于Hadoop的数据仓库
2)Hive适合大数据集的批处理作业,比如行为日志分析、多维数据分析
3)海量结构化数据离线分析

Hive分析的数据底层是在Hdfs上面。

n多个节点共同分析运算。

Hive架构

image-20210123202736662

服务器端组件

  1. Driver:执行HQL语句,编译、优化、执行计划
  2. Thrift Server:软件框架,支持其它语言开发(JDBC/ODBC)
  3. Metastore: 元数据服务,Derby(Java DB),只支持单用户访问、MySQL

客户端组件

  1. CLI:命令行接口
  2. Thrift Client:jdbc/beeline
  3. Web GUI: URL

Hive服务管理

3.1 启动服务

  • 启动start-dfs.sh 分布式文件系统 不启动 数据找不到 没法建库也没法建表

  • 启动start-yarn.sh 分析需要聚合函数 需要资源 不管是内存资源还是硬盘资源 需要资源管理 不启动得话 运算、清洗都会出错

  • 启动mr-jobhistory-daemon.sh start historyserver mr是mapreduce的缩写 不设置的话默认hive底层的引擎就是mapreduce,如果后期进行hive作业处理的时候,执行过的作业都在这个历史任务里,这个mr-jobhistory就是专门负责管历史任务的

    端口号19888 是查错用的,点进去某一条右下方有个logs 记录了信息

  • 启动metastore元数据服务 : nohup hive --service metastore &

    nohup 英文全称 no hang up(不挂起),用于在系统后台不挂断地运行命令,退出终端不会影响程序的运行。

  • ps -ef | grep metastore #查看服务是否开启

[hadoop@hadoop01 ~]$ jps
2640 NodeManager
6964 RunJar #hive启动
7092 Jps
2246 SecondaryNameNode
6855 JobHistoryServer
2059 DataNode
2523 ResourceManager
1934 NameNode

hive客户端cli连接

创一个hive_dir 文件夹,把老师给的user.txt上传进去

image-20210124180224945

进来了:

image-20210124180340142 image-20210124180601466

image-20210123204639710

[hadoop@hadoop01 logs]$ hive
which: no hbase in (/home/hadoop/.local/bin:/home/hadoop/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/model/jdk1.8/bin:/opt/model/nginx/sbin:/opt/model/hadoop-3.2.1/bin:/opt/model/hadoop-3.2.1/sbin:/opt/model/flume-1.9.0/bin:/opt/model/hive-3.1.2/bin:/opt/model/sqoop2/bin:/opt/model/scala-2.12.8/bin:/opt/model/spark-3.0/sbin:/opt/model/spark-3.0/bin)
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/model/hive-3.1.2/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/model/hadoop-3.2.1/share/hadoop/common/lib/slf4j-log4j12-1.7.25.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]
Hive Session ID = 06bf7b26-4c19-4ec8-b334-b9cb8c53f590

Logging initialized using configuration in file:/opt/model/hive-3.1.2/conf/hive-log4j2.properties Async: true
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Hive Session ID = 5df45667-0848-40b2-90db-7bf093e9210b
hive>  #看到提示输入hive命令的效果,说明进入hive客户端成功
hive> show databases;    #1)查看hive中的数据库
OK
apply_service_code
city_db
default
demodb
film_db
film_db2
gmall
model_service_code
myschooldb
original_service_code
weather_db
Time taken: 1.125 seconds, Fetched: 11 row(s)
hive> create table userinfo( #2)在默认数据库default中创建了数据表userinfo 针对user.txt里的信息把它映射成表
    > id int,
    > name string,
    > udate string)
    > row format delimited
    > fields terminated by ',';
OK
Time taken: 1.578 seconds
hive> show tables;#3)查看默认default数据库下的数据表
OK
dwd_start_log
t_hdfs_user
temp_user
userinfo
Time taken: 0.124 seconds, Fetched: 4 row(s)
hive> desc userinfo;#4)查看表结构
OK
id                  	int                 	                    
name                	string              	                    
udate               	string              	                    
Time taken: 0.283 seconds, Fetched: 3 row(s)
hive> show create table userinfo; #5)查看详细表结构
OK
CREATE TABLE `userinfo`(
  `id` int, 
  `name` string, 
  `udate` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
WITH SERDEPROPERTIES ( 
  'field.delim'=',', 
  'serialization.format'=',') 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://hadoop01:9000/hive/warehouse/userinfo'    #存在了这下面
TBLPROPERTIES (
  'bucketing_version'='2', 
  'transient_lastDdlTime'='1611406133')
Time taken: 0.428 seconds, Fetched: 18 row(s)
hive> 

image-20210124180905726

创建数仓

数据库操作

hive> show databases; --查看数据库列表
hive> create database if not exists marketdb; --数据库不存在,则创建,会在集群里开辟一个空间
image-20210124181729390

具体的配置在这个文件里:

[hadoop@hadoop01 hive_dir]$ cd /opt/model/
[hadoop@hadoop01 model]$ ll
总用量 0
drwxrwxr-x 12 hadoop hadoop 293 9月  15 10:18 flume-1.9.0
drwxr-xr-x 12 hadoop hadoop 183 7月  16 2020 hadoop-3.2.1
drwxrwxr-x 13 hadoop hadoop 247 7月  21 2020 hive-3.1.2
drwxr-xr-x  7 hadoop hadoop 245 10月  5 2019 jdk1.8
drwxrwxr-x 11 hadoop hadoop 151 7月  16 2020 nginx
drwxrwxr-x  6 hadoop hadoop  50 12月  4 2018 scala-2.12.8
drwxr-xr-x 15 hadoop hadoop 235 7月  23 2020 spark-3.0
drwxrwxr-x 10 hadoop hadoop 185 7月  21 2020 sqoop2
[hadoop@hadoop01 model]$ cd hive-3.1.2/
[hadoop@hadoop01 hive-3.1.2]$ ll
总用量 56
drwxrwxr-x  3 hadoop hadoop   157 7月  20 2020 bin
drwxrwxr-x  2 hadoop hadoop  4096 7月  20 2020 binary-package-licenses
drwxrwxr-x  2 hadoop hadoop   308 12月 28 10:07 conf
drwxrwxr-x  4 hadoop hadoop    34 7月  20 2020 examples
drwxrwxr-x  7 hadoop hadoop    68 7月  20 2020 hcatalog
drwxrwxr-x  2 hadoop hadoop    49 1月  24 17:55 hive_logs
drwxrwxr-x 15 hadoop hadoop  4096 9月  16 16:42 hiveserver2_operation_logs
drwxrwxr-x  2 hadoop hadoop    44 7月  20 2020 jdbc
drwxrwxr-x  4 hadoop hadoop 12288 9月  15 18:00 lib
-rw-r--r--  1 hadoop hadoop 20798 8月  23 2019 LICENSE
drwxrwxr-x  2 hadoop hadoop   102 7月  21 2020 logs
-rw-r--r--  1 hadoop hadoop   230 8月  23 2019 NOTICE
-rw-r--r--  1 hadoop hadoop  2469 8月  23 2019 RELEASE_NOTES.txt
drwxrwxr-x  4 hadoop hadoop    35 7月  20 2020 scripts
[hadoop@hadoop01 hive-3.1.2]$ cd conf/
[hadoop@hadoop01 conf]$ ll
总用量 40
-rw-r--r-- 1 hadoop hadoop 1596 8月  23 2019 beeline-log4j2.properties.template
-rw-r--r-- 1 hadoop hadoop 2399 9月  15 16:58 hive-env.sh
-rw-r--r-- 1 hadoop hadoop 2274 8月  23 2019 hive-exec-log4j2.properties.template
-rw-r--r-- 1 hadoop hadoop 3078 7月  20 2020 hive-log4j2.properties
-rw-r--r-- 1 hadoop hadoop 1873 9月  15 16:58 hive-site.xml
-rw-r--r-- 1 hadoop hadoop 2060 8月  23 2019 ivysettings.xml
-rw-r--r-- 1 hadoop hadoop 3558 8月  23 2019 llap-cli-log4j2.properties.template
-rw-r--r-- 1 hadoop hadoop 7163 8月  23 2019 llap-daemon-log4j2.properties.template
-rw-r--r-- 1 hadoop hadoop 2662 8月  23 2019 parquet-logging.properties
[hadoop@hadoop01 conf]$ vi hive-site.xml 

元数据的记录信息放到了mysql数据库里面

页配置了新建数据库所存放的目录(元数据的仓库)

image-20210124182217088
hive> desc database marketdb; --查看数据库具体存储情况
OK
marketdb hdfs://hadoop01:9000/hive/warehouse/marketdb.db hadoop USER
Time taken: 0.213 seconds, Fetched: 1 row(s)

hive> use marketdb; --使用该数据库
hive> drop database if exists marketdb cascade; --数据库存在则删除该数据库(cascade级联删除数据库里面的表和数据)

数据表操作

表:在hdfs中会以“文件夹”形式管理

数据类型

int 、float、double、boolean、string、timestamp

内部表

存储路径 :/hive/warehouse/数据库目录/

删除内部表:同时删除表结构和数据

#在marketdb里创建一个userinfo表(我直接把刚刚创建的default里面的userinfo移过去了,如果要新建的话记得use一个marketdb)
[hadoop@hadoop01 conf]$ hdfs dfs -mv /hive/warehouse/userinfo /hive/warehouse/marketdb.db
#上面的移动没有用,不能移动 还是要新建
hive> create table userinfo(
    > id int,
    > name string,
    > udate string)
    > row format delimited
    > fields terminated by ',';
OK

#把刚刚上传的users.txt导进去
hive> load data local inpath '/opt/data/hive_dir/users.txt' into table userinfo; --3加载数据

Loading data to table marketdb.userinfo
OK
Time taken: 1.704 seconds

image-20210124185915323 image-20210124190223979

导入的数据还是以文件的形式存储的。

删除表

drop table userinfo;--5.删除表,则/hive/warehouse下表结构和数据一并被删除

外部表

存储路径:比较灵活,可以随意规划在hdfs的任意空间

删除外部表: 只会删除表结构,不删除数据

注意:为了提升操作的效率,采用sql脚本方式操作

  • 准备: hdfs 创建目录: /input/user 命令:hdfs dfs -mkdir -p /input/user

    image-20210124190807005
  • 将users.txt 上传到 /input/user 命令: hdfs dfs -put users.txt /input/user

    出现问题:

    [hadoop@hadoop01 conf]$ hdfs dfs -put /opt/data/hive_dir/users.txt /input/user
    2021-01-24 19:09:18,467 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = false
    

    这不是问题,这是成功了。- -||无语orz

    image-20210124191933230

任务: 创建sql脚本文件: userinfo.sql

[hadoop@hadoop01 model]$ cd /opt/data/hive_dir/
[hadoop@hadoop01 hive_dir]$ vi userinfo.sql
#在这个文件夹底下创建
image-20210124192650618
  • 创建数据库userdb

  • 创建外部表userinfo

  • 给userinfo导入数据users.txt

    用location关联这个目录

【执行该sql脚本文件】

[hadoop@hadoop01 hive_dir]$ hive -f userinfo.sql #执行
which: no hbase in (/home/hadoop/.local/bin:/home/hadoop/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/model/jdk1.8/bin:/opt/model/nginx/sbin:/opt/model/hadoop-3.2.1/bin:/opt/model/hadoop-3.2.1/sbin:/opt/model/flume-1.9.0/bin:/opt/model/hive-3.1.2/bin:/opt/model/sqoop2/bin:/opt/model/scala-2.12.8/bin:/opt/model/spark-3.0/sbin:/opt/model/spark-3.0/bin)
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/model/hive-3.1.2/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/model/hadoop-3.2.1/share/hadoop/common/lib/slf4j-log4j12-1.7.25.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]
Hive Session ID = bed19b18-7b73-4e39-8c19-fecb85326598

Logging initialized using configuration in file:/opt/model/hive-3.1.2/conf/hive-log4j2.properties Async: true
Hive Session ID = 0fa4f4bd-fdfa-48a4-8f27-88b8c6524e4c
OK
Time taken: 1.699 seconds
OK
Time taken: 0.117 seconds
OK
Time taken: 0.526 seconds

image-20210124193219004 image-20210124193458325

数据也没问题。

但是网站上这个地方是没有tables的

image-20210124193554690

这就可以理解了为什么我的移动不起作用,它把那个/input/user的目录关联到了这里,里面的数据就是userinfo里的数据,相当于指针?

因为是关联的,没有真正把数据挪过来,所以删除userdb这个数据库,真正的uers.txt这个数据也不会被删除。

分区表

  • 内部表、外部表都可以设置成分区表

  • 分区表:将数据在物理上移动用户频繁检索的区域 ,某类信息放到同一个位置,方便检索,例如说2020年的信息放在一起

    或者说某一个区的数据规定好了就放在某一个或几个连续的特定的节点上面,因为大的集群都有上千台节点

  • 提升数据检索、查询的效率

只要进行hive操作,一般都要建分区,hive操作,一般数据量都非常大,需要进行高频的查询检索。

为了清晰明了,把老师给3个数据文件通过xftp上传上到hive_dir里

image-20210125201334850

每一天的信息以日期的方式排列(跟实际的生产环境中的日志差不多)

image-20210125201548993

我们来为他们创建分区。

具体如何分区要取决于具体的业务,按照地区分区啊 按照日期分区,学校里按专业分区啊等等

一年建一个分区,一年里有12个月份,再按月份建子目录

1.静态分区

来一个文件有一个专门的地方接待你,放到指定的目录里

利用一下之前写的userinfo.sql的脚本

[hadoop@hadoop01 hive_dir]$ cp userinfo.sql userinfo_static_partition.sql
[hadoop@hadoop01 hive_dir]$ ll
总用量 24
-rw-rw-r-- 1 hadoop hadoop  64 1月  25 20:13 2020_12_11.users.txt
-rw-rw-r-- 1 hadoop hadoop  69 1月  25 20:13 2021_01_19_users.txt
-rw-rw-r-- 1 hadoop hadoop  69 1月  25 20:13 2021_02_02_users.txt
-rw-rw-r-- 1 hadoop hadoop 345 1月  24 19:27 userinfo.sql
-rw-rw-r-- 1 hadoop hadoop 345 1月  25 20:22 userinfo_static_partition.sql
-rw-rw-r-- 1 hadoop hadoop 169 1月  24 18:03 users.txt
[hadoop@hadoop01 hive_dir]$ vi userinfo_static_partition.sql 

里面有一些内容需要修改一下

image-20210125221653148

【执行sql脚本文件】

[hadoop@hadoop01 hive_dir]$ hive -f userinfo_static_partition.sql 
结果:
which: no hbase in (/home/hadoop/.local/bin:/home/hadoop/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/model/jdk1.8/bin:/opt/model/nginx/sbin:/opt/model/hadoop-3.2.1/bin:/opt/model/hadoop-3.2.1/sbin:/opt/model/flume-1.9.0/bin:/opt/model/hive-3.1.2/bin:/opt/model/sqoop2/bin:/opt/model/scala-2.12.8/bin:/opt/model/spark-3.0/sbin:/opt/model/spark-3.0/bin)
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/model/hive-3.1.2/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/model/hadoop-3.2.1/share/hadoop/common/lib/slf4j-log4j12-1.7.25.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]
Hive Session ID = 4932e82c-c4d1-4cbc-94bd-acc332559949

Logging initialized using configuration in file:/opt/model/hive-3.1.2/conf/hive-log4j2.properties Async: true
Hive Session ID = 1cb7c407-982c-42c1-91a4-6a77657c28e7
OK
Time taken: 1.72 seconds
OK
Time taken: 0.142 seconds
OK
Time taken: 1.007 seconds
Loading data to table userdb.userinfo_static_partition partition (year=2020, month=12, day=11)
OK
Time taken: 3.21 seconds
Loading data to table userdb.userinfo_static_partition partition (year=2021, month=1, day=19)
OK
Time taken: 1.343 seconds
Loading data to table userdb.userinfo_static_partition partition (year=2021, month=2, day=2)
OK
Time taken: 1.391 seconds
OK
1001	张三	2021-01-19	2021	1	19
1111	李四	2021-01-19	2021	1	19
1123	王五	2021-01-19	2021	1	19
Time taken: 5.654 seconds, Fetched: 3 row(s)

image-20210125222809197 image-20210125222809197

2.动态分区

数据一股脑都来了,没办法挨个指定在哪在哪,我们需要提前设定好一个规则,自动分配。

image-20210126132550452

image-20210126133553315 image-20210126133641241

注意:因为是从静态分区检索的(当然也可以不从静态分区检索),所以动态分区的这个year、month字段要和静态分区的partion里的字段一致

【执行sql脚本】

[hadoop@hadoop01 hive_dir]$ hive -f userinfo_dynamic_partition.sql 

image-20210126132937686

从相应的日志可以看出来,是基于mapreduce来二点

先map,map执行完之后执行reduce操作。

检索、过滤、移动数据。

image-20210126133206718

也是相当于把数据复制了过来。