版本说明
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;
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 文件
4.0.0 org.example hive-dev 1.0-SNAPSHOT 8 8 org.apache.hive hive-jdbc 3.1.2
执行 mvn clean install 即可。
修改hive目录下的conf文件夹中的hive-site.xml,添加配置:
hive.server2.authentication CUSTOM hive.server2.custom.authentication.class org.apache.hadoop.hive.contrib.auth.CustomPasswdAuthenticator hive.jdbc_passwd.auth.fl hive
最后还需要修改hadoop的相关文件,切换到hadoop配置文件目录:hadoop/etc/hadoop,修改hadoop:core-site.xml,否则java连接hive没权限;
hadoop.proxyuser.root.hosts * hadoop.proxyuser.root.groups *
重启hadoop和hive。
Jdbc 操作hive demo
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
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://10.188.120.201: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



