Spark(Scala)连接带Kerberos认证的Hbase

参数

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

Java版代码

连接代码

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}
import org.apache.hadoop.hbase.client.{Connection, ConnectionFactory}
import org.apache.hadoop.security.UserGroupInformation

object HbaseUtils {
	private val zookeeperQuorum = "xxx.xx.xxx.150"
	private val clientPort = "2181"
	private val znodeParent = "/hbase-secure"
	private val kerberosConfPath = "/etc/krb5.conf"
	private val principal = "spark-xxxx@XXXX.COM"
	private val keytabPath = "/etc/securty/keytabs/spark.xxxx.keytab"

	def getHbaseConf: Configuration = {
		val 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)
		conf
	}

	@throws[Exception]
	def getConnection: Connection = {
		var connection: Connection = null
		val conf = getHbaseConf
		UserGroupInformation.setConfiguration(conf)
		UserGroupInformation.loginUserFromKeytab(principal, keytabPath)
		connection = ConnectionFactory.createConnection(conf)
		connection
	}

	@throws[Exception]
	def main(args: Array[String]): Unit = {
		val connection = HbaseUtils.getConnection
		val info = TableName.valueOf("DB_TYPE_INFO")
		val table = connection.getTable(info)
		println(table)
	}
}
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页