Hbase+kerberos 工具类

##Hbase+kerberos 工具类

import java.util

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.{HBaseConfiguration, HColumnDescriptor, HTableDescriptor, TableName, TableNotFoundException}
import org.apache.hadoop.hbase.client.{Admin, Connection, ConnectionFactory, Get, Put, Result, ResultScanner, Scan}
import org.apache.hadoop.security.UserGroupInformation
import org.apache.log4j.Logger

object HbaseUtils {

  private val kerberosConfPath = this.getClass.getClassLoader.getResource("krb5.conf").getFile
  private val principal = "hbaseUser@CDH.COM"
  //private val principal = "hbaseTest@CDH.COM"
  private val keytabPath = this.getClass.getClassLoader.getResource("hbaseUser.keytab").getFile
  private val log: Logger = Logger.getLogger("HbaseUtils")
  var scanner: ResultScanner = null

  def main(args: Array[String]): Unit = {
    val admin: Admin = getHbaseAdim()
    println(admin.listTableNames().mkString("  "))
    createTable(admin, "scalaTable", "info")
    insertTable(admin, "scalaTable", "1002", "info", "sex", "boy")
    insertTable(admin, "scalaTable", "1003", "info", "name", "Tome")
    insertTable(admin, "scalaTable", "1002", "info", "age", "20")
    val iter: util.Iterator[Result] = scanDataFromHTable(admin, "scalaTable")
    while (iter.hasNext) {
      println(iter.next())
    }
    dropTable(admin,"scalaTable")
    try {
      scanner.close()
      admin.close()
    } catch {
      case e: Exception => e.printStackTrace()
    }
    println("aafadf")
  }


  def getHbaseConf: Configuration = {
    val conf = HBaseConfiguration.create

    /*  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
  }

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

  def getHbaseAdim() = {
    val connection = HbaseUtils.getConnection
    val admin: Admin = connection.getAdmin
    admin
  }

  def scanDataFromHTable(admin: Admin, tableName: String) = {
    try {
      val connection: Connection = admin.getConnection
      val table = connection.getTable(TableName.valueOf(tableName))
      //定义scan对象
      val scan = new Scan()
      //添加列簇名称
      //scan.addFamily(columnFamily.getBytes())
      //从table中抓取数据来scan
      scanner = table.getScanner(scan)
      val iter: util.Iterator[Result] = scanner.iterator()
      iter
    } catch {
      case e: Exception => {
        e.printStackTrace()
        val iter: util.Iterator[Result] = new util.ArrayList[Result]().iterator()
        iter
      }
    }
  }

  def insertTable(admin: Admin, tableName: String, rowkey: String, columnFamily: String, column: String, value: String) = {
    try {
      val connection: Connection = admin.getConnection
      val table = connection.getTable(TableName.valueOf(tableName))
      //准备key 的数据
      val puts = new Put(rowkey.getBytes())
      //添加列簇名,字段名,字段值value
      puts.addColumn(columnFamily.getBytes(), column.getBytes(), value.getBytes())
      //把数据插入到tbale中
      table.put(puts)
      println("insert successful!!")
    } catch {
      case e: Exception => e.printStackTrace()
    }
  }

  def createTable(admin: Admin, tableName: String, columns: String*) = {

    val table: TableName = TableName.valueOf(tableName)
    if (!admin.tableExists(table)) {
      val descriptor: HTableDescriptor = new HTableDescriptor(table)
      for (elem <- columns) {
        descriptor.addFamily(new HColumnDescriptor(elem))
      }
      admin.createTable(descriptor)
      log.info("表创建成功!")
    } else {
      log.info("表已经存在")
    }
  }

  def dropTable(admin: Admin, tableName: String) = {
    try {
      admin.disableTable(TableName.valueOf(tableName))
      admin.deleteTable(TableName.valueOf(tableName))
      println("drop successful!!")
    } catch {
      case e: TableNotFoundException => e.printStackTrace()
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MrSun329

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值