Hase Java API 和 Hbase Scala API

Table of Contents

Java 版的

Hbase 工具类,配置 zookeeper 的地址

建表操作

修改表结构

列出所有表

删表

写表

根据 rowKey 删数据

查表,所有数据

根据 rowKey 查数据

Scala 版的

Hbase 工具类

列出所有表

建表

查看表结构

删表

写数据

扫描整个表

根据 rowKey 查数据

根据 rowKey 删数据

查询所有记录数 count*

完整代码


Java 版的

Hbase 工具类,配置 zookeeper 的地址

public class HbaseUtil {

    public  static Configuration getHbaseConfig() {
        Configuration config = HBaseConfiguration.create();
        // 显式配置 hbase zookeeper 地址
        config.set("hbase.zookeeper.quorum", "192.168.78.135");
//        config.set("hbase.table.sanity.checks", "false");
        // hdfs 配置和 hbase 配置 (hbase-site.xml, core-site.xml)
//        config.addResource(new Path(CommUtils.getBasicPath() + "/config/hbase-site.xml"));
//        config.addResource(new Path(CommUtils.getBasicPath() + "/config/core-site.xml"));
        return config;
    }
}

建表操作

private static void createSchemaTables(Admin admin) throws IOException {
        // 设置 table name
        HTableDescriptor table = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
        // 设置 列族,压缩
        table.addFamily(new HColumnDescriptor(CF_DEFAULT)
                .setCompressionType(Compression.Algorithm.NONE));
        // 预设 region 数量 和 region 算法
        table.setRegionReplication(2);
        table.setRegionSplitPolicyClassName(RegionSplitter.UniformSplit.class.getName());

        System.out.print("Creating table Start");
        // 如果表存在,先 disable 表,在 delete 表
        if (admin.tableExists(table.getTableName())) {
            admin.disableTable(table.getTableName());
            admin.deleteTable(table.getTableName());
        }
        // 实际的创建表函数
        admin.createTable(table);
        System.out.println("Create table Done");
    }

修改表结构

private static void modifySchema(Admin admin) throws IOException {

        TableName tableName = TableName.valueOf(TABLE_NAME);
        // 修改表之前,判断表是否存在,防止报异常
        if (!admin.tableExists(tableName)) {
            System.out.println("Table does not exist.");
            System.exit(-1);
        }
        // 创建表对象
        HTableDescriptor table = admin.getTableDescriptor(tableName);
        // 创建列族对象
        HColumnDescriptor newColumn = new HColumnDescriptor("NEWCF");
        // 设置压缩格式
        newColumn.setCompactionCompressionType(Compression.Algorithm.GZ);
        // 执行最大版本号
        newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
        // 新增表列族
        admin.addColumn(tableName, newColumn);
        // 修改存在 column family 的压缩格式和版本
        HColumnDescriptor existingColumn = new HColumnDescriptor(CF_DEFAULT);
        existingColumn.setCompactionCompressionType(Compression.Algorithm.GZ);
        existingColumn.setMaxVersions(HConstants.ALL_VERSIONS);
        table.modifyFamily(existingColumn);
        admin.modifyTable(tableName, table);
        // Disable an existing table
        admin.disableTable(tableName);
        // Delete an existing column family
        admin.deleteColumn(tableName, CF_DEFAULT.getBytes("UTF-8"));
        // Delete a table (Need to be disabled first)
        admin.deleteTable(tableName);
    }

列出所有表

private static void listTable(Admin admin) throws IOException {
        TableName[] tableNames = admin.listTableNames();
        for (TableName tableName : tableNames) {
            System.err.println("tableName is : " + tableName);
        }
    }

删表

 private static void dropTable(Admin admin, String tableName) throws IOException {
        TableName table = TableName.valueOf(tableName);
        admin.disableTable(table);
        System.err.println("disable table is success");
        admin.deleteTable(table);
        System.err.println("drop table is success");
    }

写表

private static void insertData(HTable table) throws IOException {
        // rowKey 前缀
        String rowKey = "rowKey";
        // 列族名称
        String family = "f1";
        // 造一批假数据
        for (int i = 0; i < 50; i++) {
            Put put = new Put(Bytes.toBytes(rowKey + i));
            for (int j = 0; j < 10; j++) {
                put.add(family.getBytes(), Bytes.toBytes(rowKey + i + "-key" + j), Bytes.toBytes(rowKey + i + "-value" + j));
            }
            table.put(put);
        }
    }

根据 rowKey 删数据

private static void deleteData(HTable table) throws IOException {
        Delete delete = new Delete("rowKey1".getBytes());
        // 指定 列族 名称,删除整个列族
        delete.addFamily("f1".getBytes());
        // 指定列族下的一个列,删除列
        delete.addColumn("f1".getBytes(), "key1".getBytes());
        // 什么也不指定,则删除整个 rowKey 下的数
        table.delete(delete);
    }

查表,所有数据

private static void scanData(HTable table) throws IOException {
        // 初始化 startRow 和 endRow
        Scan scan = new Scan("123".getBytes(), "rowKey11".getBytes());

        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            // 获取 rowKey
            String rowKey = new String(result.getRow());
            // 获取 key
            NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap("f1".getBytes());
            for (Map.Entry<byte[], byte[]> entry : familyMap.entrySet()) {
                String key = new String(entry.getKey());
                String value = new String(entry.getValue());
                // 打印结果
                System.err.println("rowKey is : " + rowKey + ", key is : " + key + ", value is : " + value);
            }
        }
    }

根据 rowKey 查数据

 private static void selectDataByRowKey(HTable table) throws IOException {
        Get get = new Get("rowKey1".getBytes());
        // 获取单个 rowKey 下整个结果
        Result result = table.get(get);
        System.err.println("result is : " + result.toString());
        // 根据列族,获取 key
        NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap("f1".getBytes());
        for (Map.Entry<byte[], byte[]> entry : familyMap.entrySet()) {
            System.err.println("key is : " + new String(entry.getKey()) + ", value is : " + new String(entry.getValue()));
        }
        // 获取 value
        byte[] value = result.getValue("f1".getBytes(), "key1".getBytes());
        System.err.println("value is : " + new String(value));
    }

Scala 版的

Hbase 工具类

object HbaseUtil {
//  已过时
//  val conf = new HBaseConfiguration()
  val conf = HBaseConfiguration.create()
  conf.set("hbase.zookeeper.quorum", "192.168.78.135")
  val connection = ConnectionFactory.createConnection(conf)
}

列出所有表

def listTables(admin: Admin) = {
    val names = admin.listTableNames()
    names.foreach(println(_))
  }

建表

def createTable(admin: Admin) = {

    if (admin.tableExists(this.tb)) {
      println("表已存在,请换个表名重新建表")
    } else {
      val descriptor = new HTableDescriptor
      descriptor.setName(TableName.valueOf(tableName))
      descriptor.addFamily(new HColumnDescriptor("f1"))
      admin.createTable(descriptor)
      println("create table done")
    }
  }

查看表结构

def describeTable(admin: Admin) = {

    val descriptor = admin.getTableDescriptor(TableName.valueOf(tableName))
    val families = descriptor.getFamilies
    println(families)
  }

删表

def dropTable(admin: Admin) = {

    if (admin.tableExists(this.tb)) {
      admin.disableTable(this.tb)
      admin.deleteTable(this.tb)
    } else
      println("表不存在,请确认表名")
  }

写数据

def insertData(tableName: String): Unit = {
    // 新建表对象
    val table = new HTable(conf, tableName)
    // 造假数据开始
    for (a <- 51 to 150) {
      // 创建 put 对象,用来存放数据,构造方法里放 rowKey 的值
      val put = new Put(("rowKey" + a).getBytes())
      for (b <- 1 to 10) {
        val key = ("rowKey" + a + "-key" + b).getBytes()
        val value = ("rowKey" + a + "-value" + b).getBytes()
        // addColumn 方法参数:列族,key,value
        put.addColumn("f1".getBytes(), key, value)
      }
      // put 数据
      table.put(put)
    }
  }

扫描整个表

def scanData(tableName: String): Unit = {
    // 创建 scan 对象
    val scan = new Scan
    // 创建表对象,构造方法:配置对象,表名
    val table = new HTable(conf, tableName)
    // 获取 scanner 对象
    val scanner: ResultScanner = table.getScanner(scan)
    // 从 scanner 对象中取所有结果
    val result = scanner.next()
    // 遍历全部结果
    while (result != null) {
      // 获取 rowKey
      val row = new String(result.getRow)
      // 获取 rowKey 下所有数据,返回的结构是 NavigableMap<byte[], byte[]>
      val map = result.getFamilyMap("f1".getBytes)
      val iter = map.entrySet().iterator()
      // 开始遍历 map 结果数据
      while (iter.hasNext) {
        val entry = iter.next()
        val key = new String(entry.getKey)
        val value = new String(entry.getValue)
        println("rowKey is : " + row + ", key is : " + key + ", value is : " + value)
      }
    }
  }

根据 rowKey 查数据

def scanData(tableName: String): Unit = {
    // 创建 scan 对象
    val scan = new Scan
    // 创建表对象,构造方法:配置对象,表名
    val table = new HTable(conf, tableName)
    // 获取 scanner 对象
    val scanner: ResultScanner = table.getScanner(scan)
    // 从 scanner 对象中取所有结果
    val result = scanner.next()
    // 遍历全部结果
    while (result != null) {
      // 获取 rowKey
      val row = new String(result.getRow)
      // 获取 rowKey 下所有数据,返回的结构是 NavigableMap<byte[], byte[]>
      val map = result.getFamilyMap("f1".getBytes)
      val iter = map.entrySet().iterator()
      // 开始遍历 map 结果数据
      while (iter.hasNext) {
        val entry = iter.next()
        val key = new String(entry.getKey)
        val value = new String(entry.getValue)
        println("rowKey is : " + row + ", key is : " + key + ", value is : " + value)
      }
    }
  }

根据 rowKey 删数据

def deleteDataByRowKey(tableName: String, row: String): Unit = {
    val table = new HTable(conf, tableName)

    val delete = new Delete(row.getBytes())

    table.delete(delete)
  }

查询所有记录数 count*

def countData(tableName: String): Unit = {
    val table = new HTable(conf, tableName)
    val descriptor = table.getTableDescriptor
    // 定义协调处理器名称
    val aggre = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation"
    // 如果表没有该协同处理器,先关表,addCoprocessor,开启表
    if (!descriptor.hasCoprocessor(aggre)) {
      val tbn = TableName.valueOf(tableName)
      admin.disableTable(tbn)
      descriptor.addCoprocessor(aggre)
      admin.modifyTable(TableName.valueOf(tableName), descriptor)
      admin.enableTable(tbn)
    }
    // 创建处理器客户端
    val client = new AggregationClient(conf)
    // 计算 count 时间
    val watch: StopWatch = new StopWatch()
    watch.start()
    // 开始 rowCount 操作
    val count = client.rowCount(table, new LongColumnInterpreter, new Scan())
    watch.stop()
    // 打印
    println(watch.getTime)
    println(tableName + " count si : " + count)
  }

 Scala 操作表代码

class TableSchemaDemo1(val tableName: String) {
  val tb = TableName.valueOf(tableName)


  def dropTable(admin: Admin) = {

    if (admin.tableExists(this.tb)) {
      admin.disableTable(this.tb)
      admin.deleteTable(this.tb)
    } else
      println("表不存在,请确认表名")
  }

  def describeTable(admin: Admin) = {

    val descriptor = admin.getTableDescriptor(TableName.valueOf(tableName))
    val families = descriptor.getFamilies
    println(families)
  }

  def createTable(admin: Admin) = {

    if (admin.tableExists(this.tb)) {
      println("表已存在,请换个表名重新建表")
    } else {
      val descriptor = new HTableDescriptor
      descriptor.setName(TableName.valueOf(tableName))
      descriptor.addFamily(new HColumnDescriptor("f1"))
      admin.createTable(descriptor)
      println("create table done")
    }
  }

  def listTables(admin: Admin) = {
    val names = admin.listTableNames()
    names.foreach(println(_))
  }


  def listTables(): Unit = {

  }


  override def toString = s"TableSchemaDemo1($tableName)"
}

object TableSchemaDemo1 {
  def main(args: Array[String]): Unit = {

    // 获取 hbase 配置
    val conf = HbaseUtil.conf
    // 获取连接
    val connection = HbaseUtil.connection
    // 获取管理员
    val admin = connection.getAdmin()

    val demo = new TableSchemaDemo1("tbs")
    demo.listTables(admin)
    demo.createTable(admin)
    demo.describeTable(admin)
//    demo.dropTable(admin)

    println(demo.toString)
  }
}

完整代码

Java:https://github.com/zhang-peng-fei/java_bigdata

Scala:https://github.com/zhang-peng-fei/scala_bigdata

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值