hbase开发实例
1、put/checkAndPut
package com.testdata; import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes; public class TestPut { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf); HTableInterface table = conn.getTable("testdata");
Put testput = new Put(Bytes.toBytes("row1"));
testput.add(Bytes.toBytes("cf"),Bytes.toBytes("col1"),Bytes.toBytes("E"));
table.put(testput);
//使用checkAndPut
table.checkAndPut(Bytes.toBytes("row1"), Bytes.toBytes("cf"),Bytes.toBytes("col5"),Bytes.toBytes("E"),testput);
table.close();
conn.close(); } }
使用checkAndPut,需要先对数据进行验证,上面的例子中,向row1中的cf:col1写入数据"E",而验证的是row1中的cf:col5的值是否为"E",注意这一点,相当于加了条件。

2、使用get读取数据
package com.testdata; import java.io.IOException;
import java.util.List; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; public class TestGet { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Get testget = new Get(Bytes.toBytes("row1"));
Result row1 = table.get(testget);
String value = new String(row1.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col1")));
System.out.println(value);
//下面限定到具体的列
testget.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col2"));
Result result = table.get(testget);
if(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col2")) != null){
String value2 = new String(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col2")));
System.out.println(value2);
}
//另外一种读取方式
List<Cell> cells = row1.listCells();
for(Cell cell : cells){
String rowkey = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String collumn = new String(CellUtil.cloneQualifier(cell));
String cvalue = new String(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue); } //注意要关闭
table.close();
conn.close(); } }
参考结果:

3、使用scan获取数据
package com.testdata; import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan; public class TestScan { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Scan testscan =new Scan();
ResultScanner rs = table.getScanner(testscan);
for(Result r : rs ){
List<Cell> cells = r.listCells();
for(Cell cell : cells){
String rowkey = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String collumn = new String(CellUtil.cloneQualifier(cell));
String cvalue = new String(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue); }
}
rs.close();
table.close();
conn.close(); } }
4、delete/checkAndDelete
package com.testdata; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import java.io.IOException; import org.apache.hadoop.hbase.util.Bytes; public class TestDelete { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Delete testdelete = new Delete(Bytes.toBytes("row1"));
testdelete.deleteColumns(Bytes.toBytes("cf"), Bytes.toBytes("col1"));
//区别只是checkAndDelete需要进行验证,相当于加了前提条件
//table.delete(testdelete);
table.checkAndDelete(Bytes.toBytes("row1"), Bytes.toBytes("cf"), Bytes.toBytes("col2"),Bytes.toBytes("BC"), testdelete);
table.close();
conn.close(); } }
5、append
package com.testdata; import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Append;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.util.Bytes; public class TestAppend { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Append testappend = new Append(Bytes.toBytes("row1"));
testappend.add(Bytes.toBytes("cf"),Bytes.toBytes("col1"),Bytes.toBytes("F"));
table.append(testappend);
table.close();
conn.close();
} }
下面是结果,注意append是在原有的值之上附加,先前的值为"E",现在变为"EF"

6、计数器
计数器可以用于统计用户数,点击量等信息
package com.testdata; import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.util.Bytes; public class TestIncrement { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); long result = table.incrementColumnValue(Bytes.toBytes("row1"),Bytes.toBytes("cf"),Bytes.toBytes("coli"), 10); System.out.println(result);
table.close();
conn.close(); } }
注意 long result = table.incrementColumnValue(Bytes.toBytes("row1"),Bytes.toBytes("cf"),Bytes.toBytes("coli"), 10);
最后一个参数,可以为0,意味着读取,也可以是负数。
可以使用get_counter可以获取对应的计数器的值,也可以使用以下命令进行操作
incr '<table>', '<row>', '<column>', |<increment-value>|

7、filter
使用时注意性能
package com.testdata; import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes; public class TestSimplefilter { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Scan sc = new Scan();
sc.setCacheBlocks(false);
//行过滤器,判断"row1"与行的key是否相等
//Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("row1")));
//是否以"row"为前缀
//Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator(Bytes.toBytes("row")));
//是否包含"row"
//Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("row")); //列过滤器,与行类似
Filter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("col1"))); sc.setFilter(filter); ResultScanner rs = table.getScanner(sc);
for(Result r : rs ){
List<Cell> cells = r.listCells();
for(Cell cell : cells){
String rowkey = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String collumn = new String(CellUtil.cloneQualifier(cell));
String cvalue = new String(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue);
}
}
rs.close();
table.close();
conn.close();
}
}
使用filterlist
package com.testdata; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes; public class TestFilterList { public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","localhost");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn = HConnectionManager.createConnection(conf);
HTableInterface table = conn.getTable("testdata"); Scan sc = new Scan();
Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("row2")));
SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("cf"),Bytes.toBytes("col1"),CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator(Bytes.toBytes("B"))); SingleColumnValueFilter filter3 = new SingleColumnValueFilter(Bytes.toBytes("cf"),Bytes.toBytes("col1"),CompareFilter.CompareOp.EQUAL,new RegexStringComparator("B|C"));
filter2.setFilterIfMissing(true);
filter3.setFilterIfMissing(true); //List<Filter> filters = new ArrayList<Filter>();
//filters.add(filter1);
//filters.add(filter2);
//FilterList filterlist = new FilterList(filters); //也可以这样写,MUST_PASS_ALL标识满足所有的filter,当然也可以使用MUST_PASS_ONE,标识只需要满足一个
FilterList filterlist = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterlist.addFilter(filter1);
filterlist.addFilter(filter2);
filterlist.addFilter(filter3);
sc.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("col1"));
sc.setFilter(filterlist); ResultScanner rs = table.getScanner(sc);
for(Result r : rs ){
List<Cell> cells = r.listCells();
for(Cell cell : cells){
String rowkey = new String(CellUtil.cloneRow(cell));
String family = new String(CellUtil.cloneFamily(cell));
String collumn = new String(CellUtil.cloneQualifier(cell));
String cvalue = new String(CellUtil.cloneValue(cell));
System.out.println("rowkey:" + rowkey + " family:" + family + " column:" + collumn +" value:" + cvalue);
}
}
rs.close();
table.close();
conn.close();
}
}
以上一组filter标识了这样的条件,即行的key必须为"row2",列名必须为"col1",值必须为"B"
结果参考:
如果没有 sc.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("col1"));这一句,结果会是下面的样子
rowkey:row2 family:cf column:col1 value:B
rowkey:row2 family:cf column:colb value:U
问题出在 SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("cf"),Bytes.toBytes("col1"),CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator(Bytes.toBytes("B")));这一句,如果打印Bytes.toBytes("B")与Bytes.toBytes("U"),会发现都是以"B"开头的。即使换成BinaryComparator,也不会解决问题。
这里是值得注意的地方,搜索网络可以发现一样的结论,使用时务必使用sc.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("col1"))类似的语句。
rowkey:row2 family:cf column:col1 value:B
hbase开发实例的更多相关文章
- ecshop二次开发 给商品添加自定义字段【包含我自己进一步的开发实例详解】
本文包含商品自定义添加教程及进一步的开发实例: 教程: 说起自定义字段,我想很多的朋友像我一样会想起一些开源的CMS(比如Dedecms.Phpcms.帝国)等,他们是可以在后台直接添加自定义字段的. ...
- RDIFramework.NET -.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(WebForm版)
RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(WebForm版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之 ...
- RDIFramework.NET-.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(MVC版)
RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(MVC版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之产品管理 ...
- Cocos2d-x 3.X手游开发实例详解
Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...
- 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)
最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...
- RDIFramework.NET开发实例━表约束条件权限的使用-Web
RDIFramework.NET开发实例━表约束条件权限的使用-Web 在上一篇文章“RDIFramework.NET开发实例━表约束条件权限的使用-WinForm”我们讲解了在WinForm下表约束 ...
- RDIFramework.NET开发实例━表约束条件权限的使用-WinForm
RDIFramework.NET开发实例━表约束条件权限的使用-WinForm 在实际的应用中,客户常有这样的需求,指定用户或角色可以看指定条件下的数据,这里的“指定条件”在RDIFramework. ...
- RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm)
RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm) 现在,我们使用.NET快速开发整合框架(RDIFramework.NET)来开发一个应用,此应用皆在说明如何使 ...
- Android音乐播放器的开发实例
本文将引导大家做一个音乐播放器,在做这个Android开发实例的过程中,能够帮助大家进一步熟悉和掌握学过的ListView和其他一些组件.为了有更好的学习效果,其中很多功能我们手动实现,例如音乐播放的 ...
随机推荐
- Java垃圾回收
垃圾收集算法 引用计数 堆中的每个对象都有一个引用计数,当对象被引用时引用计数加1,当对象的引用被重新赋值或超出有效区域时引用计数减1,当一个对象被回收后,它所引用的对象的引用计算减1.当一个对象的引 ...
- ABP框架 - 实体
文档目录 本节内容: 实体类 聚合根类 领域事件 约定的接口 审计 软删除 活跃/消极 实体 实体变化事件 IEntity 接口 实体是DDD一个核心的概念.Eric Evans是这么描述的:“一个对 ...
- C++ std::list
std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...
- Code First Migrations
在MVC开发当中难免会对类进行修改,修改后再次运行就会出现异常,提示上下文的模型已在数据库创建后发生改变. 支持“AppContext”上下文的模型已在数据库创建后发生更改.请考虑使用 Code Fi ...
- Javascript刷题 》数组求和
计算给定数组 arr 中所有元素的总和 输入描述: 数组中的元素均为 Number 类型 输入例子: sum([ 1, 2, 3, 4 ]) 输出例子: 10 解题方法 1.定义一个变量,将前面的和后 ...
- VS2010开发工具使用技巧<之简单讲解>
俗语云:工欲善其事必先利其器! 1.代码放大 效果:放大前----------------------------------------------------------------->放大 ...
- EntityFramework.Extended 实现 update count+=1
在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...
- spring ioc
spring ioc是spring的核心之一,也是spring体系的基础,那么spring ioc所依赖的底层技术是什么的?反射,以前我们开发程序的时候对象之间的相互调用需要用new来实现,现在所有的 ...
- 解决新版Android studio导入微信支付和支付宝官方Demo的问题
最近项目要用到支付宝支付和微信支付,本想使用第三方支付框架ping++或者BeeCloud的,但是由于他们的收费问题,让我望而却步,而且公司给了相应的公钥.私钥和APPID等,所以就用下开放平台的呗. ...
- 设置eclipse中自动添加get,set的注释为字段属性的注释
一:说明 首先具体来看一下是什么效果,上图可能会更清楚一点 就是在get/set中自动加上属性的注释,那我们要怎么配置呢? 二:配置 2.1:下载附件 下载附件 2.2:替换class 原生的ecli ...