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开发实例的更多相关文章

  1. ecshop二次开发 给商品添加自定义字段【包含我自己进一步的开发实例详解】

    本文包含商品自定义添加教程及进一步的开发实例: 教程: 说起自定义字段,我想很多的朋友像我一样会想起一些开源的CMS(比如Dedecms.Phpcms.帝国)等,他们是可以在后台直接添加自定义字段的. ...

  2. RDIFramework.NET -.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(WebForm版)

    RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(WebForm版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之 ...

  3. RDIFramework.NET-.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(MVC版)

    RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(MVC版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之产品管理 ...

  4. Cocos2d-x 3.X手游开发实例详解

    Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...

  5. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)

    最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...

  6. RDIFramework.NET开发实例━表约束条件权限的使用-Web

    RDIFramework.NET开发实例━表约束条件权限的使用-Web 在上一篇文章“RDIFramework.NET开发实例━表约束条件权限的使用-WinForm”我们讲解了在WinForm下表约束 ...

  7. RDIFramework.NET开发实例━表约束条件权限的使用-WinForm

    RDIFramework.NET开发实例━表约束条件权限的使用-WinForm 在实际的应用中,客户常有这样的需求,指定用户或角色可以看指定条件下的数据,这里的“指定条件”在RDIFramework. ...

  8. RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm)

    RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm) 现在,我们使用.NET快速开发整合框架(RDIFramework.NET)来开发一个应用,此应用皆在说明如何使 ...

  9. Android音乐播放器的开发实例

    本文将引导大家做一个音乐播放器,在做这个Android开发实例的过程中,能够帮助大家进一步熟悉和掌握学过的ListView和其他一些组件.为了有更好的学习效果,其中很多功能我们手动实现,例如音乐播放的 ...

随机推荐

  1. LeetCode-1TwoSum(C#)

    # 题目 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up ...

  2. .NET基础 一步步 一幕幕[循环、逻辑语句块]

    循环.逻辑语句块   好久不写博客了,断更了好几天了,从上周五到今天,从北京到上海,跨越了1213.0公里,从一个熟悉的城市到陌生的城市,还好本人适应力比较好,还有感谢小伙伴的接风咯,一切都不是事,好 ...

  3. 搭建一个简单的mybatis框架

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  4. Hibernate整合Spring异常'sessionFactory' or 'hibernateTemplate' is required

    今日在写GenericDao时,发现了一个异常,内容如下: org.springframework.beans.factory.BeanCreationException: Error creatin ...

  5. 在linux平台实现atosl

    ➠更多技术干货请戳:听云博客 序言 怎么在linux 平台下实现一个类似于mac 平台下的 atos 工具( iOS 符号化解析)? 分析问题 在github上找到了几年前的开源实现,[https:/ ...

  6. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  7. Android之常见问题集锦Ⅰ

     Android中如何在ViewPager中使动态创建的ImageView铺满屏幕 最近在做东西的时候,有一个要求,就是把用于在ViewPager里面轮播的图片铺满屏幕,但是中间遇到的问题是,Imag ...

  8. 总结个关于MySQL数据库的问题

    问题概括:MySQL Server has gone away? 遇到这个问题还得追溯到这次前往南通软件园出差.当天下午下班之前,主管说可能明天出差,把项目和最新的数据库备份一下,备份完成之后,也没在 ...

  9. SQL Server 并行操作优化,避免并行操作被抑制而影响SQL的执行效率

    为什么我也要说SQL Server的并行: 这几天园子里写关于SQL Server并行的文章很多,不管怎么样,都让人对并行操作有了更深刻的认识. 我想说的是:尽管并行操作可能(并不是一定)存在这样或者 ...

  10. awk使用说明

    原文地址:http://www.cnblogs.com/verrion/p/awk_usage.html Awk使用说明 运维必须掌握的三剑客工具:grep(文件内容过滤器),sed(数据流处理器), ...