Hbase2.0.2のscala API

6633 ワード

import java.util
import java.util.{ArrayList, HashMap, List, Map}
import org.apache.hadoop.hbase.{Cell, CompareOperator, HBaseConfiguration, TableName}
import org.apache.hadoop.hbase.client._
import org.apache.hadoop.hbase.filter._
import org.apache.hadoop.hbase.util.Bytes
import org.apache.spark.SparkConf
import scala.collection.JavaConversions._
object HbaseScala {

  def main(args: Array[String]): Unit = {
     System.setProperty("hadoop.home.dir", "D:\\hadoop-common-bin-2.7.x")
     val sparkConf = new SparkConf().setAppName("HBaseTest")
     val conf = HBaseConfiguration.create
     val tableName:TableName =TableName.valueOf("blog_scala")
     conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03")
     conf.set("hbase.zookeeper.property.clientPort", "2181")


      //Connection    hbase   
      val connection= ConnectionFactory.createConnection(conf)
      //     (   )
//      createHTable(connection, tableName,"area")

      //   (   )
      val columnFamilys: Array[String] = Array("article", "author")
//      createHTable(connection, tableName,columnFamilys)

      val listMap: List[Map[String, AnyRef]] = new ArrayList[Map[String, AnyRef]]
      val map1: Map[String, AnyRef] = new HashMap[String, AnyRef]
      map1.put("rowKey", "ce_shi1")
      map1.put("columnFamily", "article")
      map1.put("columnName", "title")
      map1.put("columnValue", "Head First HBase")
      listMap.add(map1)
      val map2: Map[String, AnyRef] = new HashMap[String, AnyRef]
      map2.put("rowKey", "ce_shi1")
      map2.put("columnFamily", "article")
      map2.put("columnName", "content")
      map2.put("columnValue", "HBase is the Hadoop database")
      listMap.add(map2)
      val map3: Map[String, AnyRef] = new HashMap[String, AnyRef]
      map3.put("rowKey", "ce_shi1")
      map3.put("columnFamily", "article")
      map3.put("columnName", "tag")
      map3.put("columnValue", "Hadoop,HBase,NoSQL")
      listMap.add(map3)
      val map4: Map[String, AnyRef] = new HashMap[String, AnyRef]
      map4.put("rowKey", "ce_shi1")
      map4.put("columnFamily", "author")
      map4.put("columnName", "name")
      map4.put("columnValue", "nicholas")
      listMap.add(map4)
      val map5: Map[String, AnyRef] = new HashMap[String, AnyRef]
      map5.put("rowKey", "ce_shi1")
      map5.put("columnFamily", "author")
      map5.put("columnName", "nickname")
      map5.put("columnValue", "lee")
      listMap.add(map5)
      val map6: Map[String, AnyRef] = new HashMap[String, AnyRef]
      map6.put("rowKey", "ce_shi2")
      map6.put("columnFamily", "author")
      map6.put("columnName", "name")
      map6.put("columnValue", "spark")
      listMap.add(map6)
      val map7: Map[String, AnyRef] = new HashMap[String, AnyRef]
      map7.put("rowKey", "ce_shi2")
      map7.put("columnFamily", "author")
      map7.put("columnName", "nickname")
      map7.put("columnValue", "hadoop")
      listMap.add(map7)
//      insertMany(connection, tableName,listMap);
//      insertMany(connection,tableName)

      //      
//      insertSingle(connection,tableName)

      //  RowKey,  ,     
      val rowKey = "ce_shi2"
      val columnFamily = "author"
      val columnName = "name"
      val columnValue = "hbase"
//      updateData(connection,tableName,rowKey,columnFamily,columnName,columnValue);

      val rowKey1 = "rowKey5"
      val columnFamily1 = "author"
      val columnName1 = "tag"
      val columnNames = new util.ArrayList[String]
      columnNames.add("name")
      columnNames.add("nickname")
      //            
//      deleteData(connection,tableName,rowKey1,columnFamily1,columnName1);
      //        
//      deleteData(connection,tableName,rowKey1,columnFamily1);
      //            
//      deleteData(connection,tableName,rowKey1,columnFamily1,columnNames);
      //    
      deleteData(connection,tableName,rowKey1);

    //  RowKey    
//      getResult(connection,tableName,"rowKey5")

      //    
      scanTable(connection,tableName)

      //rowKey   
//      rowkeyFilter(connection,tableName)

      //     
//      singColumnFilter(connection,tableName)

      //       
//      columnPrefixFilter(connection,tableName)

      //     
//      filterSet(connection,tableName)
  }

  /**
    *    (      )
    * @param connection
    * @param tableName
    */
  def createHTable(connection: Connection,tableName: TableName,columnFamily:String): Unit=
  {
    val admin = connection.getAdmin
    if (!admin.tableExists(tableName)) {
      //       
      val tdb = TableDescriptorBuilder.newBuilder(tableName)
      //        
      val cdb:ColumnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily))
      //      
      val cfd: ColumnFamilyDescriptor = cdb.build
      //    
      tdb.setColumnFamily(cfd)
      //      
      val td = tdb.build
      //   
      admin.createTable(td)
      println("create done.")
    }else{
      print("    ")
    }

  }

  /**
    *    (   )
    * @param connection
    * @param tableName
    * @param columnFamilys
    */
  def createHTable(connection: Connection,tableName: TableName, columnFamilys: Array[String]): Unit ={
    val admin=connection.getAdmin
    if(!admin.tableExists(tableName)){
      //       
      val tdb = TableDescriptorBuilder.newBuilder(tableName)
      var cdb:ColumnFamilyDescriptorBuilder=null
      //      
      var cfd: ColumnFamilyDescriptor=null
      for (columnFamily  0) {
      for (map   GREATER_OR_EQUAL >=   GREATER >   NO_OP     
  /**
    * rowKey   
    * @param connection
    * @param tableName
    */
  def rowkeyFilter(connection: Connection,tableName: TableName): Unit = {
    val table = connection.getTable(tableName)
    val scan = new Scan
    val filter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator("Key1$")) //str$     ,   sql   %str  ^str    ,   sql  str%
    scan.setFilter(filter)
    val scanner = table.getScanner(scan)
    for (rs