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

1.配置 HBaseConfiguration

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

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

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

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创建表

  1. public void createTable() throws Exception {
  2. // 创建表管理类
  3. HBaseAdmin admin = new HBaseAdmin(config); // hbase表管理
  4. // 创建表描述类
  5. TableName tableName = TableName.valueOf("test3"); // 表名称
  6. HTableDescriptor desc = new HTableDescriptor(tableName);
  7. // 创建列族的描述类
  8. HColumnDescriptor family = new HColumnDescriptor("info"); // 列族
  9. // 将列族添加到表中
  10. desc.addFamily(family);
  11. HColumnDescriptor family2 = new HColumnDescriptor("info2"); // 列族
  12. // 将列族添加到表中
  13. desc.addFamily(family2);
  14. // 创建表
  15. admin.createTable(desc); // 创建表
  16. }

6.批量插入数据:

  1. /**
  2. * 向hbase中增加数据
  3. */
  4. @SuppressWarnings({ "deprecation", "resource" })
  5. @Test
  6. public void insertData() throws Exception {
  7. table.setAutoFlushTo(false);
  8. table.setWriteBufferSize(534534534);
  9. ArrayList<Put> arrayList = new ArrayList<Put>();
  10. for (int i = 21; i < 50; i++) {
  11. Put put = new Put(Bytes.toBytes("1234"+i));
  12. put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"+i));
  13. put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234+i));
  14. arrayList.add(put);
  15. }
  16. //插入数据
  17. table.put(arrayList);
  18. //提交
  19. table.flushCommits();
  20. }

7.修改数据:

  1.    /**
  2. * 修改数据
  3. */
  4. @Test
  5. public void uodateData() throws Exception {
  6. Put put = new Put(Bytes.toBytes("1234"));
  7. put.add(Bytes.toBytes("info"), Bytes.toBytes("namessss"), Bytes.toBytes("lisi1234"));
  8. put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234));
  9. //插入数据
  10. table.put(put);
  11. //提交
  12. table.flushCommits();
  13. }

8.删除数据:

  1. /**
  2. * 删除数据
  3. */
  4. @Test
  5. public void deleteDate() throws Exception {
  6. Delete delete = new Delete(Bytes.toBytes("1234"));
  7. delete.addFamily(Bytes.toBytes("info1"));//删除列族
  8. delete.addColumn(Bytes.toBytes("info1"), Bytes.toBytes("name"));//删除列族下的name
  9. table.delete(delete);
  10. table.flushCommits();
  11. }

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

单条查询:

  1. /**
  2. * 单条查询
  3. */
  4. @Test
  5. public void queryData() throws Exception {
  6. Get get = new Get(Bytes.toBytes("1234"));
  7. Result result = table.get(get);
  8. System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));
  9. System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("namessss"))));
  10. System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex"))));
  11. }

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

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

  1. /**
  2. * 全表扫描
  3. */
  4. @Test
  5. public void scanData() throws Exception {
  6. //设置全表扫描封装类
  7. Scan scan = new Scan();
  8. scan.setStartRow(Bytes.toBytes("wangsf_0"));//区间
  9. scan.setStopRow(Bytes.toBytes("wangwu"));
  10. //扫描
  11. ResultScanner scanner = table.getScanner(scan);
  12. for (Result result : scanner) {
  13. System.out.println(Bytes.toString(result.getRow())); //携带的rowkey
  14. System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));
  15. System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
  16. }
  17. }

过滤器查询

1.hbase过滤器:  

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

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

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

2.过滤器的种类: 

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

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

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

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

2.1.列值过滤器:

  1. /**
  2. * 全表扫描的过滤器
  3. * 列值过滤器
  4. */
  5. @Test
  6. public void scanDataByFilter1() throws Exception {
  7. // 创建全表扫描的scan
  8. Scan scan = new Scan();
  9. //过滤器:列值过滤器
  10. SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"),
  11. Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL,
  12. Bytes.toBytes("zhangsan2"));//下面示例检查列值和字符串'zhangsan2'相等
  13. //设置过滤器
  14. scan.setFilter(filter);
  15. // 打印结果集
  16. ResultScanner scanner = table.getScanner(scan);
  17. for (Result result : scanner) {
  18. System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));
  19. System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
  20. }
  21. }

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

  1. /**
  2. * 匹配列名前缀过滤器
  3. */
  4. @Test
  5. public void scanDataByFilter3() throws Exception {
  6. // 创建全表扫描的scan
  7. Scan scan = new Scan();
  8. //匹配rowkey以wangsenfeng开头的
  9. ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("na"));
  10. // 设置过滤器
  11. scan.setFilter(filter);
  12. // 打印结果集
  13. ResultScanner scanner = table.getScanner(scan);
  14. for (Result result : scanner) {
  15. System.out.println("rowkey:" + Bytes.toString(result.getRow()));
  16. System.out.println("info:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("name"))));
  17. // 判断取出来的值是否为空
  18. if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) {
  19. System.out.println("info:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"))));
  20. }
  21. // 判断取出来的值是否为空
  22. if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) {
  23. System.out.println("infi:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("sex"))));
  24. }
  25. // 判断取出来的值是否为空
  26. if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) {
  27. System.out.println("info2:name:"+ Bytes.toString(result.getValue( Bytes.toBytes("info2"),Bytes.toBytes("name"))));
  28. }
  29. // 判断取出来的值是否为空
  30. if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) {
  31. System.out.println("info2:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("age"))));
  32. }
  33. // 判断取出来的值是否为空
  34. if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) {
  35. System.out.println("info2:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("sex"))));
  36. }
  37. }
  38. }

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方法比较好

  1. /**
  2. * rowkey过滤器
  3. */
  4. @Test
  5. public void scanDataByFilter2() throws Exception {
  6. // 创建全表扫描的scan
  7. Scan scan = new Scan();
  8. //匹配rowkey以12341开头的
  9. RowFilter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^12341"));
  10. // 设置过滤器
  11. scan.setFilter(filter);
  12. // 打印结果集
  13. ResultScanner scanner = table.getScanner(scan);
  14. for (Result result : scanner) {
  15. System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));
  16. System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));
  17. }
  18. }

2.5.过滤器集合FilterList: 

  1. /**
  2. * 过滤器集合
  3. */
  4. @Test
  5. public void scanDataByFilter4() throws Exception {
  6. // 创建全表扫描的scan
  7. Scan scan = new Scan();
  8. //过滤器集合:MUST_PASS_ALL(and),MUST_PASS_ONE(or)
  9. FilterList filterList = new FilterList(Operator.MUST_PASS_ONE);
  10. //匹配rowkey以wangsenfeng开头的
  11. RowFilter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^wangsenfeng"));
  12. //匹配name的值等于wangsenfeng
  13. SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("info"),
  14. Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL,
  15. Bytes.toBytes("zhangsan"));
  16. filterList.addFilter(filter);
  17. filterList.addFilter(filter2);
  18. // 设置过滤器
  19. scan.setFilter(filterList);
  20. // 打印结果集
  21. ResultScanner scanner = table.getScanner(scan);
  22. for (Result result : scanner) {
  23. System.out.println("rowkey:" + Bytes.toString(result.getRow()));
  24. System.out.println("info:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("name"))));
  25. // 判断取出来的值是否为空
  26. if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age")) != null) {
  27. System.out.println("info:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("age"))));
  28. }
  29. // 判断取出来的值是否为空
  30. if (result.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex")) != null) {
  31. System.out.println("infi:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("sex"))));
  32. }
  33. // 判断取出来的值是否为空
  34. if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("name")) != null) {
  35. System.out.println("info2:name:"+ Bytes.toString(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("name"))));
  36. }
  37. // 判断取出来的值是否为空
  38. if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age")) != null) {
  39. System.out.println("info2:age:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("age"))));
  40. }
  41. // 判断取出来的值是否为空
  42. if (result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("sex")) != null) {
  43. System.out.println("info2:sex:"+ Bytes.toInt(result.getValue(Bytes.toBytes("info2"),Bytes.toBytes("sex"))));
  44. }
  45. }
  46.  
  47. }

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

  

5.Hbase API 操作开发的更多相关文章

  1. HBase API操作

    |的ascII最大ctrl+shift+t查找类  ctrl+p显示提示 HBase API操作 依赖的jar包 <dependencies> <dependency> < ...

  2. 通过HBase API进行开发

    http://www.cnblogs.com/netbloomy/p/6683509.html 一.将HBase的jar包及hbase-site.xml添加到IDE 1.到安装HBase集群的任意一台 ...

  3. 2、通过HBase API进行开发

    一.将HBase的jar包及hbase-site.xml添加到IDE 1.到安装HBase集群的任意一台机器上找到HBase的安装目录,到lib目录下下载HBase需要的jar包,然后再到conf目录 ...

  4. HBASE API操作问题总结

    org.apache.hadoop.hbase.MasterNotRunningException 在centos中查看,发现没有HMaster进程 解决方法: 1.启动hadoop后,需要等一段时间 ...

  5. Hbase——API操作

    1.判断表是否存在 public static boolean isTableExit(String tableName) throws IOException { // //获取配置文件信息 // ...

  6. Hbase Shell命令详解+API操作

    HBase Shell 操作 3.1 基本操作1.进入 HBase 客户端命令行,在hbase-2.1.3目录下 bin/hbase shell 2.查看帮助命令 hbase(main):001:0& ...

  7. 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化

    第6章 HBase API 操作6.1 环境准备6.2 HBase API6.2.1 判断表是否存在6.2.2 抽取获取 Configuration.Connection.Admin 对象的方法以及关 ...

  8. Hbase 设计与开发实战

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

  9. 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 ...

随机推荐

  1. Robot:robot如何连接Oracle数据库(windows+linux)

    1.需要安装基础数据库 pip install robotframework-databaselibrary 2.下载并安装对应版本的cx_Oracle,注意要和Oracle版本.系统位数.pytho ...

  2. 最新 字节跳动java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.字节跳动等10家互联网公司的校招Offer,因为某些自身原因最终选择了字节跳动.6.7月主要是做系统复习.项目复盘.Leet ...

  3. python邮件发送自动化测试报告

    话不多说直接贴代码 # encoding: utf-8import smtplib #发送邮件模块from email.mime.text import MIMEText #邮件内容from emai ...

  4. 第一周——不同java 的发展史你,注定成为不了领袖型人才

     James Gosling  1991年,James 高斯林在Sun公司的工程师小组想要设计这样一种小型计算机语言,该语言主要用于电视盒的消费类电子产品上.另外,由于不同的厂商选择不同的CPU和操作 ...

  5. 使ssh记住密码

    前面介绍过Conemu , Msys2工具,提升了windows本地文件操作的效率,但使用ssh时候,总需要重新输入密码,不能像SercurityCRT等工具记住密码,使用时不是很爽快. 其实通过ss ...

  6. java输入输出 -- Java NIO之选择器

    一.简介 前面的文章说了缓冲区,说了通道,本文就来说说 NIO 中另一个重要的实现,即选择器 Selector.在更早的文章中,我简述了几种 IO 模型.如果大家看过之前的文章,并动手写过代码的话.再 ...

  7. Python列表排序方法reverse、sort、sorted详解

    python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...

  8. PHP中类成员的访问控制

    类成员访问控制: 1.public 默认的,任何地方都可以访问,类内,类外,子类中 2.protected 受保护的,对外是封闭的,但是类内部和子类可以访问 3.private  私有的,仅限于本类中 ...

  9. MAC自带Apache配置python3

    进入终端 sudo apachectl start 直接访问localhost 解决Mac下apache 403的问题 网上查资料发现是因为Mac版本升级导致了apache策略发生变更了,所以我们修改 ...

  10. eclipse New菜单项的显示问题

    设置自己想要的New菜单 链接:http://www.cnblogs.com/shindo/p/7089141.html