SCALA操作HBASE2.1

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/jzdzhiyun/article/details/82759331
package scala

import java.io.IOException
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter
import org.apache.hadoop.hbase.filter.SubstringComparator
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.client._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase._
import java.util.UUID
import org.slf4j.LoggerFactory

object ScalaHbase {
  def LOG = LoggerFactory.getLogger(getClass)

  def getHbaseConf: Configuration = {
    val conf: Configuration = HBaseConfiguration.create
    //hbase-site.xml配置文件拷贝到工程中来,注意应用程序的相对路径
    conf.addResource("../hbase-site.xml")
    /*conf.set("hbase.zookeeper.property.clientPort", "2181")
    //conf.set("spark.executor.memory", "3000m")
    conf.set("hbase.zookeeper.quorum", "jzd.note1.com,jzd.note2.com,jzd.note3.com")
    conf.set("hbase.master", "jzd.note1.com:60000")
    //conf.set("hbase.rootdir", Constant.HBASE_ROOTDIR)*/
    conf
  }

  @throws(classOf[MasterNotRunningException])
  @throws(classOf[ZooKeeperConnectionException])
  @throws(classOf[IOException])
  def createTable(hbaseconn: Connection, tablename: String, columnFamily: Array[String]) {
    //建立一个数据库操作对象
    var admin:Admin = hbaseconn.getAdmin();
    var myTableName :TableName = TableName.valueOf(tablename)
    if (admin.tableExists(myTableName)) {
      LOG.info(tablename+" Table exists!")
      //val tableDesc: HTableDescriptor = new HTableDescriptor(TableName.valueOf(tablename))
      //tableDesc.addCoprocessor("org.apache.hadoop.hbase.coprocessor.AggregateImplementation")
    }else {
      val tableDesc: HTableDescriptor = new HTableDescriptor(myTableName)
      tableDesc.addCoprocessor("org.apache.hadoop.hbase.coprocessor.AggregateImplementation")
      for ( i <- 0 to (columnFamily.length - 1)){
          val columnDesc: HColumnDescriptor = new HColumnDescriptor(columnFamily(i));
          tableDesc.addFamily(columnDesc);
      }
      admin.createTable(tableDesc)
      LOG.info(tablename+" create table success!")
    }
    admin.close
  }

  @throws(classOf[IOException])
  def addRow(table: Table, rowKey: String, columnFamily: String, key: String, value: String) {
    val rowPut: Put = new Put(Bytes.toBytes(rowKey))
    if (value == null) {
      rowPut.addColumn(columnFamily.getBytes, key.getBytes, "".getBytes)
    } else {
      rowPut.addColumn(columnFamily.getBytes, key.getBytes, value.getBytes)
    }
    table.put(rowPut)
  }

  @throws(classOf[IOException])
  def getRow(table: Table, rowKey: String): Result = {
    val get: Get = new Get(Bytes.toBytes(rowKey))
    val result: Result = table.get(get)
    for (rowKv <- result.rawCells()) {
      println("Family:"+new String(rowKv.getFamilyArray,rowKv.getFamilyOffset,rowKv.getFamilyLength,"UTF-8"))
      println("Qualifier:"+new String(rowKv.getQualifierArray,rowKv.getQualifierOffset,rowKv.getQualifierLength,"UTF-8"))
      println("Timestamp:"+rowKv.getTimestamp)
      println("rowKey:"+new String(rowKv.getRowArray,rowKv.getRowOffset,rowKv.getRowLength,"UTF-8"))
      println("Value:"+new String(rowKv.getValueArray,rowKv.getValueOffset,rowKv.getValueLength,"UTF-8"))
    }
    return result
  }

  def addDataBatch(table: Table, list: java.util.List[Put]) {
    try {
      table.put(list)
    }catch {
      case e: RetriesExhaustedWithDetailsException => {
        LOG.error(e.getMessage)
      }
      case e: IOException => {
        LOG.error(e.getMessage)
      }
    }
  }

  def queryAll(table: Table):ResultScanner ={
    val scan: Scan = new Scan
    try {
      val s = new Scan()
      val results:ResultScanner = table.getScanner(s)
      return results
    }catch {
      case e: IOException => {
        LOG.error(e.toString)
      }
    }
    return null
  }

  def queryBySingleColumn(table: Table, queryColumn: String, value: String, columns: Array[String]): ResultScanner = {
    if (columns == null || queryColumn == null || value == null) {
      return null
    }
    try {
      val filter: SingleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes(queryColumn), Bytes.toBytes(queryColumn), CompareOp.EQUAL, new SubstringComparator(value))
      val scan: Scan = new Scan
      for (columnName <- columns) {
        scan.addColumn(Bytes.toBytes(columnName), Bytes.toBytes(columnName))
      }
      scan.setFilter(filter)
      return table.getScanner(scan)
    }catch {
      case e: Exception => {
        LOG.error(e.toString)
      }
    }
    return null
  }

  def dropTable(hbaseconn: Connection, tableName: String) {
    try {
      val admin: HBaseAdmin = hbaseconn.getAdmin.asInstanceOf[HBaseAdmin]
      admin.disableTable(TableName.valueOf(tableName))
      admin.deleteTable(TableName.valueOf(tableName))
    }catch {
      case e: MasterNotRunningException => {
        LOG.error(e.toString)
      }
      case e: ZooKeeperConnectionException => {
        LOG.error(e.toString)
      }
      case e: IOException => {
        LOG.error(e.toString)
      }
    }
  }

  @throws(classOf[Exception])
  def main(args: Array[String]) {
    val conf: Configuration = ScalaHbase.getHbaseConf
    val conn = ConnectionFactory.createConnection(conf)
    //表格名称test
    var table:Table = conn.getTable(TableName.valueOf("test"));
    try {
      //列族fam1、fam2
      val familyColumn: Array[String] = Array[String]("fam1", "fam2")
      ScalaHbase.createTable(conn,"test", familyColumn)

      val uuid: UUID = UUID.randomUUID
      val s_uuid: String = uuid.toString

      ScalaHbase.addRow(table, s_uuid, "fam1", "column1A", s_uuid+"_1A")
      ScalaHbase.addRow(table, s_uuid, "fam1", "column1B", s_uuid+"_1B")
      ScalaHbase.addRow(table, s_uuid, "fam2", "column2A", s_uuid+"_2A")
      ScalaHbase.addRow(table, s_uuid, "fam2", "column2B", s_uuid+"_2B")
      ScalaHbase.getRow(table, s_uuid)
      ScalaHbase.dropTable(conn,"test")
    }catch {
      case e: Exception => {
        if (e.getClass == classOf[MasterNotRunningException]) {
          System.out.println("MasterNotRunningException")
        }
        if (e.getClass == classOf[ZooKeeperConnectionException]) {
          System.out.println("ZooKeeperConnectionException")
        }
        if (e.getClass == classOf[IOException]) {
          System.out.println("IOException")
        }
        e.printStackTrace
      }
    } finally {
      if (null != table) {
        table.close
      }
    }
  }
}

 

展开阅读全文

没有更多推荐了,返回首页