一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”
摘要: 大家在使用HBase和Solr搭建系统中经常遇到的一个问题就是:“我通过SOLR得到了RowKeys后,该怎样去HBase上取数据”。使用现有的Filter性能差劲,网上也没有现成的自定义Filter解决方案,我在这里把这个问题的解决办法分享给大家,抛砖引玉一下。先讲一下,虽然使用自定义过滤器来达到取数据的目的,但它其实并不是一个好的解决办法,因为它的性能是有问题的,具体分析还要看我的博客HBase 高性能获取数据 - 多线程批量式解决办法:http://www.cnblogs.com/wgp13x/p/4245182.html
Solr和HBase专辑
1、“关于Solr的使用总结的心得体会”(http://www.cnblogs.com/wgp13x/p/3742653.html)
2、“中文分词器性能比较”(http://www.cnblogs.com/wgp13x/p/3748764.html)
3、“Solr与HBase架构设计”(http://www.cnblogs.com/wgp13x/p/a8bb8ccd469c96917652201007ad3c50.html)
4、 “大数据架构: 使用HBase和Solr将存储与索引放在不同的机器上”(http://www.cnblogs.com/wgp13x/p/3927979.html)
5、“一个自定义 HBase Filter -通过RowKeys来高性能获取数据”(http://www.cnblogs.com/wgp13x/p/4196466.html)
|
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.filter.FilterBase;
import org.apache.hadoop.hbase.util.Bytes;
/**
* @description 自定义过滤器,用来读取大量离散行
* @author 王安琪
* @time 2014年11月8日上午10:47:17
* @className RowKeyFilter
*/
public class RowKeyFilter extends FilterBase
{
private byte[] value = null;
private boolean filterRow = true;
/**
* map中存放需要读的行RowKey
*/
public Map<Object, Object> map = new HashMap<Object, Object>();
public RowKeyFilter()
{
super();
}
public RowKeyFilter(byte[] value)
{
this.value = value;
}
@Override
public ReturnCode filterKeyValue(KeyValue ignored)
{
if (this.filterRow == false)
return ReturnCode.INCLUDE;
else
return ReturnCode.NEXT_ROW;
}
/**
* 行过滤,查询该行RowKey是否在Map中
*/
@Override
public boolean filterRowKey(byte[] buffer, int offset, int length)
{
byte[] rowKey = Arrays.copyOfRange(buffer, offset, offset + length);
String str = new String(rowKey);
if (map.containsKey(str))
{ // 在0(1)时间内返回,效率较高
this.filterRow = false; // false表示包括这一行
}
return this.filterRow;
}
@Override
public void reset()
{
this.filterRow = true;
}
@Override
public boolean filterRow()
{
return filterRow;
}
/**
* 将Map中的数据以Byte[]形式传给服务器
*/
@Override
public void write(DataOutput dataOutput) throws IOException
{
Bytes.writeByteArray(dataOutput, this.value);
}
/**
* 服务器读取Byte[]数据,再将数据存储到Map中 不同的RowKey以","分割
*/
@Override
public void readFields(DataInput dataInput) throws IOException
{
this.value = Bytes.readByteArray(dataInput);
String string = new String(this.value);
String[] strs = string.split(",");
for (String str : strs)
{
map.put(str, str);
}
}
} |
|
/**
* 根据rowKeys获取数据
*
* @param rowKeys:每个rowkey之间使用逗号分隔符
* @param filterColumn:表示过滤的列,如果为空表示所有列的数据都返回
* @param isContiansRowkeys:设置为true,表示返回结果集中包含rowkeys;否则返回结果集中不包含rowkeys
* @return
*/
/* @Override */
public Datas getDatasFromHbase(String rowKeys, List<String> filterColumn,
boolean isContiansRowkeys)
{
Datas datas = new Datas();
HTableInterface hTableInterface = getTable(tableName);
Scan scan = new Scan();
if (filterColumn != null)
{
for (String column : filterColumn)
{
scan.addColumn(columnFamilyName.getBytes(), column.getBytes());
}
}
if (rowKeys != null && rowKeys.length() > 0)
{
RowKeyFilter rowKeyFilter = new RowKeyFilter(rowKeys.getBytes());
scan.setFilter(rowKeyFilter);
}
ResultScanner resultScanner = null;
List<Data> listData = new ArrayList<Data>();
try
{
resultScanner = hTableInterface.getScanner(scan);
for (Result result : resultScanner)
{
Data data = new Data();
if (isContiansRowkeys)
{
data.setRowkey(new String(result.getRow()));
}
Map<String, String> map = new HashMap<String, String>();
List<String> content = new ArrayList<String>();
String[] temp = null;
if (filterColumn != null)
{
temp = new String[filterColumn.size()];
}
for (KeyValue keyValue : result.raw())
{
if (filterColumn == null)
{
content.add(new String(keyValue.getValue()));
}
else if (filterColumn != null)
{
String qualifier = new String(keyValue.getQualifier());
String value = new String(keyValue.getValue());
if (filterColumn.contains(qualifier))
{
int index = filterColumn.indexOf(qualifier);
temp[index] = value;
}
}
}
if (temp != null)
{
for (int i = 0; i < temp.length; i++)
{
content.add(temp[i]);
}
}
data.setContent(content);
listData.add(data);
}
datas.setDatas(listData);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
resultScanner.close();
try
{
hTableInterface.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return datas;
} |

一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”的更多相关文章
- HBase 高性能获取数据(多线程批量式解决办法) + MySQL和HBase性能测试比较
摘要: 在前篇博客里已经讲述了通过一个自定义 HBase Filter来获取数据的办法,在末尾指出此办法的性能是不能满足应用要求的,很显然对于如此成熟的HBase来说,高性能获取数据应该不是问题. ...
- Hbase之尝试使用错误列族获取数据
import com.google.common.base.Strings; import org.apache.hadoop.conf.Configuration; import org.apach ...
- HBase 高性能加入数据 - 按批多“粮仓”式解决办法
摘要:如何从HBase中的海量数据中,以很快的速度的获取大批量数据,这一议题已经在<HBase 高性能获取数据>(http://www.cnblogs.com/wgp13x/p/42451 ...
- hbase高性能读取数据
有时需要从hbase中一次读取大量的数据,同时对实时性有较高的要求.可以从两方面进行考虑:1.hbase提供的get方法提供了批量获取数据方法,通过组装一个list<Get> gets即可 ...
- 从SQLite获取数据完成一个产品信息展示
在ios实际开发当中,我们常常用到Core Data做为数据储存首选.但在处理一些大量复杂的数据值且数据之间相互关联的时候,这就不得不使用关系型数据库来实现.例如一个导航程序,自身应该包含大量的地图自 ...
- HBase - Filter - 过滤器的介绍以及使用
1 过滤器HBase 的基本 API,包括增.删.改.查等.增.删都是相对简单的操作,与传统的 RDBMS 相比,这里的查询操作略显苍白,只能根据特性的行键进行查询(Get)或者根据行键的范围来查询( ...
- HBase Filter 过滤器之 Comparator 原理及源码学习
前言:上篇文章HBase Filter 过滤器概述对HBase过滤器的组成及其家谱进行简单介绍,本篇文章主要对HBase过滤器之比较器作一个补充介绍,也算是HBase Filter学习的必备低阶魂技吧 ...
- asp.net MVC之 自定义过滤器(Filter) - shuaixf
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...
- HBase filter shell操作
创建表 create 'test1', 'lf', 'sf' lf: column family of LONG values (binary value) -- sf: column family ...
随机推荐
- MVC中在一个视图中,怎么加载另外一个视图?
在RazorView.cshtml视图: <!--在视图中调用无返回值的方法,视图中调用无返回值的方法,要加上大括号--> <!--在一个视图中,直接加载另外一个视图--> @ ...
- ACM中的浮点数精度处理
在ACM中,精度问题非常常见.其中计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模板一般就不成问题了.精度问题则不好说,有时候一个精度问题就可能成为一道题的瓶颈,让你debu ...
- 【C#】线程之Task
Task开启线程 有两种启动方式: 1.构造创建线程,然后启动 var taskForAction = new Task(() => { //do something }); taskForAc ...
- Python基础:序列
一.概述 序列类型(Sequence Types)类似于C中的数组,它是包含一组成员的连续列表,这些成员紧邻排列,并且可以通过序号(下标偏移量)访问其中的一个或多个成员.序列类型的示意图如下所示: P ...
- 根据Expander的IsExpanded属性值的变化动态设计Control的size
简要说明: 当Expander 的IsExpanded属性为“True” 时给控件设个尺寸(此处为高度),当为“False”时给控件设另外一个值. 知识点:数据绑定.Style和Trigger < ...
- ActiveReports 报表应用教程 (1)---Hello ActiveReports
在开始专题内容之前,我们还是了解一下 ActiveReports 是一款什么产品:ActiveReports是一款在全球范围内应用非常广泛的报表控件,以提供.NET报表所需的全部报表设计功能领先于同类 ...
- 回文串---Hotaru's problem
HDU 5371 Description Hotaru Ichijou recently is addicated to math problems. Now she is playing wit ...
- AC自动机---Keywords Search
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...
- git 给远程库 添加多个url地址
目录[-] 前提 使用流程 原理解析 注意 Other 参考文章 作者:shede333主页:http://my.oschina.net/shede333 && http://blo ...
- No.011:Container With Most Water
问题: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...