hbase的认证

1 篇文章 0 订阅
1 篇文章 0 订阅

现在很多的公司都开始使用了kerberos认证,进行权限控制.下面我就说说hbase的kerberos认证.

def putLogsToHTable(p: Iterator[(HBaseKey,String)],bp:Broadcast[Properties]):Unit = {
  val (ugi,hBaseConf): (UserGroupInformation,Configuration) = getUGIAndConf()
UserGroupInformation.setLoginUser(ugi)
val hbaseTableName:TableName = TableName.valueOf(bp.value.getProperty("spark.netlog_hbase_namespace"))

ugi.doAs(new PrivilegedExceptionAction[Unit]{
        override def run() = {
            val conn = ConnectionFactory.createConnection(hBaseConf)
            val hbaseTable = conn.getTable(hbaseTableName)
            val puts = new util.ArrayList[Put]()
            p.foreach(line => {
                    val (hRow,hFamily,hQualifier) = line._1
                    val put = new Put(Bytes.toBytes(hRow)).add(hFamily.getBytes,hQualifier.getBytes,Bytes.toBytes(line._2))
                    puts.add(put)
                })
        }
        hbaseTable.put(puts)
        hbaseTable.close()
        conn.close()
    }
)
def getUGIAndConf(bp:Broadcast[Properties]):(UserGroupInformation,Configuration) = {
    val hBaseConf = getHBaseConf(bp)
    val ugi = kinit(hBaseConf,bp)
    (ugi,hBaseConf)
}
def getHBaseConf(bp:Broadcast[Properties]):Configuration = {
    val hBaseConf = HBaseConfiguration.create()
    val hBaseSiteXML = bp.value.getProperty("HBASE_SITE_XML")
    val hdfsSiteXML = bp.value.getProperty("HDFS_SITE_XML")
    val coreSiteXML = bp.value.getProperty("CORE_SITE_XML")

    hBaseConf.addResource(hdfsSite.XML)
    hBaseConf.addResource(hBaseSite.XML)
    hBaseConf.addResource(coreSite.XML)
    hBaseConf.set("hadoop.security.authentication","kerberos")
    hBaseConf.set("hbase.security.authentication","kerberos")    
}
def kinit(hadoopConfig: Configuration, bp: Broadcast[Properties]): UserGroupInformation = {
    UserGroupInformation.setConfiguration(hadoopConfig)
    
    val userName = bp.value.getProperty("HBASE_KEYTAB_USER")
    //这里使用的是通过获取当前线程的文件,来加载path路径,如果直接给文件名,可能加载不到,导致连接拒绝
    val ketTableFile = Thread.currentThread().getContextClassLoader.getResource(bp.value.getProperty("HBASE_KEYTAB_FILE")).getPath
    
    UserGroupInformation.loginUserFromKeytableAndReturnUGI(userName,keyTableFile)
}



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值