Hive设置连接用户名和密码

背景

当hive客户端搭建起来之后,应用项目通过jdbc远程连接hive,目前是不需要用户名和密码,这是由于hive默认的用户名和密码都是空的,所以我们现在需要为hive添加新的用户名和密码;连接hive的时候就需要使用用户名和密码。

配置步骤

首先,添加hive的maven依赖,这儿maven的版本和hive运行的jar版本保持一致,不然容易出问题,hive/lib下查看jar的版本信息。

  1. pom添加hive依赖
<dependency>
	<groupId>org.apache.hive</groupId>
	<artifactId>hive-jdbc</artifactId>
	<version>version-info</version>
</dependency>

需要先用IDEA打包一个jar工具类,用于解析用户名和密码,具体需要打包的代码如下:

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;  
		  }
}

之后,将jar包放在hive的lib目录下,同时,需要修改conf下的hive-site.xml配置文件;

<!--自定义远程连接用户名和密码-->
<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value><!--默认为none,修改成CUSTOM-->
</property>
 
<!--指定解析jar包-->
<property>
<name>hive.server2.custom.authentication.class</name>
<value>org.apache.hadoop.hive.contrib.auth.CustomPasswdAuthenticator</value>
</property>  
 
<!--设置用户名和密码-->
<property>
 <name>hive.jdbc_passwd.auth.muzili</name><!--用户名为最后一个:muzili-->
 <value>muzili</value><!--密码-->
</property>  

以下没有做,可以跳过。
最后还需要修改hadoop的相关文件,切换到hadoop配置文件目录:hadoop/etc/hadoop,修改hadoop:core-site.xml,否则java连接hive没权限

<property>
  <name>hadoop.proxyuser.hadoop.hosts</name>
  <value>*</value>
</property>
<property>
  <name>hadoop.proxyuser.hadoop.groups</name>
  <value>*</value>
</property>

重启hadoop和hive,可以利用beeline命令去测试;

  1. 创建连接测试,使用beeline命令,进入hive的beeline客户端,执行以下命令:
  !connect jdbc:hive2//localhost:port/database
  1. 运行成功之后,即可读取hive里面的数据

总结
开发工具远程连接hive可自定义端口,缺省10000

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值