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

1.配置 HBaseConfiguration

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

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

  static Configuration config = null;
private Connection connection = null;
private Table table = null;
@Before
public 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" })
@Test
public 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.修改数据:

    /**
* 修改数据
*/
@Test
public 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.删除数据:

     /**
* 删除数据
*/
@Test
public 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.查询数据:(单挑查询、批量查询、过滤器查询)

单条查询:

    /**
* 单条查询
*/
@Test
public 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(获取值的接口)

/**
* 全表扫描
*/
@Test
public 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())); //携带的rowkey
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"))));
}
}

过滤器查询

1.hbase过滤器:  

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

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

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

2.过滤器的种类: 

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

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

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

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

2.1.列值过滤器:

/**
* 全表扫描的过滤器
* 列值过滤器
*/
@Test
public void scanDataByFilter1() throws Exception {
// 创建全表扫描的scan
Scan 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:用于指定列名前缀值相等

/**
* 匹配列名前缀过滤器
*/
@Test
public void scanDataByFilter3() throws Exception {
// 创建全表扫描的scan
Scan 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过滤器
*/
@Test
public void scanDataByFilter2() throws Exception {
// 创建全表扫描的scan
Scan 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: 

     /**
* 过滤器集合
*/
@Test
public void scanDataByFilter4() throws Exception {
// 创建全表扫描的scan
Scan 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的值等于wangsenfeng
SingleColumnValueFilter 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

  

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. Java遍历日期代码

    import java.util.ArrayList; import java.util.List; public class DateTraveller { public static List&l ...

  2. nmap探测大网络空间中的存活主机

    前言 扫描大网络空间中的存活主机 实现 nmap -v -sn -PE -n --min-hostgroup 1024 --min-parallelism 1024 -oX nmap_output.x ...

  3. rest_framework之三种分页器使用方法

      from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination ...

  4. 记:联调安卓设备的神药-无需usb数据线即可直连

    前言 最近需要调试公司的安卓服务,正常情况下,我们调试都是减安卓设备通过usb连接在我们座位旁,再不济就是我们扛笔记本到硬件旁边,这样调试屡试不爽,但是有一天你突然发现你带的数据线因为各种原因总是终端 ...

  5. 登陆Linux服务器时触发邮件提醒

    目前,客户只能在发现数据或者虚拟机被恶意侵入或者用户的误操作导致了数据的丢失之后,采取善后的手段,但是并没法做到提前的预警.那么通过 PAM 模块,就可以实现用户登录及获取root 权限时,通过邮件的 ...

  6. Neo4j常用的查询

    一.添加操作 1. 添加节点: create (x:学生{studentId:'1001',age:20} 2. 添加关系: 对现有的节点添加关系 match (x:学生{studentId:1001 ...

  7. java 微信开发的工具类WeChatUtils

    import com.alibaba.fastjson.JSONObject;import com.bhudy.entity.BhudyPlugin;import com.bhudy.service. ...

  8. vim入门一 常用指令

    以下为自己常用的vim指令总结 一.插入命令 a 在光标所在字符后进入插入模式 A 调到光标所在行行尾进入插入模式 i 在光标所在字符前插入模式 I 调到光标所在行行首进入插入模式 o 调到光标所在上 ...

  9. Python数值日期时间笔记

    数值: 格式化 小数位的处理 随机数: random.choice() 序列中随机选择一个值 random.sample() 获取指定数目的序列 random.shuffle() 打乱顺序 rando ...

  10. Angular系列-AngularJs1使用Ace编辑器

    Ace编辑器 Ace编辑器是一个嵌入web的代码编辑器,支持语法高亮,自动补全等功能,如果想在页面展示或编辑代码,使用该工具是很合适的. 参考项目地址:https://github.com/ajaxo ...