Hbase scala Utills

import com.feidee.bigdata.realtime.entities.Constants
import org.apache.hadoop.hbase.{HBaseConfiguration, HColumnDescriptor, HTableDescriptor, TableName}
import org.apache.hadoop.hbase.client._
import org.apache.hadoop.hbase.io.compress.Compression
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.util.RegionSplitter.{HexStringSplit, SplitAlgorithm}
import org.slf4j.LoggerFactory

/**

  • USER:xuying_xie
  • DATE:2018/11/13
  • INFO:
    */
    object HbaseUtils {

private val logger = LoggerFactory.getLogger(HbaseUtils.getClass)
lazy val conn = getHConnection()

private def getHConnection(): Connection = {
try {
val conf = HBaseConfiguration.create
conf.set(“hbase.zookeeper.quorum”, Constants.COMMON_CONF_PROPERTIES.getProperty(“zookeeper.quorum”))
conf.set(“fs.hdfs.impl”, “org.apache.hadoop.hdfs.DistributedFileSystem”)
conf.set(“fs.file.impl”, “org.apache.hadoop.fs.LocalFileSystem”)
conf.set(“fs.hdfs.impl.disable.cache”, “true”)
ConnectionFactory.createConnection(conf)
} catch {
case e: Exception => {
logger.error(“get connection to hbase error”, e)
null
}
}
}

def getTable(name: String): Table = {
this.conn.getTable(TableName.valueOf(name))
}

def createTable(tableName: String, cf: String, splitCount: Int): Unit = {
val hTable = TableName.valueOf(tableName)
val admin = this.conn.getAdmin()
val splitAlgo: SplitAlgorithm = new HexStringSplit()
try {
if (admin.tableExists(hTable)) {
logger.error(“table already exists:{}”, tableName)
return
}
val desc: HTableDescriptor = new HTableDescriptor(hTable)
val hColumnDescriptor = new HColumnDescriptor(Bytes.toBytes(cf))
hColumnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY)
desc.addFamily(hColumnDescriptor)
admin.createTable(desc, splitAlgo.split(splitCount))
} catch {
case e: Exception => logger.error(s"create table:${tableName} exception", e)
} finally {
admin.close()
}
}

def addCoprocessor(tableName: String): Unit = {
logger.info(“start to add coprocessor for table:{}”, tableName)
val hTable = TableName.valueOf(tableName)
val admin = this.conn.getAdmin()
admin.disableTable(hTable)
val htd = admin.getTableDescriptor(hTable)
try {
val className = “org.apache.hadoop.hbase.coprocessor.AggregateImplementation”
if (htd.hasCoprocessor(className)) {
logger.error(“coprocessor has already exists for table:{}”, tableName)
return
}
htd.addCoprocessor(className)
admin.modifyTable(hTable, htd)
admin.enableTable(hTable)
} catch {
case e: Exception => logger.error(s"add coprocessor for table:${tableName} exception", e)
} finally {
admin.close()
}
}

/**
* 创建快照
*
* @param name
* @param date
*/
def createSnapshot(name: String, date: String): Unit = {
val snapshotName = s"KaTeX parse error: Expected group after '_' at position 34: …NAPSHOT_PREFIX}_̲{name}_${date}"
val admin = this.conn.getAdmin
logger.info(“create snapshot:{}”, snapshotName)
if (admin.listSnapshots(snapshotName).isEmpty) {
admin.snapshot(snapshotName, TableName.valueOf(name))
logger.info(“create snapshot:{} successfully”, snapshotName)
} else {
logger.error(“snapshot already exists:{}”, snapshotName)
}
admin.close()
this.conn.close()
}

/**
* 删除快照
*
* @param name
* @param date
*/
def deleteSnapshot(name: String, date: String): Unit = {
val snapshotName = s"KaTeX parse error: Expected group after '_' at position 34: …NAPSHOT_PREFIX}_̲{name}_${date}"
val admin = this.conn.getAdmin
logger.info(“delete snapshot:{}”, snapshotName)
if (!admin.listSnapshots(snapshotName).isEmpty) {
admin.deleteSnapshot(snapshotName)
logger.info(“delete snapshot:{} successfully”, snapshotName)
} else {
logger.error(“snapshot doesn’t exists:{}”, snapshotName)
}
admin.close()
this.conn.close()
}

/**
* 恢复快照
*
* @param name
* @param date
*/
def restoreSnapshot(name: String, date: String): Unit = {
val snapshotName = s"KaTeX parse error: Expected group after '_' at position 34: …NAPSHOT_PREFIX}_̲{name}_${date}"
val admin = this.conn.getAdmin
logger.info(“restore snapshot:{}”, snapshotName)
if (!admin.listSnapshots(snapshotName).isEmpty) {
admin.disableTable(TableName.valueOf(name))
admin.restoreSnapshot(snapshotName)
admin.enableTable(TableName.valueOf(name))
logger.info(“restore snapshot:{} successfully”, snapshotName)
} else {
logger.error(“snapshot doesn’t exists:{}”, snapshotName)
}
admin.close()
this.conn.close()
}

def closeTable(table: Table): Unit = {
try {
if (table != null) {
table.close()
logger.info(“closed table {}”, table.getName)
}
} catch {
case e: Exception => logger.error(“close hbase table failed”, e)
}
}

def destroy(): Unit = {
try {
if (this.conn != null) {
this.conn.close()
logger.info(“closed hbase connection”)
}
} catch {
case e: Exception => logger.error(“close hbase connection failed”, e)
}
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值