Java连接Kerberos认证的Hbase

参数

  • zookeeperQuorum:zookeeper主机名/IP
  • clientPort:zookeeper端口
  • znodeParent:Hbase在zookeeper中的元数据节点(可在Hbase配置文件中或者ZkCli中查看该配置)
  • kerberosConfPath:krb5.conf路径(可放在项目的resurces目录下)
  • principal:具有访问权限的Kerberos票据
  • keytabPath:Kerberos票据对应的keytab路径

Scala版代码

连接代码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.security.UserGroupInformation;

public class HbaseConfUtil {
    private String zookeeperQuorum = "xxx.xx.xxx.xxx";
    private String clientPort = "2181";
    private String znodeParent = "/hbase-secure";
    private String kerberosConfPath = "/etc/krb5.conf";
    private String principal = "[email protected]";
    private String keytabPath = "/etc/securty/keytabs/spark.xxxx.keytab";

    public Configuration getHbaseConf() {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", zookeeperQuorum);
        conf.set("hbase.zookeeper.property.clientPort", clientPort);
        conf.set("zookeeper.znode.parent", znodeParent);
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 30000);
        conf.setInt("hbase.client.scanner.timeout.period", 200000);
        conf.set("hadoop.security.authentication", "kerberos");
        conf.set("hbase.security.authentication", "kerberos");
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
        System.setProperty("java.security.krb5.conf", kerberosConfPath);

        return conf;
    }

    public synchronized Connection getConnection() throws Exception {
        Connection connection = null;
        Configuration conf = getHbaseConf();
		// 使用票据登陆Kerberos
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(principal, keytabPath);

        connection = ConnectionFactory.createConnection(conf);
        return connection;
    }

    public static void main(String[] args) throws Exception {
        HbaseConfUtil hbaseConfUtil = new HbaseConfUtil();
        Connection connection = hbaseConfUtil.getConnection();
        TableName info = TableName.valueOf("DB_TYPE_INFO");
        Table table = connection.getTable(info);

        System.out.println(table);
    }
}

你可能感兴趣的:(大数据,Kerberos,Hbase)