HBase Client API使用(二)---查询及过滤器
相关知识
创建表插入数据删除等见:http://www.cnblogs.com/wishyouhappy/p/3735077.html
HBase API简介见:http://www.cnblogs.com/wishyouhappy/p/3753347.html
按行、列簇等查询
package wish.hbase; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
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.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes; public class HBaseQuerydata
{
static Configuration cfg = HBaseConfiguration.create();
static { cfg.set("hbase.zookeeper.quorum", "192.168.1.95");
cfg.set("hbase.zookeeper.property.clientPort", "2181");
} public static void queryByRowKey(String tablename, String rowKey) throws IOException
{
HTable table = new HTable(cfg, tablename);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g); for (KeyValue kv : rs.raw())
{
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column : " + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} public static void queryByRowKeyFamily(String tablename, String rowKey, String family) throws IOException
{
HTable table = new HTable(cfg, tablename);
Get g = new Get(rowKey.getBytes());
g.addFamily(Bytes.toBytes(family));
Result rs = table.get(g);
for (KeyValue kv : rs.raw())
{
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column : " + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} public static void queryByRowKeyFamilyColumn(String tablename, String rowKey, String family, String column)
throws IOException
{
HTable table = new HTable(cfg, tablename);
Get g = new Get(rowKey.getBytes());
g.addColumn(family.getBytes(), column.getBytes()); Result rs = table.get(g); for (KeyValue kv : rs.raw())
{
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column : " + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} /*
* 查询所有
*/
public static void queryAll(String tableName) {
HTablePool pool = new HTablePool(cfg, 1000);
try {
ResultScanner rs = pool.getTable(tableName).getScanner(new Scan());
for (Result r : rs) {
System.out.println("rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ " 值:" + new String(keyValue.getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws Exception
{
System.out.println("******************************queryall******************************");
queryAll("wishtest1");System.out.println("******************************query by rowkey******************************");
queryByRowKey("wishTest1", "2");
System.out.println("******************************query by rowkey family******************************");
queryByRowKeyFamily("wishTest1", "2","name");
System.out.println("******************************query by rowkey family column******************************");
queryByRowKeyFamilyColumn("wishTest1", "6","score","Chinese");
}
}
输出如下:
******************************queryall******************************
rowkey:****1121
列:age 值:20
列:gender 值:male
列:name 值:Chander
列:score 值:120
列:score 值:100
列:score 值:99
rowkey:*1111
列:age 值:20
列:gender 值:male
列:name 值:Chander
列:score 值:120
列:score 值:100
列:score 值:99
rowkey:1
列:age 值:20
列:gender 值:male
列:name 值:rain
rowkey:2
列:age 值:20
列:gender 值:female
列:name 值:wish
rowkey:3
列:age 值:20
列:gender 值:male
列:name 值:Joey
rowkey:4
列:age 值:20
列:gender 值:male
列:name 值:Jenny
列:score 值:90
列:score 值:100
列:score 值:100
列:score 值:90
rowkey:5
列:age 值:20
列:gender 值:male
列:name 值:Chander
列:score 值:120
列:score 值:100
列:score 值:99
rowkey:6
列:age 值:20
列:gender 值:male
列:name 值:Joey
列:score 值:100
列:score 值:100
列:score 值:90
******************************query by rowkey******************************
rowkey: 2
Column Family: age
Column :
value : 20
rowkey: 2
Column Family: gender
Column :
value : female
rowkey: 2
Column Family: name
Column :
value : wish
******************************query by rowkey family******************************
rowkey: 2
Column Family: name
Column :
value : wish
******************************query by rowkey family column******************************
rowkey: 6
Column Family: score
Column : Chinese
value : 100
行过滤器
使用过滤器可以提高操作表的效率,HBase中两种数据读取函数get()和scan()都支持过滤器
可以使用预定义好的过滤器或者是实现自定义过滤器
过滤器在客户端创建,通过RPC传送到服务器端,在服务器端执行过滤操作
行过滤器简单例子如下:
测试table中所有数据如下:
rowkey:****1121
列:age 值:20
列:gender 值:male
列:name 值:Chander
列:score 值:120
列:score 值:100
列:score 值:99
rowkey:*1111
列:age 值:20
列:gender 值:male
列:name 值:Chander
列:score 值:120
列:score 值:100
列:score 值:99
rowkey:1
列:age 值:20
列:gender 值:male
列:name 值:rain
rowkey:2
列:age 值:20
列:gender 值:female
列:name 值:wish
rowkey:3
列:age 值:20
列:gender 值:male
列:name 值:Joey
rowkey:4
列:age 值:20
列:gender 值:male
列:name 值:Jenny
列:score 值:90
列:score 值:100
列:score 值:100
列:score 值:90
rowkey:5
列:age 值:20
列:gender 值:male
列:name 值:Chander
列:score 值:120
列:score 值:100
列:score 值:99
rowkey:6
列:age 值:20
列:gender 值:male
列:name 值:Joey
列:score 值:100
列:score 值:100
列:score 值:90
eg1:
public static void rowFilterLESS_OR_EQUAL(String tableName, String columnFamily, String column, String condition){
System.out.println("***********************start rowFilterLESS_OR_EQUAL***************************");
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column)); Filter f = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes(condition)));
scan.setFilter(f);
HTablePool pool = new HTablePool(cfg, 1000);
try {
ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
Result rs = resultScanner.next();
for (; rs != null; rs = resultScanner.next())
{
for (KeyValue kv : rs.list())
{
System.out.println("-------------------------------");
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("***********************end rowFilterLESS_OR_EQUAL***************************"); }
public static void main(String[] args) {
String tableName = "wishTest1";
HBaseFilter.rowFilterLESS_OR_EQUAL(tableName, "score", "Math", "4");
}
输出:
***********************start rowFilterLESS_OR_EQUAL*************************** -------------------------------
rowkey: ****1121
Column Family: score
Column :Math
value : 99
-------------------------------
rowkey: *1111
Column Family: score
Column :Math
value : 99
-------------------------------
rowkey: 4
Column Family: score
Column :Math
value : 90
-------------------------------
rowkey: 5
Column Family: score
Column :Math
value : 99
***********************end rowFilterLESS_OR_EQUAL***************************
eg2:
/*
* rowFilterRegex
*/
public static void rowFilterRegex(String tableName, String columnFamily, String column, String condition){
System.out.println("***********************start rowFilterRegex***************************");
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column)); Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("\\*."));
scan.setFilter(f);
HTablePool pool = new HTablePool(cfg, 1000);
try {
ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
Result rs = resultScanner.next();
for (; rs != null; rs = resultScanner.next())
{
for (KeyValue kv : rs.list())
{
System.out.println("-------------------------------");
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("***********************end rowFilterRegex***************************"); }
public static void main(String[] args) {
String tableName = "wishTest1";
HBaseFilter.rowFilterRegex(tableName, "score", "Math", "\\*");
}
输出:
***********************start rowFilterRegex***************************
-------------------------------
rowkey: ****1121
Column Family: score
Column :Math
value : 99
-------------------------------
rowkey: *1111
Column Family: score
Column :Math
value : 99
***********************end rowFilterRegex***************************
eg3:
/*
* rowFilterSubstring
*/
public static void rowFilterSubstring(String tableName, String columnFamily, String column, String condition){
System.out.println("***********************start rowFilterSubstring***************************");
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column)); Filter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("\\*"));
scan.setFilter(f);
HTablePool pool = new HTablePool(cfg, 1000);
try {
ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
Result rs = resultScanner.next();
for (; rs != null; rs = resultScanner.next())
{
for (KeyValue kv : rs.list())
{
System.out.println("-------------------------------");
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("***********************end rowFilterSubstring***************************"); }
public static void main(String[] args) {
String tableName = "wishTest1";
HBaseFilter.rowFilterSubstring(tableName, "score", "Math", "\\*");
}
输出同上
列簇过滤器
public static void familyFilterLess(String tableName, String condition){
Filter filter = new FamilyFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes(condition)));
Scan scan = new Scan();
scan.setFilter(filter);
HTablePool pool = new HTablePool(cfg,100);
try {
ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
Result rs = resultScanner.next();
for (; rs != null; rs = resultScanner.next())
{
for (KeyValue kv : rs.list())
{
System.out.println("-------------------------------");
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} resultScanner.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
String tableName = "wishTest1";
HBaseFilter.familyFilterLess(tableName, "gender");
//HBaseFilter.rowFilterSubstring(tableName, "score", "Math", "\\*");
}
输出:
-------------------------------
rowkey: ****1121
Column Family: age
Column :
value : 20
-------------------------------
rowkey: *1111
Column Family: age
Column :
value : 20
-------------------------------
rowkey: 1
Column Family: age
Column :
value : 20
-------------------------------
rowkey: 2
Column Family: age
Column :
value : 20
-------------------------------
rowkey: 3
Column Family: age
Column :
value : 20
-------------------------------
rowkey: 4
Column Family: age
Column :
value : 20
-------------------------------
rowkey: 5
Column Family: age
Column :
value : 20
-------------------------------
rowkey: 6
Column Family: age
Column :
value : 20
列名过滤器
/*
* 列名过滤器
*/ public static void qualifierFilterLess(String tableName, String condition){
Filter filter = new QualifierFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes(condition)));
Scan scan = new Scan();
scan.setFilter(filter);
HTablePool pool = new HTablePool(cfg,100);
try {
ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
Result rs = resultScanner.next();
for (; rs != null; rs = resultScanner.next())
{
for (KeyValue kv : rs.list())
{
System.out.println("-------------------------------");
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} resultScanner.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
值过滤器
/*
* 值过滤器
*/ public static void valueFilterLess(String tableName, String condition){
Filter filter = new ValueFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes(condition)));
Scan scan = new Scan();
scan.setFilter(filter);
HTablePool pool = new HTablePool(cfg,100);
try {
ResultScanner resultScanner = pool.getTable(tableName).getScanner(scan);
Result rs = resultScanner.next();
for (; rs != null; rs = resultScanner.next())
{
for (KeyValue kv : rs.list())
{
System.out.println("-------------------------------");
System.out.println("rowkey: " + new String(kv.getRow()));
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
} resultScanner.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
HBase Client API使用(二)---查询及过滤器的更多相关文章
- 第四部分 数据搜索之使用HBASE的API实现条件查询
因为数据清洗部分需要用到Mapreduce,所以先解决hbase的问题,可以用命令先在hbase存一下简单的数据进行查询,之后只要替换数据就可以实现了原本功能 在看该部分前,确保Hase API看了, ...
- HBase 相关API操练(二):Java API
一.HBase Java编程 (1)HBase是用Java语言编写的,它支持Java编程: (2)HBase支持CRUD操作:Create,Read,Update和Delete: (3)Java AP ...
- 第六部分 数据搜索之使用HBASE的API实现条件查询
题目 使用HADOOP的MAPReduce,实现以下功能: (1)基于大数据计算技术的条件查询:使用mapreduce框架,实现类似Hbase六个字段查询的功能 (2)时段流量统计:以hh:mm:ss ...
- HBASE的读写以及client API
一:读写思想 1.系统表 hbase:namespace 存储hbase中所有的namespace的信息 hbase:meta rowkey:hbase中所有表的region的名称 column:re ...
- 073 HBASE的读写以及client API
一:读写思想 1.系统表 hbase:namespace 存储hbase中所有的namespace的信息 hbase:meta rowkey:hbase中所有表的region的名称 column:re ...
- TFS API:二、TFS 代码查询工作项
TFS API:二.TFS 代码查询工作项 首先我们需要认识TFS的两大获取服务对象的类. 他们分别为TfsConfigurationServer和TfsTeamProjectCollection, ...
- HBase总结(十二)Java API 与HBase交互实例
HBase提供了Java Api的訪问接口,掌握这个就跟Java应用使用RDBMS时须要JDBC一样重要 import java.io.IOException; import org.apache.h ...
- Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
目录 引言 Search APIs Search API Search Request 可选参数 使用 SearchSourceBuilder 构建查询条件 指定排序 高亮请求 聚合请求 建议请求 R ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十)ES6.2.2 Client API
scala版本2.11 java版本1.8 spark版本2.2.1 es版本6.2.2 hadoop版本2.9.0 elasticsearch节点列表: 192.168.0.120 192.168. ...
随机推荐
- 深入理解Android 自定义attr Style styleable以及其应用
相信每一位从事Android开发的猿都遇到过需要自己去自定义View的需求,如果想通过xml指定一些我们自己需要的参数,就需要自己声明一个styleable,并在里面自己定义一些attr属性,这个过程 ...
- iPad横竖屏代码适配
你可能非常了解用不同的方式去适配不同尺寸的iPhone屏幕,在适配iPhone屏幕时你需要考虑的只是屏幕大小变化带来的UI元素间隔的变化,但是在iPad上主要针对的是横竖屏下完全不同的UI元素的布局, ...
- gridview添加header
gridview是不能添加header的,这里的解决方法是将listview改造成gridview使用,功能很好用,唯一的缺点是列数不能自适应 示例代码下载地址http://pan.baidu.com ...
- Java基础知识强化17:JAVA不可以将所覆盖的方法的访问权限变得比父类的小
首先我们看一下下面的代码如下: interface I { void go(); } abstract class A implements I { } class C extends A { voi ...
- HUD 2089 位数dp
/* 做的不多的位数dp 暴力的话 不知道多少组数据 会T 所以写dp 思路就和数学课本上那种“不超过xxx的x位偶数有几个” 这里可以类似的维护一个前缀和模样的东西(但又不同于前缀和) 状态:f[i ...
- Dhroid框架笔记(DhNet、Adapter)
3.1.1 DhNet用于获取网络中的数据 DhNet net=new DhNet("路劲"); net.addParam("key", "参数&qu ...
- asp.net中@page 指令的属性Inherits、Src、CodeBehind区别
在 ASP.NET 中使用代码隐藏方法来设计Web 窗体,可使页代码能够更清晰地从 HTML 内容中分离到完全单独的文件中. <%@ Page language="c#" C ...
- 关于T-SQL重编译那点事,内联函数和表值函数在编译生成执行计划的区别
本文出处:http://www.cnblogs.com/wy123/p/6266724.html 最近在学习 WITH RECOMPILE和OPTION(RECOMPILE)在重编译上的区别的时候,无 ...
- 3.题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
public static void main(String[] args) { Scanner scanner=new Scanner(System.in); ...
- Android Studio删除Project
最直截了当的方法是在Project所在目录直接删除整个Project,然后再次打开Android Studio的欢迎界面中把光标移到你的Project上,然后按键盘上的Delete键即可! 参考自st ...