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