大数据开发工程师-第八周 第3章 Hive基础使用


第八周 第3章 Hive基础使用

Hive的使用方式

1
2
操作Hive可以在Shell命令行下操作,或者是使用JDBC代码的方式操作
下面先来看一下在命令行中操作的方式

命令行方式

1
2
3
4
5
6
7
8
针对命令行这种方式,其实还有两种使用
第一个是使用bin目录下的hive命令,这个是从hive一开始就支持的使用方式

后来又出现一个beeline命令,它是通过HiveServer2服务连接hive,它是一个轻量级的客户端工具,所以后来官方开始推荐使用这个。

具体使用哪个我觉得属于个人的一个习惯问题,特别是一些做了很多年大数据开发的人,已经习惯了使用hive命令,如果让我使用beeline会感觉有点别扭

针对我们写的hive sql通过哪一种客户端去执行结果都是一样的,没有任何区别,所以在这里我们使用哪个就无所谓了。

hive

1
2
3
4
5
6
7
先看第一种,这种直接就可以连进去

[root@bigdata04 apache-hive-3.1.2-bin]# bin/hive

这里有一行信息提示,从Hive2开始Hive-on-MR就过时了,并且在以后的版本中可能就不维护了,建议使用其它的计算引擎,例如:spark或者tez

如果你确实想使用MapReduce引擎,那建议你使用Hive1.x的版本。
创建表
1
2
3
4
下面以hive开头的内容就是说明我们进入了Hive的命令行中,在这里就可以写Hive的SQL了
我们先查看一下目前有哪些表,后面的ok表示执行成功,现在默认是空的
hive> show tables;
hive> create table t1(id int,name string);
添加数据
1
2
hive> insert into t1(id,name) values(1,"zs");
此时会产生mapreduce任务

image-20230319174503747

查找数据
1
2
3
4
hive> select * from t1;

查询数据,为什么这时没有产生mapreduce任务呢?因为这个计算太简单了,不需要经过mapreduce任
务就可以获取到结果,直接读取表对应的数据文件就可以了。
删除表
1
hive> drop table t1;
1
2
3
4
退出
exit;
quit;
ctrl C

beeline

启动hiveserver2
1
2
3
1.启动hiveserver2服务
/bin/hiveserver2
2.bin/beeline -u jdbc:hive2://localhost:10000

image-20230319175020854

启动beeline

image-20230319175157534

1
2
3
4
5
6
7
8
9
10
注意了,启动hiveserver2服务之后,最下面会输出几行Hive Session ID的信息,一定要等到输出4行以后再使用beeline去连接,否则会提示连接拒绝

当hiveserver2服务没有真正启动成功之前连接会提示这样的信息

[root@bigdata04 apache-hive-3.1.2-bin]# bin/beeline -u jdbc:hive2://localhost
Connecting to jdbc:hive2://localhost:10000
20/05/06 16:44:21 [main]: WARN jdbc.HiveConnection: Failed to connect to loca
Could not open connection to the HS2 server. Please check the server URI and
Error: Could not open client transport with JDBC Uri: jdbc:hive2://localhost:
Beeline version 3.1.2 by Apache Hive
1
2
3
4
等待hiveserver2服务真 正启动之后再连接,此时就可以连接进去了

hiveserver2默认会监听本机的10000端口,所以命令是这样的
bin/beeline -u jdbc:hive2://localhost:10000
创建表、插入数据
1
2
3
0: jdbc:hive2://localhost:10000> create table t1(id int,name string);

0: jdbc:hive2://localhost:10000> insert into t1(id,name) values(1,"zs");
1
2
3
4
5
6
7
8
发现添加数据报错,提示匿名用户对/tmp/hadoop-yarn没有写权限
解决方法有两个
1. 给hdfs中的/tmp/hadoop-yarn设置777权限,让匿名用户具备权限
可以直接给tmp及下面的所有目录设置777权限
hdfs dfs -chmod -R 777 /tmp

2. 在启动beeline的时候指定一个对这个目录有操作权限的用户
bin/beeline -u jdbc:hive2://localhost:10000 -n root

image-20230319180814641

1
此时使用bin/hive命令行查看也是可以的,这两种方式维护的是同一份Metastore
1
注意:在beeline后面指定hiveserver2的地址的时候,可以指定当前机器的内网ip也是可以的。(其它机器也可连)
1
2
3
后面我们使用的时候我还是使用hive命令,已经习惯用这个了,还有一个就是大家如果也用这个的话,别人是不是感觉你也是老司机了,但是你要知道官方目前是推荐使用beeline命令的

在工作中我们如果遇到了每天都需要执行的命令,那我肯定想要把具体的执行sql写到脚本中去执行,但是现在这种用法每次都需要开启一个会话,好像还没办法把命令写到脚本中。
1
2
3
4
5
6
7
注意了,hive后面可以使用 -e 命令,这样这条hive命令就可以放到脚本中定时调度执行了因为这样每次hive都会开启一个新的会话,执行完毕以后再关闭这个会话。

/bin/hive -e "select * from t1"

当然了beeline也可以,后面也是跟一个-e参数

bin/beeline -u jdbc:hive2://localhost:10000 -e "select * from t1"
1
2
3
此时我们再把hive的bin目录配置到path环境变量中,在脚本直接使用hive或者beeline就可以了

# vim /etc/profile

image-20230319182144292

JDBC方式

1
2
3
4
5
6
7
8
9
10
11
JDBC这种方式也需要连接hiveserver2服务,前面我们已经启动了hiveserver2服务,在这里直接使用就可以了

创建maven项目 名为db_hive
在pom中添加hive-jdbc的依赖(官网mvn可以下载依赖)

<!-- hive-jdbc驱动 -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.2</version>
</dependency>

image-20230319182611353

image-20230319182735791

1
2
开发代码,创建包名: com.imooc.hive
创建类名: HiveJdbcDemo

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.imooc.hive;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
* JDBC代码操作 Hive
* 注意:需要先启动hiveserver2服务
* Created by xuwei
*/
public class HiveJdbcDemo {
public static void main(String[] args) throws Exception{
//指定hiveserver2的连接
String jdbcUrl = "jdbc:hive2://192.168.206.132:10000";
//获取jdbc连接,这里的user使用root,就是linux中的用户名,password随便指定即
Connection conn = DriverManager.getConnection(jdbcUrl, "root", "any")
//获取Statement
Statement stmt = conn.createStatement();
//指定查询的sql
String sql = "select * from t1";
//执行sql
ResultSet res = stmt.executeQuery(sql);
//循环读取结果
while (res.next()){
System.out.println(res.getInt("id")+"\t"+res.getString("name"));
}
}
}
1
此时直接执行运行,可能开发工具一直在下载相应的依赖包,可以通过cmd命令打jar包更快,然后应该会在maven项目的依赖包下出现,如果没有需要项目邮件手动导入

image-20230319183607128

image-20230319184037811

异常

1
2
3
4
5
6
7
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/.m2/org/apache/logging/log4j/log4j-slf4
SLF4J: Found binding in [jar:file:/D:/.m2/org/slf4j/slf4j-log4j12/1.6.1/slf4j
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanati
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory

ERROR StatusLogger No log4j2 configuration file found. Using default configur
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
分析上面的警告信息,发现现在是有两个log4j的实现类,需要去掉一个,还有就是缺少log4j2的配置文件,注意log4j2的配置文件是xml格式的,不是properties格式的
1: 去掉多余的log4j依赖,从日志中可以看到日志的路径
这两个去掉哪个都可以,这两个都是hive-jdbc这个依赖带过来的,所以需要修改pom文件中hive-jdbc中
的依赖
<!-- hive-jdbc驱动 -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.2</version>
<exclusions>
<!-- 去掉 log4j依赖 -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2:在项目的resources目录中增加log4j2.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

Set命令的使用

1
2
3
4
5
6
7
8
9
10
在hive命令行中可以使用set命令临时设置一些参数的值,
其实就是临时修改hive-site.xml中参数的值

不过通过set命令设置的参数只在当前会话有效,退出重新打开就无效了

如果想要对当前机器上的当前用户有效的话可以把命令配置在 ~/.hiverc文件中

所以总结一下,使用set命令配置的参数是当前会话有效,在~/.hiverc文件中配置的是当前机器中的当前用户有效,而在hive-site.xml中配置的则是永久有效了,

在hive-site.xml中有一个参数是 hive.cli.print.current.db ,这个参数可以显示当前所在的数据库名称,默认值为 false 。

显示数据库名

1
2
hive> set hive.cli.print.current.db = true;
hive (default)>

显示列属性

1
2
3
4
5
6
7
8
9
10
hive (default)> select * from t1;
OK
1 zs
Time taken: 0.184 seconds, Fetched: 1 row(s)
hive (default)> set hive.cli.print.header = true;
hive (default)> select * from t1;
OK
t1.id t1.name
1 zs
Time taken: 0.202 seconds, Fetched: 1 row(s)
1
2
这些参数属于我自己的个人习惯,所以我希望把这个配置放到我个人用户下面
修改 ~/.hiverc ,我们每次在进入hive命令行的时候都会加载当前用户目录下的 .hiverc 文件中的内容

image-20230319211203019

历史命令

1
2
3
4
5
6
7
如果我们想查看一下hive的历史操作命令如何查看呢?
linux中有一个history命令可以查看历史操作命令hive中也有类似的功能,hive中的历史命令会存储在当前用户目录下的 .hivehistory 目录中

[root@bigdata04 apache-hive-3.1.2-bin]# more ~/.hivehistory
show tables;
exit
.....

Hive的日志配置

image-20230319211750165

1
2
3
4
我们每次进入hive命令行的时候都会出现这么一坨日志,看着很恶心,想要去掉,怎么办呢?
通过分析日志可知,现在也是有重复的日志依赖,所以需要删除一个,这里是hive中的一个日志依赖包和hadoop中的日志依赖包冲入了,那我们只能去掉Hive的了,因为hadoop是共用的,尽量不要删它里面的东西。

为了保险起见,我们可以使用mv给这个日志依赖包重命名,这样它就不生效了
1
2
3
4
还有就是当我们遇到Hive执行发生错误的时候,我们要学会去查看Hive的日志信息,通过日志的提示来分析,找到错误的根源,帮助我们及时解决错误。
那我们在哪里查看Hive日志呢,我们可以通过配置文件来找到默认日志文件所在的位置。

在hive的conf目录下有一些log4j的模板配置文件,我们需要去修改一下,让它生效。
1
首先是 hive-log4j.properties.template 这个文件,去掉 .template 后缀,修改里面的 property.hive.log.level 和 property.hive.log.dir 参数
1
2
3
4
5
6
7
[root@bigdata04 conf]# mv hive-log4j2.properties.template hive-log4j2.properties
[root@bigdata04 conf]# vi hive-log4j2.properties
property.hive.log.level = WARN
property.hive.root.logger = DRFA
property.hive.log.dir = /data/hive_repo/log
property.hive.log.file = hive.log
property.hive.perflogger.log.level = INFO
1
2
3
4
5
6
7
8
9
然后修改 hive-exec-log4j2.properties.template 这个文件,去掉 .template 后缀修改里面的

[root@bigdata04 conf]# mv hive-exec-log4j2.properties.template hive-exec-log4
[root@bigdata04 conf]# vi hive-exec-log4j2.properties
property.hive.log.level = WARN
property.hive.root.logger = FA
property.hive.query.id = hadoop
property.hive.log.dir = /data/hive_repo/log
property.hive.log.file = ${sys:hive.query.id}.log
1
2
3
这样后期分析日志就可以到 /data/hive_repo/log 目录下去查看了。

提交任务后,在mapreduce产生的一些日志,还是要去yarn的weibu界面看

本文标题:大数据开发工程师-第八周 第3章 Hive基础使用

文章作者:TTYONG

发布时间:2022年02月20日 - 12:02

最后更新:2023年04月18日 - 23:04

原始链接:http://tianyong.fun/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%BC%80%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88-%E7%AC%AC%E5%85%AB%E5%91%A8-%E7%AC%AC3%E7%AB%A0-Hive%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8.html

许可协议: 转载请保留原文链接及作者。

多少都是爱
0%