1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
| package com.etiantian.bigdata
import java.util.Properties import java.util.concurrent.{ExecutorService, Executors}
import org.apache.hadoop.conf.Configuration import org.apache.hadoop.hbase.client._ import org.apache.hadoop.hbase.io.ImmutableBytesWritable import org.apache.hadoop.hbase.mapreduce.TableOutputFormat import org.apache.hadoop.hbase.util.Bytes import org.apache.hadoop.hbase.{HBaseConfiguration, HColumnDescriptor, HTableDescriptor, TableName} import org.apache.hadoop.mapreduce.Job import scala.collection.convert.wrapAsJava._
object HbaseUtil { @transient private var hbaseConn:Connection = null private val hbaseConf: Configuration = HBaseConfiguration.create() private val replaceToken = "#S%P#"
def init(properties: Properties):Unit = { hbaseConf.set("hbase.zookeeper.quorum", properties.getProperty("hbase.zookeeper.quorum")) hbaseConf.set("hbase.zookeeper.property.clientPort", properties.getProperty("hbase.zookeeper.property.clientPort")) synchronized { if (hbaseConn == null) { println("########################## init hbase properties and connection synchronized ##########################") hbaseConn = ConnectionFactory.createConnection( hbaseConf, Executors.newFixedThreadPool(properties.getOrDefault("hbase.poolSize", "8").toString.toInt) ) } } }
def getConn(): Connection = { hbaseConn }
private def getTable(tableName: String): Table = { getConn.getTable(getTableName(tableName)) }
private def getAdmin(): Admin = { getConn().getAdmin }
/** * Change the method to createJob * @param tableName * @param family * @return */ @Deprecated def create(tableName: TableName, family: String) = { hbaseConf.set(TableOutputFormat.OUTPUT_TABLE, tableName.toString)
val job = Job.getInstance(hbaseConf) job.setOutputKeyClass(classOf[ImmutableBytesWritable]) job.setOutputValueClass(classOf[Put]) job.setOutputFormatClass(classOf[TableOutputFormat[ImmutableBytesWritable]])
job }
def createJob(tableName: TableName, family: String) = { hbaseConf.set(TableOutputFormat.OUTPUT_TABLE, tableName.toString)
val job = Job.getInstance(hbaseConf) job.setOutputKeyClass(classOf[ImmutableBytesWritable]) job.setOutputValueClass(classOf[Put]) job.setOutputFormatClass(classOf[TableOutputFormat[ImmutableBytesWritable]])
job }
private def getTableName(tableName: String): TableName = TableName.valueOf(tableName)
def tableExists(tableName: String): Boolean = { getAdmin.tableExists(getTableName(tableName)) }
def createTable(tableName: String, family: String*): Unit = { if (!tableExists(tableName)) { val descriptor = new HTableDescriptor(getTableName(tableName)) family.foreach(x => { val hColumnDescriptor = new HColumnDescriptor(x) descriptor.addFamily(hColumnDescriptor) })
getAdmin.createTable(descriptor) } }
def createTable(tableName: String, family: Seq[String], splits: Seq[String]): Unit = { if (!tableExists(tableName)) { val descriptor = new HTableDescriptor(getTableName(tableName)) family.foreach(x => { val hColumnDescriptor = new HColumnDescriptor(x) descriptor.addFamily(hColumnDescriptor) })
val splitsArray = splits.map(Bytes.toBytes).toArray
getAdmin.createTable(descriptor, splitsArray) } }
def truncTable(tableName: String, preserveSplits: Boolean): Unit = { val admin = getAdmin admin.disableTable(getTableName(tableName)) admin.truncateTable(getTableName(tableName), preserveSplits) }
def truncTable(tableName: String): Unit = { truncTable(tableName, false) }
def dropTable(tableName: String): Unit = { val admin = getAdmin admin.disableTable(getTableName(tableName)) admin.deleteTable(getTableName(tableName)) }
def getValueByKey(tableName: String, key: String): Result = { val table = getTable(tableName) table.get(new Get(Bytes.toBytes(key))) }
def getColumnValue(result: Result, family: String, column: String): String = { val bytesValue = result.getValue(Bytes.toBytes(family), Bytes.toBytes(column)) if (bytesValue == null) null else Bytes.toString(bytesValue) }
def getColumnValue(tableName: String, key: String, family: String, column: String): String = { val result = getValueByKey(tableName, key) getColumnValue(result, family, column) }
def insert(tableName: String, put: Put): Unit = { val table = getTable(tableName) table.put(put) }
def insert(tableName: String, putList: List[Put]): Unit = { val table = getTable(tableName) table.put(putList) }
def insert(tableName: String, key: String, family: String, column: String, value: String): Unit = { val put = new Put(Bytes.toBytes(key)) put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), if (value == null) null else Bytes.toBytes(value)) insert(tableName, put) }
def deleteRow(tableName: String, key: String): Unit = { getTable(tableName).delete(new Delete(Bytes.toBytes(key))) }
def deleteRowList(tableName: String, keyList: List[String]): Unit = { getTable(tableName).delete(keyList.map(key =>new Delete(Bytes.toBytes(key)))) }
def deleteMulti(tableName: String, delList: List[Delete]): Unit = { getTable(tableName).delete(delList) } /** * 插入并替换原有行 */ def replaceRow(tableName: String, put: Put): Unit = { deleteRow(tableName, Bytes.toString(put.getRow)) insert(tableName, put) }
def columnPlus(tableName: String, key: String, family: String, column: String, plusNum: Long): Long = { var columnVal = getColumnValue(tableName, key, family, column).toLong columnVal += plusNum insert(tableName, key, family, column, columnVal.toString) columnVal }
def columnPlus(tableName: String, key: String, family: String, column: String): Long = { columnPlus(tableName, key, family, column, 1) }
def insertList[T](tableName: String, key: String, family: String, column: String, values: List[T], separator: String): Unit = { val put = new Put(Bytes.toBytes(key)) put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), if (values == null) null else { Bytes.toBytes(values.map(x => { x.toString.replaceAll(separator, replaceToken) }).mkString(separator)) }) insert(tableName, put) }
def insertList[T](tableName: String, key: String, family: String, column: String, values: List[T]): Unit = { insertList[T](tableName, key, family, column, values, ",") }
def getList[T](tableName: String, key: String, family: String, column: String, separator: String): List[T] = { val value = getColumnValue(tableName, key, family, column) if (value != null && !value.equals("")) value.split(separator).map(_.replaceAll(replaceToken, separator).asInstanceOf[T]).toList else null }
def getList[T](tableName: String, key: String, family: String, column: String): List[T] = { getList[T](tableName, key, family, column, ",") }
def addToList[T](tableName: String, key: String, family: String, column: String, t: T, separator: String): Unit = { val newValue = t.toString.replaceAll(separator, replaceToken) val value = getColumnValue(tableName, key, family, column) if (value != null && !value.equals("")) insert(tableName, key, family, column, value + separator + newValue) else insert(tableName, key, family, column, newValue) }
def addToList[T](tableName: String, key: String, family: String, column: String, t: T): Unit = { addToList[T](tableName, key, family, column, t, ",") }
/** * 添加到list中,list 会去重 */ def addUniqueToList[T](tableName: String, key: String, family: String, column: String, t: T, separator: String): Unit = { val newValue = t.toString.replaceAll(separator, replaceToken) val value = getColumnValue(tableName, key, family, column) if (value != null && !value.equals("")) { val list = value.split(separator).toList :+ newValue insertList(tableName, key, family, column, list.distinct, separator) } else insert(tableName, key, family, column, newValue) }
/** * 添加到list中,list 会去重 */ def addUniqueToList[T](tableName: String, key: String, family: String, column: String, t: T): Unit = { addUniqueToList(tableName, key, family, column, t, ",") }
def removeFromList[T](tableName: String, key: String, family: String, column: String, t: T, separator: String): Unit = { val newValue = t.toString.replaceAll(separator, replaceToken) val value = getColumnValue(tableName, key, family, column) if (value != null && !value.equals("")) { insertList( tableName, key, family, column, value.split(separator).filter(!_.equals(newValue)).toList, separator ) } }
def removeFromList[T](tableName: String, key: String, family: String, column: String, t: T): Unit = { removeFromList[T](tableName, key, family, column, t, ",") } }
|