Live2D

java 链接hive(kerberos认证)实例 以及 错误点讲解

没有kerberos认证的hive链接比较方便,但是有kerberos认证就需要多做一些事情。

1、pom.xml 依赖:

<dependency>
   <groupId>org.apache.hive</groupId>
   <artifactId>hive-jdbc</artifactId>
   <version>0.12.0</version>
</dependency>
<dependency>
   <groupId>org.apache.hadoop</groupId>
   <artifactId>hadoop-client</artifactId>
   <version>2.7.2</version>
</dependency>

2、示例代码:(标黄的需要认真修改)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
  
public class App {
    private static String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver";
    private static String CONNECTION_URL ="jdbc:hive2://10.23.13.196:10000/ods;principal=hive/tw-manager@TDH";
  
    static {
        try {
            Class.forName(JDBC_DRIVER);
  
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
  
    public static void main(String[] args) throws Exception  {
        Class.forName(JDBC_DRIVER);
  
        //登录Kerberos账号
        System.setProperty("java.security.krb5.conf", "/root/tdh-cdh/ticket/krb5.conf");
  
        Configuration configuration = new Configuration();
        configuration.set("hadoop.security.authentication" , "Kerberos" );
        UserGroupInformation. setConfiguration(configuration);
        UserGroupInformation.loginUserFromKeytab("hive@TDH" , "/root/tdh-cdh/ticket/hive.keytab");
  
        Connection connection = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            connection = DriverManager.getConnection(CONNECTION_URL);
            ps = connection.prepareStatement("show tables");
            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         
    }
}

  

3、注意事项:

(1)krb5.conf、hive.keytab2个文件,路径要改对

(2)jar依赖包版本和服务器hadoop、hive版本要匹配,否则会报错:

    Caused by: org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq

    原因:This indicates a version mismatch between client and server, namely that the client is newer than the server, which is your case.

    客户端和服务端包的版本不一致;

(3)错误:Error: Could not open client transport with JDBC Uri: jdbc:hive2://master:10000/default: Peer indicated failure:

    Unsupported mechanism type PLAIN (state=08S01,code=0)

    解决: jdbc连接url时,缺少principal,整个url连接如下:jdbc:hive2://master:10000/default;principal=hive/master@HADOOP.COM


(4)错误: Could not open client transport with JDBC Uri: jdbc:hive2://master:10000/default;principal=hdfs/master@HADOOP.COM: Peer indicated failure:

    GSS initiate failed (state=08S01,code=0)
  解决: jdbc连接url时,principal的用户有问题,整个url连接如下:
    principal=hdfs/master@HADOOP.COM 替换为:hive/master@HADOOP.COM
    jdbc:hive2://master:10000/default;principal=hive/master@HADOOP.COM

 

 

 

参考:

  1、https://blog.csdn.net/DH2442897094/article/details/108007806?utm_medium=distribute.pc_relevant.none-task-blog-blogcommendfrommachinelearnpai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-blogcommendfrommachinelearnpai2-2.channel_param

  2、https://blog.csdn.net/weixin_44865574/article/details/106420734?utm_medium=distribute.pc_relevant.none-task-blog-title-3&spm=1001.2101.3001.4242

posted @   -涂涂-  阅读(5019)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示