版本说明
hadoop 3.2.2
hive 版本 3.1.2
安装好hive 之后,hive默认的用户名和密码都是空的,所以需要我们设置用户名和密码。
参考网上的,按照自定义的方式设置hive 的用户名和密码。
1.首先要打一个jar 包,把这个jar 包放到hive 安装目录的lib下,我这里已经打好了,需要可以去下载。
链接:https://pan.baidu.com/s/1h2LRDaCp1cmjRWfi2ccYlg
提取码:nkt5
2.也可以自己通过代码编译自行打包;
package org.apache.hadoop.hive.contrib.auth;
import org.slf4j.Logger;
import javax.security.sasl.AuthenticationException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
/**
* @date: 2022-02-10 20:52
* @description: 自定义设置 hive的用户名和密码
*/
public class CustomPasswdAuthenticator implements org.apache.hive.service.auth.PasswdAuthenticationProvider {
private Logger LOG = org.slf4j.LoggerFactory.getLogger(CustomPasswdAuthenticator.class);
private static final String HIVE_JDBC_PASSWD_AUTH_PREFIX = "hive.jdbc_passwd.auth.%s";
private Configuration conf = null;
@Override
public void Authenticate(String userName, String passwd)
throws AuthenticationException {
LOG.info("user: " + userName + " try login.");
String passwdConf = getConf().get(String.format(HIVE_JDBC_PASSWD_AUTH_PREFIX, userName));
if (passwdConf == null) {
String message = "user's ACL configration is not found. user:" + userName;
LOG.info(message);
throw new AuthenticationException(message);
}
if (!passwd.equals(passwdConf)) {
String message = "user name and password is mismatch. user:" + userName;
throw new AuthenticationException(message);
}
}
public Configuration getConf() {
if (conf == null) {
this.conf = new Configuration(new HiveConf());
}
return conf;
}
public void setConf(Configuration conf) {
this.conf = conf;
}
}
注意: 自己建一个新的项目,包名必须是
org.apache.hadoop.hive.contrib.auth;
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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>hive-dev</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
</project>
执行 mvn clean install 即可。
修改hive目录下的conf文件夹中的hive-site.xml,添加配置:
<!--自定义远程连接用户名和密码-->
<property>
<name>hive.server2.authentication</name>
<!--默认为none,修改成CUSTOM-->
<value>CUSTOM</value>
</property>
<!--指定解析jar包-->
<property>
<name>hive.server2.custom.authentication.class</name>
<value>org.apache.hadoop.hive.contrib.auth.CustomPasswdAuthenticator</value>
</property>
<!--设置用户名和密码,如果有多个用户和密码,可以多写几个property-->
<property>
<!--用户名是 fl-->
<name>hive.jdbc_passwd.auth.fl</name>
<!--密码-->
<value>hive</value>
</property>
最后还需要修改hadoop的相关文件,切换到hadoop配置文件目录:hadoop/etc/hadoop,修改hadoop:core-site.xml,否则java连接hive没权限;
<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>
重启hadoop和hive。
Jdbc 操作hive demo
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* @description: java jdbc 连接hive hive 版本3.1.2
*/
public class HiveConnection {
public static void main(String[] args) throws Exception {
// 注册jdbc驱动
Class.forName("org.apache.hive.jdbc.HiveDriver");
//连接对象,连接hive的本机ip,不指定端口,默认端口号是10000,数据库名是hive_db,连接hive的账号是xxx,密码是 hive
Connection conn = DriverManager.getConnection("jdbc:hive2://1x.1xx.1x.2x1:10000/hive_db", "xxx", "hive");
// 创建SQL执行器
Statement st = conn.createStatement();
// 执行SQL语句,得到结果集
String sql = "show tables";
ResultSet rs = st.executeQuery(sql);
// 处理结果
while (rs.next()) {
System.out.println(rs.getString(1));
}
// 关闭资源
rs.close();
st.close();
conn.close();
}
}
执行如下命令,可以看你jdbc 连接hive 的执行情况。(执行这个 Java才能连接成功)
./hive --service hiveserver2