package org.apache.hadoop.hive.contrib.auth;
import javax.security.sasl.AuthenticationException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.slf4j.Logger;
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;
}
}
hive.server2.authentication
CUSTOM
hive.server2.custom.authentication.class
org.apache.hadoop.hive.contrib.auth.CustomPasswdAuthenticator
hive.jdbc_passwd.auth.muzili
muzili
hadoop.proxyuser.hadoop.hosts
*
hadoop.proxyuser.hadoop.groups
*
1. pom添加hive依赖
org.apache.hive
hive-jdbc
2.1.1
org.eclipse.jetty.aggregate
*
2. 创建main方法连接测试
package com.springboot.sixmonth.common.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* hive连接测试类
* @author muzili
* @Date 2021年9月3日
*
*/
public class HiveTest {
//9019是自定义远程连接的端口,默认是10000
private static final String URLHIVE = "jdbc:hive2://47.100.200.200:9019/default";
private static Connection connection = null;
public static Connection getHiveConnection() {
if (null == connection) {
synchronized (HiveTest.class) {
if (null == connection) {
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
connection = DriverManager.getConnection(URLHIVE, "muzili", "muzili");
System.out.println("hive启动连接成功!");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
return connection;
}
public static void main(String args[]) throws SQLException{
String sql1="select * from sixmonth limit 1";
PreparedStatement pstm = getHiveConnection().prepareStatement(sql1);
ResultSet rs= pstm.executeQuery(sql1);
while (rs.next()) {
System.out.println(rs.getString(2));
}
pstm.close();
rs.close();
}
}
3. 运行成功之后,即可读取hive里面的数据