hive设置账户与密码+DbVisualizer可视工具连接

  1. 自定义解析用户名与密码
    pom.xml
    注意hadoop与hive版本

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-common -->
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-common</artifactId>
            <version>2.3.5</version>
        </dependency>
        <dependency>

            <groupId>org.apache.hive</groupId>

            <artifactId>hive-service</artifactId>

            <version>2.3.5</version>

        </dependency>

自定义解析代码:

package org.apache.hadoop.hive.contrib.auth;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.slf4j.Logger;
import javax.security.sasl.AuthenticationException;
import org.apache.hive.service.auth.PasswdAuthenticationProvider;
public class CustomPasswdAuthenticator implements 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包,上传将jar包到hive根目录的lib目录下

  1. 修改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.hahah</name><!--用户名为最后一个:sixmonth-->
 <value>hahah</value><!--密码-->
</property> 

  1. 修改Hadoop代理访问 并重启Hadoop 与hive
  2. Java代码连接 hive
    pom
<dependency>
	<groupId>org.apache.hive</groupId>
	<artifactId>hive-jdbc</artifactId>
	<version>2.1.1</version>
	<exclusions>
		<exclusion>
			<groupId>org.eclipse.jetty.aggregate</groupId>
			<artifactId>*</artifactId>
		</exclusion>
	</exclusions>
</dependency>
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 sixmonth
 * @Date 2019年5月13日
 *
 */
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, "sixmonth", "sixmonth");
                        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();
    	
    }
 
}

DbVisualizer 连接hive 所需要驱动:

在这里插入图片描述
驱动安装方法
在DbVisualizer jdbc 目录下创建hive目录将驱动jar 放入 然后按照下图导入驱动即可
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值