Hbase增删改查Scala代码实现

点击跳转:Hbase增删改查Java代码实现

开门见山,如下:

我们依然需要导入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>

import java.text.SimpleDateFormat
import java.util
import java.util.Date

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
import scala.collection.mutable.{ArrayBuffer, ListBuffer}

object HbaseUtils {
  /**
    * 获取管理员对象
    *
    * @param conf 对hbase client配置一些参数
    * @return 返回hbase的HBaseAdmin管理员对象
    */
  def getAdmin(conf: Configuration): HBaseAdmin = {
    val conn = ConnectionFactory.createConnection(conf)
    conn.getAdmin().asInstanceOf[HBaseAdmin]
  }

  /**
    * 根据指定的管理员,表名,列族名称创建表
    *
    * @param admin         管理员
    * @param tName         表名称
    * @param columnFamilys 列粗名称
    */
  def createTable(admin: HBaseAdmin, tName: String, columnFamilys: List[String]): Unit = {
    /*if (admin.tableExists(TableName.valueOf(tName))) {
      println("table already exists!")
      admin.disableTable(tName)
      admin.deleteTable(tName)
    }*/

    val tableDesc = new HTableDescriptor(TableName.valueOf(tName))
    columnFamilys.foreach(columnFamilysName => tableDesc.addFamily(new HColumnDescriptor(columnFamilysName)))
    admin.createTable(tableDesc)
    println("create table success!")

    /*catch {
      case e: Exception => e.printStackTrace()
    }*/
  }

  def deletetable(admin: HBaseAdmin, tName: String): Unit = {
    admin.disableTable(tName)
    admin.deleteTable(tName)
    println("delete table success!")
  }

  def insertData(conf: Configuration, tableName: String, rowKey: String, columnFamily: String, column: String, value: String): Unit = {
    //创建连接
    val con = ConnectionFactory.createConnection(conf)
    //
    val table = con.getTable(TableName.valueOf(tableName))
    val put = new Put(Bytes.toBytes(rowKey))
    put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value))
    table.put(put)
    close(table, con)
    print("数据插入成功")
  }

  def close(table: Table, con: Connection): Unit = {
    if (table != null)
      table.close()
    if (con != null)
      con.close()
  }

  def selectDate(conf: Configuration, tName: String): ArrayBuffer[Array[Cell]] = {
    var arrayBuffer = ArrayBuffer[Array[Cell]]()
    val scanner = new Scan()
    val conn = ConnectionFactory.createConnection(conf)
    val table = conn.getTable(TableName.valueOf(tName))
    val results = table.getScanner(scanner)
    var res: Result = results.next()
    while (res != null) {
      arrayBuffer += res.rawCells()
      res = results.next()
    }
    arrayBuffer
  }

  def getRow(conf: Configuration, tName:String ,row: String) : Array[Cell] = {
    val con: Connection = ConnectionFactory.createConnection(conf)
    val table: Table = con.getTable(TableName.valueOf(tName))
    val get: Get = new Get(Bytes.toBytes(row))
    val result: Result = table.get(get)
    result.rawCells()
  }

  def deleteRow(conf:Configuration, tName:String, row:String) : Unit = {
    val connection: Connection = ConnectionFactory.createConnection(conf)
    val table: Table = connection.getTable(TableName.valueOf(tName))
    table.delete(new Delete(Bytes.toBytes(row)))
    println("删除成功")
  }

  def inserts(conf:Configuration, tName:String, rowKet:String, columnFamily:String, column:String, value:String) : Unit = {
    val con: Connection = ConnectionFactory.createConnection(conf)
    val table: BufferedMutator = con.getBufferedMutator(TableName.valueOf(tName))
    val put: Put = new Put(Bytes.toBytes(rowKet))
    val puts: Put = put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value))
    val putss: Put = put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value))
    val putsss: Put = put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value))
    //这个Array是JDK里面
    val mutations: util.ArrayList[Mutation] = new util.ArrayList[Mutation]()
    mutations.add(put)
    mutations.add(puts)
    mutations.add(putss)
    mutations.add(putsss)
    table.mutate(mutations)
    table.flush()
    if(table!=null)
      table.close()
    if(con!=null)
      con.close()
    println("数据插入成功")
  }

  def getKeys() : mutable.ListBuffer[String] = {
    val format: SimpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss")
    var num: Long = format.format(new Date()).toLong
      val array: Array[Char] = new Array[Char](7)
      val list: ListBuffer[String] = new mutable.ListBuffer[String]()
      val builder: StringBuilder = new mutable.StringBuilder()
      for (i <- 0 until 7){
        array(6 -i) = (num % 42 + 48).toChar
      num = num / 42
    }
    for (it <- array) {
      builder.append(it.toString)
    }
    list.append(builder.toString())
    list
  }


  def main(args: Array[String]): Unit = {
    //創建配置對象
    val conf: Configuration = HBaseConfiguration.create()
    //設置zookeeper地址
    conf.set("hbase.zookeeper.quorum", "node03:2181,node04:2181,node05:2181")
    //創建admin管理對象
    val admin: HBaseAdmin = getAdmin(conf)
    //創建列族集合
    /*val columnFamilys: List[String] = List[String]("stu_infos","stu_addr")*/
    //創建表
    /*createTable(admin,"t_students",columnFamilys)*/
    //删除表
    /*deletetable(admin,"t_students")*/
    //插入数据
    /*insertData(conf,"t_student","st001","stu_addr","name","wangchongchong")*/
    //查看表中所有信息
    /*val buffer: ArrayBuffer[Array[Cell]] = selectDate(conf, "t_student")
      buffer.foreach(a=>{
        val b=a.map(x=>
            new String(x.getRowArray,x.getRowOffset,x.getRowLength)+" "
          +new String(x.getFamilyArray,x.getFamilyOffset,x.getFamilyLength)+" "
          +new String(x.getQualifierArray,x.getQualifierOffset,x.getQualifierLength)+" "
          +new String(x.getValueArray,x.getValueOffset,x.getValueLength)
        )
        b.foreach(println(_))
      })*/
    //根据行间获取具体的某一个行
    /*val cells: Array[Cell] = getRow(conf,"t_student","st001")
    cells.foreach(i => println(i))*/
    //删除指定的Row
    /*deleteRow(conf,"t_student","st001")*/
    //批量添加
//    inserts(conf,"t_student","st01","stu_addr","age","17")
    val strings: ListBuffer[String] = getKeys()
    strings.foreach(i => println(i))
  }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值