HBAse CRUDのscalaコード例

5516 ワード

POM構成
    
        UTF-8
        UTF-8
        1.8
        1.6.0
        2.10.4
        2.10
        1.2.0-cdh5.8.2
    


    
        
            org.apache.hbase
            hbase-client
            ${hbase.version}
        
        
            org.apache.hbase
            hbase-common
            ${hbase.version}
        
        
            org.apache.hbase
            hbase-server
            ${hbase.version}
        
    

コードの例
導入パッケージ
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.{ ConnectionFactory, Admin, Put, Delete, Get, Table, ResultScanner, Scan, Result }
import org.apache.hadoop.hbase.{ TableName, HTableDescriptor, HColumnDescriptor }
import org.apache.hadoop.hbase.util.Bytes

初期準備
val conf = HBaseConfiguration.create()
conf.set("hbase.zookeeper.quorum", "sparkmaster1"); 
val conn = ConnectionFactory.createConnection(conf)
val admin = conn.getAdmin
val wetagTable = TableName.valueOf("wetag")

テーブルの削除
if (admin.tableExists(wetagTable)) {
    println("drop table `wetag` ")
    admin.disableTable(wetagTable)
    admin.deleteTable(wetagTable)
    println("done")
}

テーブルの作成
val tableDescr = new HTableDescriptor(wetagTable)
tableDescr.addFamily(new HColumnDescriptor("attr".getBytes))
tableDescr.addFamily(new HColumnDescriptor("bhvr".getBytes))
    
if (!admin.tableExists(wetagTable)) {
    println("Creating table `wetag` ")
    admin.createTable(tableDescr)
    println("Table `wetag` created  ")
}

レコードの挿入(更新)
val key="12345678"
val family="attr"
val column="population.age"
val value="30"
val table=conn.getTable(wetagTable)

val p=new Put(key.getBytes)
p.addColumn(family.getBytes,column.getBytes,value.getBytes())
table.put(p)


val family="attr"
val column="population.sex"
val value="male"
val table=conn.getTable(wetagTable)

val p=new Put(key.getBytes)
p.addColumn(family.getBytes,column.getBytes,value.getBytes())
table.put(p)

val family="attr"
val column="population.age"
val value="45"
val table=conn.getTable(wetagTable)

val p=new Put(key.getBytes)
p.addColumn(family.getBytes,column.getBytes,value.getBytes())
table.put(p)

レコードの削除
val key="12345678"
val family="attr"
val column="population.age"
val value="30"
val table=conn.getTable(wetagTable)

val d = new Delete(key.getBytes)
d.addColumn(family.getBytes,column.getBytes)
table.delete(d)

レコードのクエリー
val key="12345678"
val family="attr"
val column="population.sex"

val table=conn.getTable(wetagTable)
val g = new Get(key.getBytes)
val result = table.get(g)
val value = Bytes.toString(result.getValue(family.getBytes, column.getBytes))

println("GET: " + key + " " + value)

スキャンレコード
var table:Table=null
var scanner:ResultScanner=null
val family="attr"
val column="population.sex"

try{
      table=conn.getTable(wetagTable)
      val s=new Scan()
      s.addColumn(family.getBytes(),column.getBytes())
      scanner=table.getScanner(s)
      println("scan...for...")
      
      var result:Result=scanner.next()
      while(result!=null) {
        println("Found row:" + result)
        println("Found value: "+Bytes.toString(result.getValue(family.getBytes(),column.getBytes())))
        result=scanner.next()
      }
} finally {
      if(table!=null)
        table.close()
      scanner.close()
}

列の削除
   val conf = HBaseConfiguration.create()
    conf.set("hbase.zookeeper.quorum", "zk1,zk2,zk3");
    val conn = ConnectionFactory.createConnection(conf)


    var table:Table=null
    var scanner:ResultScanner=null
    val family="info"
    val column="uuid"
    val wetagTable = TableName.valueOf("tb")

    try{
      table=conn.getTable(wetagTable)
      val s=new Scan()
      s.addColumn(family.getBytes(),column.getBytes())
      scanner=table.getScanner(s)
      println("scan...for...")

      var result:Result=scanner.next
      while(result!=null) {
        val rowkey = result.getRow
        println("Found rowkey: " + Bytes.toString(rowkey) + " " + family +":"+column +": " + Bytes.toString(result.getValue(family.getBytes, column.getBytes)))

        val delCell = new Delete(rowkey)
        delCell.addColumns(family.getBytes, column.getBytes)
        table.delete(delCell)

        result=scanner.next
      }
    } finally {
      if(table!=null)
        table.close()
      scanner.close()
    }