写数据到hbase

package com.bnls.test.hbase

import org.apache.hadoop.hbase._
import org.apache.hadoop.hbase.client.{ConnectionFactory, Delete, HTable, Put}
import org.apache.hadoop.hbase.util.Bytes

import scala.util.Try
import scala.util.parsing.json.JSON

object HbaseHelper {

// Hbase 简要配置以及开启服务
val hbaseConf = HBaseConfiguration.create()
hbaseConf.set("hbase.zookeeper.quorum", "10.60.81.168,10.60.81.167,10.60.81.166")
hbaseConf.set("hbase.zookeeper.property.clientPort", "2181")
// hbaseConf.set("hbase.defaults.for.version.skip", "true")
val connHbase = ConnectionFactory.createConnection(hbaseConf)
//val admin = new HBaseAdmin(hbaseConf)
val admin = connHbase.getAdmin()

//判断表空间是否成在
def isExistsNamespace(strNamespace: String): Boolean = {
val namespaces = admin.listNamespaceDescriptors
for (c <- namespaces) {
if (strNamespace.equals(c.getName)) return true
}
false
}

var tableName :String = null

def insert2hbase(data: String): Unit ={
try {
val jsonS = JSON.parseFull(data)
val jsonObj = jsonS match {
case Some(map: Map[String, Any]) => map
}
val headObj = jsonObj.get("head").get.asInstanceOf[Map[String, String]]
val tableStr = headObj.get("table").getOrElse("")
val dbStr = headObj.get("db").getOrElse("")
val typeStr = headObj.get("type").getOrElse("")
val afterObj = jsonObj.get("after").get.asInstanceOf[Map[String, String]]

val rowKey = afterObj.get("rowKey").getOrElse("")

if (tableStr.contains("_00")) {
tableName = tableStr.substring(0, tableStr.length - 5)
} else {
tableName = tableStr
}

//val put = new Put(Bytes.toBytes(java.util.UUID.randomUUID().toString))

val put = new Put(Bytes.toBytes(rowKey))

val tableNameStr = TableName.valueOf(dbStr, tableName)
val tableOBJ = new HTable(hbaseConf, tableNameStr)

if (typeStr == "DELETE") {
tableOBJ.delete(new Delete(rowKey.getBytes()))
} else {
if (!admin.isTableAvailable(tableNameStr)) {
print("Table Not Exists! Create Table")

if (!isExistsNamespace(dbStr)) {
val namespaceDescriptor = NamespaceDescriptor.create(dbStr).build
admin.createNamespace(namespaceDescriptor)
}
val tableOBJ01 = new HTableDescriptor(tableNameStr)
tableOBJ01.addFamily(new HColumnDescriptor(tableName.getBytes()))
admin.createTable(tableOBJ01)
admin.close()
afterObj.keysIterator.foreach { x =>
put.add(Bytes.toBytes(tableName), Bytes.toBytes(x), Bytes.toBytes(afterObj.get(x).getOrElse("")))
Try(tableOBJ.put(put)).getOrElse(tableOBJ.close())
}
} else {
print("Table Exists! not Create Table")
afterObj.keysIterator.foreach { x =>
put.add(Bytes.toBytes(tableName), Bytes.toBytes(x), Bytes.toBytes(afterObj.get(x).getOrElse("")))
Try(tableOBJ.put(put)).getOrElse(tableOBJ.close())
}
}
}
} catch {
case e: Exception =>
println(s"不规则数据 " + e.getMessage)
}
}

}

posted @ 2018-12-04 15:21  何国秀_xue  阅读(295)  评论(0编辑  收藏  举报