设为首页 加入收藏

TOP

HBase 2.1.0 新式API 基本功能写法
2019-05-12 13:52:03 】 浏览:188
Tags:HBase 2.1.0 新式 API 基本 功能 写法
版权声明:转载请标明出处,本来就不牛逼,让我小小的骄傲一下.谢谢!!! https://blog.csdn.net/qq_43020538/article/details/86601321

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")
	}
  • HBase各属性个人理解:

tableName: 表名:相当于Mysql的数据库
rowKey: 相当于Mysql行数
columnFamily 列簇:相当于Mysql的表
column/Qualifier 属性/限定词:mysql的字段名
value 值:Mysql的具体数值

  • 添加表

在新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.cloneva lue(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.cloneva lue(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.cloneva lue(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()
  }
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇laravel面包屑导航(2)模块化开发 下一篇Spring Boot集成HBase

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目