HBase自定义过滤器

HBase自定义过滤器

由于HBase内置的过滤器的过滤规则不是特别严谨(在HBase-0.90之前时),所以使用hbase的自定义过滤器。

package com.hbase;

import java.io.DataInput;

import java.io.DataOutput;

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.HTable;

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.Filter;

import org.apache.hadoop.hbase.filter.FilterBase;

import org.apache.hadoop.hbase.filter.FilterList;

import org.apache.hadoop.hbase.util.Bytes;

//自定义过滤器

public class CustomerFilter extends FilterBase {

// 加载hbase的配置

private static Configuration config = null;

private static HTable table = null;

static {

config = HBaseConfiguration.create();

try {

table = new HTable(config, "testtable");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private byte[] value = null;

private boolean filterRow = true;

public CustomerFilter() {

super();

}

// 设置要比较的值

public CustomerFilter(byte[] value) {

this.value = value;

}

//

@SuppressWarnings("deprecation")

public ReturnCode filterKeyValue(KeyValue kv) {

if (Bytes.compareTo(value, kv.getValue()) == 0) {

// 当新行匹配设置值时让其通过过滤器

filterRow = false;

}

// 先包含keyvalue的实例,直到filterRow()决定是否过滤这一行。

return ReturnCode.INCLUDE;

}

// 基于标志位判断从而决定数据是否被返回

@Override

public boolean filterRow() {

return filterRow;

}

// 每当有新行时重置过滤器的标志位

@Override

public void reset() throws IOException {

this.filterRow = true;

}

// 把设定值写入DataOutput中,在服务器实例化过滤器时可以读到设定值

public void write(DataOutput dataOutput) {

try {

Bytes.writeByteArray(dataOutput, this.value);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 服务器使用这个方法来初始化过滤器实例,所以客服端的设定值可以被读到

public void readFields(DataInput dataIntput) {

try {

this.value = Bytes.readByteArray(dataIntput);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(String[] args) {

// 使用集合添加过滤规则

List<Filter> filters = new ArrayList<Filter>();

// 添加过滤条件

Filter filter1 = new CustomerFilter(Bytes.toBytes("val1"));

filters.add(filter1);

Filter filter2 = new CustomerFilter(Bytes.toBytes("val2"));

filters.add(filter2);

Filter filter3 = new CustomerFilter(Bytes.toBytes("val3"));

filters.add(filter3);

// 过滤器

// 1、FilterList代表一个过滤器列表

// FilterList.Operator.MUST_PASS_ALL -->and

// FilterList.Operator.MUST_PASS_ONE -->or

// eg、FilterList list = new

// FilterList(FilterList.Operator.MUST_PASS_ONE);

// 2、SingleColumnValueFilter

// 3、ColumnPrefixFilter用于指定列名前缀值相等

// 4、MultipleColumnPrefixFilter和ColumnPrefixFilter行为差不多,但可以指定多个前缀。

// 5、QualifierFilter是基于列名的过滤器。

// 6、RowFilter

// 7、RegexStringComparator是支持正则表达式的比较器。

// 8、SubstringComparator用于检测一个子串是否存在于值中,大小写不敏感。

Filter filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE, filters);

// 使用扫描器配合过滤器进行过滤

Scan scan = new Scan();

scan.setFilter(filterList);

ResultScanner scanner = null;

try {

scanner = table.getScanner(scan);

for (Result result : scanner) {

for (KeyValue kv : result.raw()) {

System.out.println("KV:" + kv + ",Value:" + Bytes.toString(kv.getValue()));

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

table.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值