Hbase API 操作开发需要连接Zookeeper进行节点的管理控制

1.配置 HBaseConfiguration

  包:org.apache.hadoop.hbase.HBaseConfiguration下的HBaseConfiguration

  作用:通过此类可以对HBase进行配置

  static Configuration config = null;private Connection connection = null;private Table table = null;@Beforepublic void init() throws Exception {config = HBaseConfiguration.create();//HBaseConfiguration.create()默认会从classpath中查找hbase-site.xml中的配置信息,初始化Configuration config.set("hbase.zookeeper.quorum", "shizhan3,shizhan5,shizhan6");// zookeeper地址config.set("hbase.zookeeper.property.clientPort", "2183");// zookeeper端口(默认2181)connection = ConnectionFactory.createConnection(config);table = connection.getTable(TableName.valueOf("user"));//通过连接池获取表}

2.表管理类 HBaseAdmin:

  包:org.apache.hadoop.hbase.client.HBaseAdmin

  作用:提供接口关系HBase 数据库中的表信息   用法:HBaseAdmin admin = new HBaseAdmin(config);

3.表描述类 HTableDescriptor:

  包:org.apache.hadoop.hbase.HTableDescriptor

  作用:HTableDescriptor 类包含了表的名字以及表的列族信息 

  用法:HTableDescriptor htd =new HTableDescriptor(tablename);  htd.addFamily(new HColumnDescriptor(“myFamily”));

4.列族描述类 HColumnDescriptor:包:org.apache.hadoop.hbase.HColumnDescriptor,维护列族的信息

5.创建表的操作:一般我们用Shell创建表

public void createTable() throws Exception {// 创建表管理类HBaseAdmin admin = new HBaseAdmin(config); // hbase表管理// 创建表描述类TableName tableName = TableName.valueOf("test3"); // 表名称HTableDescriptor desc = new HTableDescriptor(tableName);// 创建列族的描述类HColumnDescriptor family = new HColumnDescriptor("info"); // 列族// 将列族添加到表中
        desc.addFamily(family);HColumnDescriptor family2 = new HColumnDescriptor("info2"); // 列族// 将列族添加到表中
        desc.addFamily(family2);// 创建表admin.createTable(desc); // 创建表}

6.批量插入数据:

/*** 向hbase中增加数据*/@SuppressWarnings({ "deprecation", "resource" })@Testpublic void insertData() throws Exception {table.setAutoFlushTo(false);table.setWriteBufferSize(534534534);ArrayList<Put> arrayList = new ArrayList<Put>();for (int i = 21; i < 50; i++) {Put put = new Put(Bytes.toBytes("1234"+i));put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"+i));put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234+i));arrayList.add(put);} //插入数据
        table.put(arrayList);//提交
        table.flushCommits();}

7.修改数据:

    /*** 修改数据*/@Testpublic void uodateData() throws Exception {Put put = new Put(Bytes.toBytes("1234"));put.add(Bytes.toBytes("info"), Bytes.toBytes("namessss"), Bytes.toBytes("lisi1234"));put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234));//插入数据
        table.put(put);//提交
        table.flushCommits();}

8.删除数据:

     /*** 删除数据*/@Testpublic void deleteDate() throws Exception {Delete delete = new Delete(Bytes.toBytes("1234"));delete.addFamily(Bytes.toBytes("info1"));//删除列族delete.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"));//删除列族下的name
        table.delete(delete);table.flushCommits();}

9.查询数据:(单挑查询、批量查询、过滤器查询)

单条查询:

    /*** 单条查询*/@Testpublic void queryData() throws Exception {Get get = new Get(Bytes.toBytes("1234"));Result result = table.get(get);System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("namessss"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex"))));}

批量查询(全表扫描):ResultScanner

  包:org.apache.hadoop.hbase.client.ResultScanner(获取值的接口)

/*** 全表扫描*/@Testpublic void scanData() throws Exception {//设置全表扫描封装类Scan scan = new Scan();scan.setStartRow(Bytes.toBytes("wangsf_0"));//区间scan.setStopRow(Bytes.toBytes("wangwu"));//扫描ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println(Bytes.toString(result.getRow())); //携带的rowkeySystem.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));}}

过滤器查询

1.hbase过滤器:  

  FilterList 代表一个过滤器列表,可以添加多个过滤器进行查询,多个过滤器之间的关系有:

  与关系(符合所有):FilterList.Operator.MUST_PASS_ALL  

  或关系(符合任一):FilterList.Operator.MUST_PASS_ONE

2.过滤器的种类: 

  列值过滤器—SingleColumnValueFilter: 过滤列值的相等、不等、范围等

  列名前缀过滤器—ColumnPrefixFilter:过滤指定前缀的列名

  多个列名前缀过滤器—MultipleColumnPrefixFilter:过滤多个指定前缀的列名

  rowKey过滤器—RowFilter:通过正则,过滤rowKey值。

2.1.列值过滤器:

/*** 全表扫描的过滤器* 列值过滤器*/@Testpublic void scanDataByFilter1() throws Exception {// 创建全表扫描的scanScan scan = new Scan();//过滤器:列值过滤器SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL,Bytes.toBytes("zhangsan2"));//下面示例检查列值和字符串'zhangsan2'相等//设置过滤器
        scan.setFilter(filter);// 打印结果集ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));}}

2.2.列名前缀过滤器—ColumnPrefixFilter:用于指定列名前缀值相等

/*** 匹配列名前缀过滤器*/@Testpublic void scanDataByFilter3() throws Exception {// 创建全表扫描的scanScan scan = new Scan();//匹配rowkey以wangsenfeng开头的
        ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("na"));// 设置过滤器
        scan.setFilter(filter);// 打印结果集ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println("rowkey:" + Bytes.toString(result.getRow()));System.out.println("info:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("name"))));// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) {System.out.println("info:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) {System.out.println("infi:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("sex"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) {System.out.println("info2:name:"+ Bytes.toString(result.getValue( Bytes.toBytes("info2"),Bytes.toBytes("name"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) {System.out.println("info2:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("age"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) {System.out.println("info2:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("sex"))));}}}

2.3.多个列值前缀过滤器—MultipleColumnPrefixFilter:用于指定多个前缀

  byte[][] prefixes = new byte[][] {Bytes.toBytes("value1"),Bytes.toBytes("value2")}; 

  Filter f = new MultipleColumnPrefixFilter(prefixes);

  scan.setFilter(f);

2.4.rowkey过滤器:通常根据rowkey来指定范围时,使用scan扫描器的StartRow和StopRow方法比较好

     /*** rowkey过滤器*/@Testpublic void scanDataByFilter2() throws Exception {// 创建全表扫描的scanScan scan = new Scan();//匹配rowkey以12341开头的RowFilter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^12341"));// 设置过滤器
        scan.setFilter(filter);// 打印结果集ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));}}

2.5.过滤器集合FilterList: 

     /*** 过滤器集合*/@Testpublic void scanDataByFilter4() throws Exception {// 创建全表扫描的scanScan scan = new Scan();//过滤器集合:MUST_PASS_ALL(and),MUST_PASS_ONE(or)FilterList filterList = new FilterList(Operator.MUST_PASS_ONE);//匹配rowkey以wangsenfeng开头的RowFilter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^wangsenfeng"));//匹配name的值等于wangsenfengSingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL,Bytes.toBytes("zhangsan"));filterList.addFilter(filter);filterList.addFilter(filter2);// 设置过滤器
        scan.setFilter(filterList);// 打印结果集ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println("rowkey:" + Bytes.toString(result.getRow()));System.out.println("info:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("name"))));// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) {System.out.println("info:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) {System.out.println("infi:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("sex"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) {System.out.println("info2:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("name"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) {System.out.println("info2:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age"))));}// 判断取出来的值是否为空if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) {System.out.println("info2:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("sex"))));}}}

操作类文件:https://pan.baidu.com/s/1vDc-J-uleBPTB0_IMhAEeA

  

转载于:https://www.cnblogs.com/yaboya/p/9349383.html

5.Hbase API 操作开发相关推荐

  1. HBase学习(四) HBase API操作

    目录 准备工作 创建maven项目 添加依赖 API操作 创建HBase连接 创建HBase表 删除表 向表中插入数据 查看数据 过滤器操作 全部代码 注意事项 准备工作 创建maven项目 添加依赖 ...

  2. Kerberos Hbase Api 操作

    初始化连接加载配置参数,这里只创建连接,具体操作hbase的api没有变化 static { try{ Configuration conf = HBaseConfiguration.create() ...

  3. Hbase 设计与开发实战

    Hbase 概述 大数据及 NoSQL 的前世今生 传统的关系型数据库处理方式是基于全面的 ACID 保证,遵循 SQL92 的标准表设计模式(范式)和数据类型,基于 SQL 语言的 DML 数据交互 ...

  4. HBase 6、用Phoenix Java api操作HBase

    开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...

  5. HBase Java API 代码开发

    1. API 介绍 几个主要 HBase API 类和数据模型之间的对应关系: Java 类 HBase 数据模型 Admin 数据库(Database) HBaseConfiguration Tab ...

  6. 使用 Java API 操作 HBase

    使用 Java API 操作 HBase 数据库,就类似HBase Shell,本质上一个是Java 代码,一个是Shell 命令.(hadoop 的文件系统莫不如此,可用Java API 的方式操作 ...

  7. Hbase java API操作(模板代码)

    Hbase java API操作 1 创建maven工程 导入jar包 <repositories><repository><id>cloudera</id& ...

  8. Hbase 完全分布式模式的搭建、命令行操作、Java API操作

    追风赶月莫停留,平芜尽处是春山. 文章目录 追风赶月莫停留,平芜尽处是春山. 环境 Hbase 完全分布式模式的搭建 一.下载安装包,解压到合适位置: 二.配置相关的文件: 三.将Hbase复制到其他 ...

  9. Windows下配置Hadoop的Java开发环境以及用Java API操作HDFS

    场景 HDFS的访问方式之HDFS shell的常用命令: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119351218 在上 ...

  10. java api操作hbase_通过JavaAPI使用HBase

    1.准备工作 (1) 启动zookeeper服务,我的是在本地启动zookeeper /usr/local/zookeeper/bin$ sudo zkServer.sh start (2) 启动HB ...

最新文章

  1. java 如何实现导出文件
  2. 坐拥双妹、佰草集,上海家化是怎样将一只脚留在高端护肤品门外的?
  3. 剑指offer面试题21. 调整数组顺序使奇数位于偶数前面(双指针)
  4. 日周月筛选器_Excel数据筛选与高级筛选,你会用吗
  5. 微型计算机ROW,微型计算机原理与汇编语言程序设计 第3章 80x86微处理器及其体系结构zrow0c_d.ppt...
  6. 2021上半年软考中级软件设计师考试心得(10天时间你可以拿捏的)
  7. 软件工程 - 个人博客系统 - 概要设计与详细设计文档
  8. 苹果发布的Mac Pro就是“渣渣”?网友疯狂吐槽
  9. 关系模式无损分解的测试方法
  10. 花生壳内网穿透操作文档
  11. Snapchat面试题:移除K位
  12. 2020年8月中国编程语言排行榜
  13. 安卓 Day 23 :利用视图翻页器实现引导项
  14. php 会员 开源,会员组_POSCMS_PHP开源_迅睿CMS系统
  15. 04_frp内网穿透实例
  16. 牛客练习赛37 C 筱玛的迷阵探险(Trie+折半)
  17. 使用PE启动U盘安装pfSense
  18. GAN用于(无缺陷样本)产品表面缺陷检测
  19. html5将数组转换为字符串,js如何将数组元素转换为字符串
  20. 赶紧换掉windows系统自带记事本

热门文章

  1. 【pymongodb】去除重复记录
  2. 程序员必备算法——算法相关链接总结
  3. 网易编程题——牛牛的闹钟
  4. 一种基于DCNN模型的云检测方法介绍
  5. ArcGIS操作小技巧(三)之License service不能启动的解决方法
  6. Java通过微信公众号获取地理位置信息
  7. python 串口write 返回值_MicroPython 玩转硬件系列4:串口小实验
  8. 实习成长之路:DelayQueue多线程下的延迟队列的使用
  9. plugin工程及与flutter工程通信原理
  10. 面试官: MySQL 数据库的优化,你知道有哪些?