使用scala操作hbase api
最近在研究hbase 和scala,研究scala是為了spark,剛好hbase的api我也不熟,scala也不熟悉,所以就用來練手了
程式碼是兩個類
ConfigUtil 是用來產生configuration的
TestHbaeJavaApi是用來測試hbase api的
jar包是用的cdh5.7.1的版本
package com.hit.hrb
import org.apache.hadoop.conf.Configuration import org.apache.hadoop.hbase.HBaseConfiguration
/** * Created by zh on 2016/8/16. */ class ConfigUtil private {
def createHbaseConfig { val conf: Configuration = HBaseConfiguration.create() conf.addResource("hbase-site.xml") conf }
def createHadoopConfig ={ val conf: Configuration = new Configuration() conf.addResource("core-site.xml") conf.addResource("hbase-site.xml") conf.addResource("hbase-site.xml") conf } }
object ConfigUtil{ def apply: ConfigUtil = new ConfigUtil()
}
package com.hit.hrb
import java.util
import org.apache.hadoop.conf.Configuration import org.apache.hadoop.hbase.client._ import org.apache.hadoop.hbase.util.Bytes import org.apache.hadoop.hbase.{Cell, HColumnDescriptor, HTableDescriptor, KeyValue}
import collection.JavaConverters._
/** * Created by zh on 2016/8/16. */ object TestHbaeJavaApi {
private val conf: Configuration = ConfigUtil.apply.createHadoopConfig
def isExist(tableName: String) { val hAdmin: HBaseAdmin = new HBaseAdmin(conf) hAdmin.tableExists(tableName) }
def createTable(tableName: String, columnFamilys: Array[String]): Unit = { val hAdmin: HBaseAdmin = new HBaseAdmin(conf) if (hAdmin.tableExists(tableName)) { println("表" + tableName + "已經存在") return } else { val tableDesc: HTableDescriptor = new HTableDescriptor(tableName) for (columnFaily <- columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(columnFaily)) } hAdmin.createTable(tableDesc) println("建立表成功") } }
def deleteTable(tableName: String): Unit = { val admin: HBaseAdmin = new HBaseAdmin(conf) if (admin.tableExists(tableName)) { admin.disableTable(tableName) admin.deleteTable(tableName) println("刪除表成功!") } else { println("表" + tableName + " 不存在") } }
def addRow(tableName: String, row: String, columnFaily: String, column: String, value: String): Unit = { val table: HTable = new HTable(conf, tableName) val put: Put = new Put(Bytes.toBytes(row)) put.add(Bytes.toBytes(columnFaily), Bytes.toBytes(column), Bytes.toBytes(value)) table.put(put) }
def delRow(tableName: String, row: String): Unit = { val table: HTable = new HTable(conf, tableName) val delete: Delete = new Delete(Bytes.toBytes(row)) table.delete(delete) }
def delMultiRows(tableName: String, rows: Array[String]): Unit = { val table: HTable = new HTable(conf, tableName) val deleteList = for (row <- rows) yield new Delete(Bytes.toBytes(row)) table.delete(deleteList.toSeq.asJava) }
def getRow(tableName: String, row: String): Unit = { val table: HTable = new HTable(conf, tableName) val get: Get = new Get(Bytes.toBytes(row)) val result: Result = table.get(get) for (rowKv <- result.raw()) { println(new String(rowKv.getFamily)) println(new String(rowKv.getQualifier)) println(rowKv.getTimestamp) println(new String(rowKv.getRow)) println(new String(rowKv.getValue)) } }
def getAllRows(tableName: String): Unit = { val table: HTable = new HTable(conf, tableName) val results: ResultScanner = table.getScanner(new Scan()) val it: util.Iterator[Result] = results.iterator() while (it.hasNext) { val next: Result = it.next() for(kv <- next.raw()){ println(new String(kv.getRow)) println(new String(kv.getFamily)) println(new String(kv.getQualifier)) println(new String(kv.getValue)) println(kv.getTimestamp) println("---------------------") }
// val cells: Array[Cell] = next.rawCells() // for (cell <- cells) { // println(new String(cell.getRowArray)+" row") // println(new String(cell.getFamilyArray)) // println(new String(cell.getQualifierArray)) // println(new String(cell.getValueArray)) // println(cell.getTimestamp) // println("---------------------") // }
} }
def main(args: Array[String]) { //TestHbaeJavaApi.createTable("testApi",Array("info","two")) //TestHbaeJavaApi.addRow("testApi","row2","info","get","getTwo") //TestHbaeJavaApi.delRow("testApi","row2")
//TestHbaeJavaApi.getRow("testApi","row1") this.getAllRows("testApi")
}
}