一、Hive的使用方式
1.1、命令行方式hive方式
针对命令行这种方式,其实还有两种使用第一个是使用bin目录下的hive命令,这个是从hive一开始就支持的使用方式后来又出现一个beeline命令,它是通过HiveServer2服务连接hive,它是一个轻量级的客户端工具,所以后来官方开始推荐使用这个。
bin/hive

比如输入:
show tables
说明现在还没有表,下面创建一个表:
create table student(id int,name string);
插入数据:
1.2、命令行方式 beeline方式
bin/beeline -u jdbc:hive2://localhost:10000
查询数据:select * from student;
1.3、JDBC方式
在代码中写SQL查询数据
-
启动hiveserver2
bin/hiveserver2
-
测试beeline是否能链接
bin/beeline -u jdbc:hive2://192.168.234.100:10000 -n root
pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hadoop-parent</artifactId>
<groupId>com.strivelearn.hadoop</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hive</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b06</version>
</dependency>
</dependencies>
</project>
java代码
package com.strivelearn.hive;
import java.sql.*;
/**
* @author xys
* @version HiveJdbcMain.java, 2022年10月07日
*/
public class HiveJdbcMain {
public static void main(String[] args) throws SQLException {
//指定hiveserver2的连接
String jdbcUrl = "jdbc:hive2://192.168.234.100:10000";
//获取jdbc连接,这里的user使用root,就是linux中的用户名,password随便指定即
Connection conn = DriverManager.getConnection(jdbcUrl, "root", "root");
//获取Statement
Statement stmt = conn.createStatement();
//指定查询的sql
String sql = "select * from student";
//执行sql
ResultSet res = stmt.executeQuery(sql);
//循环读取结果
while (res.next()) {
System.out.println(res.getInt("id") + "\t" + res.getString("name"));
}
}
}
结果
二、Set命令使用
在hive命令行中可以使用set命令临时设置一些参数的值, 其实就是临时修改hive-site.xml中参数的值 不过通过set命令设置的参数只在当前会话有效,退出重新打开就无效了 如果想要对当前机器上的当前用户有效的话可以把命令配置在 ~/.hiverc文件中
所以总结一下,使用set命令配置的参数是当前会话有效,在~/.hiverc文件中配置的是当前机器中的当前 用户有效,而在hive-site.xml中配置的则是永久有效了, 在hive-site.xml中有一个参数是 hive.cli.print.current.db ,这个参数可以显示当前所在的数据库名 称,默认值为 false 。 在这里我们设置为true
set hive.cli.print.current.db = true; 显示当前所在的数据库名
set hive.cli.print.header = true; 控制获取结果的时候显示字段名称
三、Hive日志配置
每次启动Hive的时候,都会报日志重复,如下:
这里是hive中的一个日志依赖包和hadoop中的日志依赖包冲入了,那我们只能去掉Hive的了,因为hadoop是共用的,尽量不要删它里面的东西。为了保险起见,我们可以使用mv给这个日志依赖包重命名,这样它就不生效了
mv log4j-slf4j-impl-2.17.1.jar log4j-slf4j-impl-2.17.1.jar.bak
那我们在哪里查看Hive日志呢,我们可以通过配置文件来找到默认日志文件所在的位置。在hive的conf目录下有一些log4j的模板配置文件,我们需要去修改一下,让它生效。首先是 hive-log4j.properties.template 这个文件,去掉 .template 后缀,修改里面的 property.hive.log.level 和 property.hive.log.dir 参数
property.hive.log.level = WARN
property.hive.root.logger = DRFA
property.hive.log.dir = /root/hive_repo/log
property.hive.log.file = hive.log
property.hive.perflogger.log.level = INFO