HBase 2.1.0 新式API 基本功能写法

本文介绍HBase 2.x版本的API使用方法,包括表的创建、删除、数据的插入、查询及删除等核心操作。文章通过具体示例展示了如何使用新API进行HBase的管理,并对比了与旧版API的不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HBase2.X的部分API

HBase的1.x的API,将会在3.0版本移除很多的写法,因此,在这里对新式的api进行简单的写法记录.因为没有研究太深入,如果有写的不妥的地方,之后会加以改正.

  • 导入的包
	import java.util
	
	import org.apache.hadoop.conf.Configuration
	import org.apache.hadoop.hbase._
	import org.apache.hadoop.hbase.client._
	import org.apache.hadoop.hbase.util.Bytes
	def main(args: Array[String]): Unit = {
		//依旧是创建配置项,设置zookeeper的参数
	    val conf: Configuration = HBaseConfiguration.create()
	    conf.set("hbase.zookeeper.quorum", "hdp-nn,hdp-dn-01,hdp-dn-02,hdp-dn-03")
	    conf.set("hbase.zookeeper.property.clientPort", "2181")
	
	    //创建新表
	    CreateTableNew(conf, "Test2", "info", "grade")
	    //删除表
	    DelectTableNew(conf, "Test2")
	    //添加数据
	    InsertTable(conf, "Test1", "wwr", "grade", "age", "23")
	    InsertTable(conf, "Test1", "wwr", "grade", "name", "xujie")
	    InsertTable(conf, "Test1", "wwr", "grade", "class", "38")
	    //添加新的列簇
	    InsertColumnFaimly(conf, "Test1", "High_Speed")
	    //查询所有的表信息
	    ScanValue(conf, "Test1")
	    //查找指定row的信息
	    getRowKeyValue(conf, "Test1", "wwr")
	    //删除指定row的信息
	    deleteRows(conf, "Test1", "wwr")
	}
  • 添加表

在新API中,HTableDescriptor和HColumnDescriptor会逐渐被
TableDescriptorBuilder和ColumnFamilyDescriptorBuilder取代
##TableDescriptorBuilder 表描述生成器
##ColumnFamilyDescriptorBuilder 列簇描述生成器

  def CreateTableNew(conf: Configuration, tablename: String, columnFamily: String*) = {
  	 //创建连接
    val conn: Connection = ConnectionFactory.createConnection(conf)
    //创建'库'操作 admin对象
    val admin = conn.getAdmin.asInstanceOf[HBaseAdmin]
    //将字符串转化为表名
    val tableName = TableName.valueOf(tablename)
    //TableDescriptorBuilder 表描述生成器
    val table: TableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName)
    
    for (cf <- columnFamily) {
    	//ColumnFamilyDescriptorBuilder 列簇描述生成器
      val info = ColumnFamilyDescriptorBuilder.of(cf)
      //添加列簇
      table.setColumnFamily(info)
    }
    
	 //构建表描述
    val descriptor: TableDescriptor = table.build()
    //创建表
    admin.createTable(descriptor)
	//关闭接口
    admin.close()
    conn.close()
  }
  • 增加列簇
  def InsertColumnFaimly(conf: Configuration, tableName: String, ColumnFamily: String*): Unit = {
 	 //创建连接
    val conn = ConnectionFactory.createConnection(conf)
    
	//创建'库'操作 admin对象
    val admin: HBaseAdmin = conn.getAdmin.asInstanceOf[HBaseAdmin]
    
	//遍历插入要插入的列簇##(表)
    for (cf <- ColumnFamily) {
    
      //通过.of方法,转化列簇名
      val cff = ColumnFamilyDescriptorBuilder.of(cf)
      
      admin.addColumnFamily(TableName.valueOf(tableName), cff)
    }

    admin.close()
    conn.close()
  }
  • 删除表
  def DelectTableNew(conf: Configuration, tablename: String) = {
    //创建连接
    val conn: Connection = ConnectionFactory.createConnection(conf)
    //创建'库'操作 admin对象
    val admin = conn.getAdmin.asInstanceOf[HBaseAdmin]
    //将String类型字符串,修饰为表名
    val tableName = TableName.valueOf(tablename)
    //使表下架
    admin.disableTable(tableName)
    //删除表
    admin.deleteTable(tableName)
    //关闭接口
    admin.close()
    conn.close()
  }
  • 插入数据
  def InsertTable(conf: Configuration, tableName: String, rowkey: String, columnFamily: String, column: String, value: String) = {
    //创建连接
    val conn: Connection = ConnectionFactory.createConnection(conf)

    val name = TableName.valueOf(tableName)
    //创建表连接对象
    val table: Table = conn.getTable(name)
    //创建put对象,传递rowKey
    val put: Put = new Put(Bytes.toBytes(rowkey))
    //添加属性
    put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value))
    //添加数据到表
    table.put(put)
    table.close()
    conn.close()
  }
  • 查询表的所有信息
  def ScanValue(conf: Configuration, tableName: String) = {

    val conn = ConnectionFactory.createConnection(conf)

    val tablename = TableName.valueOf(tableName)
    //获取表
    val table = conn.getTable(tablename)
    //创建扫描 对象
    val scan = new Scan()
    //获取扫描结果
    val scanner: ResultScanner = table.getScanner(scan)

   //取该行的cell迭代器
   /* val scanner: CellScanner = result.cellScanner()
   // 迭代这一行的cell
	    while(scanner.advance()){
	      val cell = scanner.current()
	      println(s"RowKey:${Bytes.toString(CellUtil.cloneRow(cell))}")
	      println(s"ColumFamily:${Bytes.toString(CellUtil.cloneFamily(cell))}")
	      println(s"Qualifier:${Bytes.toString(CellUtil.cloneQualifier(cell))}")
	      println(s"Value:${Bytes.toString(CellUtil.cloneValue(cell))}")
	      println("==================================================================")
	    }
    */

    val cells = scanner.next().rawCells()
    for (cell <- cells) {
      println(s"RowKey:${Bytes.toString(CellUtil.cloneRow(cell))}")
      println(s"ColumFamily:${Bytes.toString(CellUtil.cloneFamily(cell))}")
      println(s"Qualifier:${Bytes.toString(CellUtil.cloneQualifier(cell))}")
      println(s"Value:${Bytes.toString(CellUtil.cloneValue(cell))}")
      println("==================================================================")
    }
   
    table.close()
    conn.close()
  }
  • 根据指定rowKey得到相应的
  def getRowKeyValue(conf: Configuration, tableName: String, RowKey: String) = {
  
    val conn = ConnectionFactory.createConnection(conf)
    
    val tablename: TableName = TableName.valueOf(tableName)
    
    val table: Table = conn.getTable(tablename)
    //通过get对象查询数据
    val get: Get = new Get(Bytes.toBytes(RowKey))
    
    val result: Result = table.get(get)
    
    //查找指定表,指定属性的值
    println(s"Value:${Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))}")
    
    val cells = result.rawCells()
    
    for (cell <- cells) {
      println(s"Row:${result.getRow}")
      println(s"Row:${Bytes.toString(CellUtil.cloneRow(cell))}")
      println(s"ColumFamily:${Bytes.toString(CellUtil.cloneFamily(cell))}")
      println(s"Qualifier:${Bytes.toString(CellUtil.cloneQualifier(cell))}")
      println(s"Value:${Bytes.toString(CellUtil.cloneValue(cell))}")
      println(s"TimeStamp:${cell.getTimestamp}")
      println("=======================================")
    }
    
    table.close()
    conn.close()
  }
  • 删除指定row数据
  def deleteRows(conf: Configuration, tableName: String, RowKey: String*) = {
    val conn: Connection = ConnectionFactory.createConnection(conf)
    val table: Table = conn.getTable(TableName.valueOf(tableName))

    val list: util.ArrayList[Delete] = new util.ArrayList[Delete]()
    for (row <- RowKey) {
      val delete: Delete = new Delete(Bytes.toBytes(row))
      list.add(delete)
    }
    table.delete(list)
    table.close()
    conn.close()
  }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值