使用scala调用Hbase API进行操作

使用scala调用Hbase API
1:查看表是否存在

def isTableExist(table: String): Boolean = {
    val conf: Configuration = HBaseConfiguration.create
    conf.set("hbase.zookeeper.quorum", "192.168.56.101,192.168.56.102,192.168.56.103")
    conf.set("hbase.zookeeper.property.clientPort", "2181")
    val admin: HBaseAdmin = new HBaseAdmin(conf)
    val bool = admin.tableExists(table)
    admin.close()
    bool
  }
    

2:根据表名,获取所有数据

val conf: Configuration = HBaseConfiguration.create
    conf.set("hbase.zookeeper.quorum", "192.168.56.101,192.168.56.102,192.168.56.103")
    conf.set("hbase.zookeeper.property.clientPort", "2181")
    val table: HTable = new HTable(conf, tableName)
    val scan: Scan = new Scan()
    val scanner = table.getScanner(scan)
    var flag = true
    while (flag) {
      val result = scanner.next()
      try {
        val cells: Array[Cell] = result.rawCells()
        cells.foreach(cell => {
          println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)))
          println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)))
          println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)))
          println("值:" + Bytes.toString(CellUtil.cloneValue(cell)))
        })
      } catch {
        case e: NullPointerException => flag = false
      }
    }
  }

3:根据表名,rowkey获取 1:此rowkey下的所有数据 2:获取指定列族下的所有数据 3:某一指定列族下的某一个列的数据

  def getRowQualifier(tableName: String, rowKey: String, family: String, qualifier1: String): Unit = {
    val conf: Configuration = HBaseConfiguration.create
    conf.set("hbase.zookeeper.quorum", "192.168.56.101,192.168.56.102,192.168.56.103")
    conf.set("hbase.zookeeper.property.clientPort", "2181")
    val table: HTable = new HTable(conf, tableName)
    // 如果不使用addColumn或者addFamily,那就是获取此rowkey下的所有列的数据
    val get = new Get(Bytes.toBytes(rowKey))
    
    // 如果使用addColumn,添加列族和列,则此列的数据
    //get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier1))
    // 如果使用addFamily,添加列族,则此列族下的所有数据
    //get.addFamily(Bytes.toBytes(family))
    
    val result = table.get(get)
    result.rawCells().foreach(cell=>{
      println("行键:" + Bytes.toString(result.getRow()))
      println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)))
      println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)))
      println("值:" + Bytes.toString(CellUtil.cloneValue(cell)))
    })
    table.close()
  }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值