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和其他一些组件.为了有更好的学习效果,其中很多功能我们手动实现,例如音乐播放的 ...
随机推荐
- [NodeJS] Hello World 起步教程
概述: 做数据,免不了需要展示数据,数据可视化是必须经历的步骤. 本文将提供一个NodeJS的起步教程,是笔者这两天探索的小结. 正文: 1. 为什么使用NodeJS 究竟是以B/S还是C/S的架构 ...
- ABP源码分析三十六:ABP.Web.Api
这里的内容和ABP 动态webapi没有关系.除了动态webapi,ABP必然是支持使用传统的webApi.ABP.Web.Api模块中实现了一些同意的基础功能,以方便我们创建和使用asp.net w ...
- Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉
Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉 1.1. 按照当前流行的分类方法,可以分为以下三部分:三部分 图像处理 图像分析 计算机视觉1 1.2. 图像处理需要 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(22)-权限管理系统-模块导航制作
系列目录 最近比较忙,系统难度独步增加,文章的发布速度明显比以前慢了. 由于我们已经跑通了整个系统,所有东西都回到了简单,接下来我们做模块制作也就是操作SysModule表. 首先我们来回顾一下之前的 ...
- 数据结构:二叉查找树(C语言实现)
数据结构:二叉查找树(C语言实现) ►写在前面 关于二叉树的基础知识,请看我的一篇博客:二叉树的链式存储 说明: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 1.若其左子树不空,则左子树上 ...
- Linux同平台数据库整体物理迁移
Linux同平台数据库整体物理迁移 需求:A机器不再使用,要将A机器的Oracle迁移到B机器. 之前写过类似需求的文章:http://www.cnblogs.com/jyzhao/p/3968504 ...
- niginx代理配置
常用关键词:rewrite.proxy_pass location ^~ /address/ { proxy_set_header Host xx.sohu.com; #设置header proxy_ ...
- 用php实现一个简单的链式操作
最近在读<php核心技术与最佳实践>这本书,书中第一章提到用__call()方法可以实现一个简单的字符串链式操作,比如,下面这个过滤字符串然后再求长度的操作,一般要这么写: strlen( ...
- 设置nginx禁止IP直接访问,只能通过指定的域名访问
nginx的版本是1.2.1. 设置配置文件disableip.conf: server { listen 80; server_name _; return500; } 这是 ...
- 利用WCF双工模式实现即时通讯
概述 WCF陆陆续续也用过多次,但每次都是浅尝辄止,以将够解决问题为王道,这几天稍闲,特寻了些资料看,昨晚尝试使用WCF的双工模式实现了一个简单的即时通讯程序,通过服务端转发实现客户端之间的通讯.这只 ...