##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()
}
}
}