hbase APIはscalaの書き方でhbaseテーブルデータを操作し、非常に簡単です

25943 ワード

pomファイルは次のように依存します.
	<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>


コード実装
package Scala

import java.util

import Scala.TestHBASE02._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase._
import org.apache.hadoop.hbase.client._
import org.apache.hadoop.hbase.util.Bytes

import scala.collection.mutable.ArrayBuffer

object HbaseUtils {
  //   hbase    
  val con: Configuration = HBaseConfiguration.create()
  //   zookeeperjiqun
  con.set("hbase.zookeeper.quorum","node2:2181,node3:2181,node4:2181")
  //     
  val conf: Connection = ConnectionFactory.createConnection(con)
  //      
  val admin: Admin = conf.getAdmin
  //        ,            
  val tableName: TableName = TableName.valueOf("student")
  //          ,      
  val table: Table = conf.getTable(tableName)

  def main(args: Array[String]): Unit = {
    // create(List("info","data"))
    // insert("rk001","info","name","lisi001")
    /* inserAll(Array(
        ("rk001","info","name","lisi001"),("rk001","info","age","18"),
      ("rk002","data","name","lisi002"),("rk002","data","age","19")))*/

    // addColumnFamily("time")
    // select()
    // selectRow("rk001")
     deleteRow("rk001")
  }

  //todo:    hbase         ,      
  //create 'student','info'
  def create(columnFamily:List[String]): Unit ={
    //   hbase    
    val tname = new HTableDescriptor(tableName)
    //    ,         
    columnFamily.foreach(x=>tname.addFamily(new HColumnDescriptor(x)))
    admin.createTable(tname)
    println("     ")
  }

  //todo:      
  //put 'student','rk001','info:name','zhangsan'
  def insert(row:String, columnFamily: String, column: String, value: String): Unit ={
    //   rowkey
    val puts = new Put(row.getBytes)
    puts.addColumn(columnFamily.getBytes, column.getBytes, value.getBytes)
    table.put(puts)
    println("      ")
  }

  //todo:      
  def inserAll(array: Array[(String,String,String,String)]): Unit ={
    // java  util
    val puts = new util.ArrayList[Put]()
    array.foreach(x=>{
      puts.add(new Put(x._1.getBytes()).addColumn(x._2.getBytes(),x._3.getBytes(),x._4.getBytes()))
    })
    table.put(puts)
    println("        ")
  }

  //todo:    
  //alter 'student', NAME => 'data'
  def addColumnFamily(columnFamily: String): Unit = {
    //    
    val tableDescriptor: HTableDescriptor = admin.getTableDescriptor(tableName)
    //      
    val columnDescriptor = new HColumnDescriptor(columnFamily)
    //      
    columnDescriptor.setVersions(2,5)
    //            
    tableDescriptor.addFamily(columnDescriptor)
    //    ,      ,     
    admin.modifyTable(tableName,tableDescriptor)
    println("      ")
  }

  // todo:       
  def select(): Unit ={
    val scan = new Scan()
    val resultScanner: ResultScanner = table.getScanner(scan)
    var result: Result = resultScanner.next()
    while (result!=null){
      val cells: Array[Cell] = result.rawCells()
      cells.foreach(x=>println(
        Bytes.toString(CellUtil.cloneRow(x))+"  "+          //   row
          Bytes.toString(CellUtil.cloneFamily(x))+"  "+     //     
          Bytes.toString(CellUtil.cloneQualifier(x))+"  "+ //     
          Bytes.toString(CellUtil.cloneValue(x))))        //    
      //       
      result = resultScanner.next()
    }


  }

  // todo:         
  def selectRow(row:String): Unit ={
    val get = new Get(row.getBytes)
    val result: Result = table.get(get)
    result.rawCells().foreach(println)
  }

  // todo:         
  def deleteRow(row:String): Unit ={
    val delete = new Delete(row.getBytes)
    table.delete(delete)
    println(s"${row}    ")
  }


  //    
  def close(): Unit = {
    if (admin != null)
      admin.close()
    if (connection != null)
      connection.close()
  }
}