Java 连接Hbase数据库,使用Kerberos进行认证获取连接
近期有个任务是开发Hbase元数据接口,获取Hbase的连接,然后去操作Hbase,获取其元数据等信息。
开始使用一般认证的方式,然后写好接口获取各种元数据信息方法是测试可行的。后面测试Kerberos认证的方式,写了测试连接的方法,通过Kerberos认证获取连接是成功的,但是测试接口获取元数据等信息的时候,就一直报错。报错信息如下
Sat Sep 03 09:52:28 CST 2022, RpcRetryingCaller{globalStartTime=1662169947866, pause=100, maxAttempts=16}, org.apache.hadoop.hbase.MasterNotRunningException: java.io.IOException: Call to dmp01/10.243.140.76:16000 failed on local exception: java.io.IOException: Failed to specify server's Kerberos principal name
Sat Sep 03 09:52:28 CST 2022, RpcRetryingCaller{globalStartTime=1662169947866, pause=100, maxAttempts=16}, org.apache.hadoop.hbase.MasterNotRunningException: java.io.IOException: Call to dmp01/10.243.140.76:16000 failed on local exception: java.io.IOException: Failed to specify server's Kerberos principal name
然后网上搜索,但是对应我的问题的解决方案都没有找到直接可行的解决办法,后面也是结合多种查找到的情况才解决我本省的问题。
首先Hbase通过Kerberos认证获取连接的代码如下, 下面代码通过Kerberos认证的方式仅仅获取连接没有问题
private static Connection HBaseConn(String hbaseZookeeperQuorum, String krb5Conf, String principal, String keytabFile) throws Exception{
Configuration configruation = HBaseConfiguration.create();
configruation.set("hadoop.security.authentication", "Kerberos");
configruation.set("hbase.zookeeper.quorum",hbaseZookeeperQuorum);
System.setProperty("java.security.krb5.conf",krb5Conf);
UserGroupInformation.setConfiguration(configruation);
UserGroupInformation.loginUserFromKeytab(principal,keytabFile);
return ConnectionFactory.createConnection(configruation);
}
}
上面的代码执行没有异常,通过Kerberos认证,使用krb5.conf、keytabFile、principal参数可以正常获取连接,但是当你使用获取到的connection进行操作的时候,比如connection.getAdmin()方法时就会报异常。上面代码缺失两块,下面代码是正确的获取Hbase连接的方法
private static Connection HBaseConn(String hbaseZookeeperQuorum, String krb5Conf, String principal, String keytabFile) throws Exception{
Configuration configruation = HBaseConfiguration.create();
configruation.set("hadoop.security.authentication", "Kerberos");
// 必需的参数,否则会报连接关闭的异常,异常为:
/*
Sat Sep 03 10:10:45 CST 2022, RpcRetryingCaller{globalStartTime=1662171044912, pause=100, maxAttempts=16}, org.apache.hadoop.hbase.MasterNotRunningException:
java.io.IOException: Call to dmp01/10.243.140.76:16000 failed on local exception: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
Sat Sep 03 10:10:45 CST 2022, RpcRetryingCaller{globalStartTime=1662171044912, pause=100, maxAttempts=16}, org.apache.hadoop.hbase.MasterNotRunningException:
java.io.IOException: Call to dmp01/10.243.140.76:16000 failed on local exception: java.io.IOException: Connection closed
*/
configruation.set("hbase.security.authentication", "Kerberos");
configruation.set("hbase.zookeeper.quorum",hbaseZookeeperQuorum);
System.setProperty("java.security.krb5.conf",krb5Conf);
// 只是测试获取Kerberos认证获取HBase连接,下面两行不需要都能成功,若要进一步操作Hbase数据库,必须有"hbase.master.kerberos.principal"参数,否则会报错
// org.apache.hadoop.hbase.MasterNotRunningException......,java.io.IOException:Failed to specify server's Kerberos principal name
configruation.set("hbase.master.kerberos.principal", principal);
UserGroupInformation.setConfiguration(configruation);
UserGroupInformation.loginUserFromKeytab(principal,keytabFile);
return ConnectionFactory.createConnection(configruation);
}
两点需要注意:
1、缺少如下的参数设置
configruation.set("hbase.security.authentication", "Kerberos");
则会报异常为
Sat Sep 03 10:10:45 CST 2022, RpcRetryingCaller{globalStartTime=1662171044912, pause=100, maxAttempts=16}, org.apache.hadoop.hbase.MasterNotRunningException: java.io.IOException: Call to dmp01/10.243.140.76:16000 failed on local exception: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。 Sat Sep 03 10:10:45 CST 2022, RpcRetryingCaller{globalStartTime=1662171044912, pause=100, maxAttempts=16}, org.apache.hadoop.hbase.MasterNotRunningException: java.io.IOException: Call to dmp01/10.243.140.76:16000 failed on local exception: java.io.IOException: Connection closed
加上"hbase.security.authentication"参数设置之后,此异常即可解决。
2、缺少如下所示的参数设置
configruation.set("hbase.master.kerberos.principal", principal);
则会报异常为:
Sat Sep 03 09:52:28 CST 2022, RpcRetryingCaller{globalStartTime=1662169947866, pause=100, maxAttempts=16}, org.apache.hadoop.hbase.MasterNotRunningException: java.io.IOException: Call to dmp01/10.243.140.76:16000 failed on local exception: java.io.IOException: Failed to specify server's Kerberos principal name
这个异常就是我遇到的,添加上此参数的设置异常可解决。
具体原因还未去深究,根据搜索内容结合参考解决的,后续找到具体原因,再来完善。写的比较粗糙,当作备忘,也供和我遇到相同问题的小伙伴进行参考。
有问题也可以留言,可以共同讨论学习
参考了下面博文,但是该博主说的必需和非必需的参数设置,和我验证的刚好相反,可能情况不同吧
https://blog.csdn.net/tonseal/article/details/114500794