过滤器的种类:
列植过滤器—SingleColumnValueFilter
过滤列植的相等、不等、范围等
列名前缀过滤器—ColumnPrefixFilter
过滤指定前缀的列名
多个列名前缀过滤器—MultipleColumnPrefixFilter
过滤多个指定前缀的列名
rowKey过滤器—RowFilter
通过正则,过滤rowKey值。
package com.ycit.hbase.test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
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.client.Table;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
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.util.Bytes;
import org.jruby.compiler.ir.operands.Operand;
import org.junit.Before;
import org.junit.Test;
/**
* @author 江鹏飞 Hbase的几种过滤器
*/
public class HbaseFilter {
/**
* 全表扫描的过滤器 列值过滤器
*/
static Configuration config = null;
private Connection connection = null;
private Table table = null;
// 配置连接信息
@Before
public void init() throws Exception {
// 创建连接
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "mini1,mini2,mini3");// zookeeper地址
config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("user"));
}
/**
* 列值过滤器
*
* @throws IOException
*/
@Test
public void scanDataByFilterClo() throws IOException {
// 创建全表扫描
Scan scan = new Scan();
// 过滤器:列值过滤器
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info1"), Bytes.toBytes("age"),
CompareOp.EQUAL, Bytes.toBytes("21"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("airen"))));
}
}
/**
* RowKey过滤器
*
* @throws IOException
*/
@Test
public void scanDataByFilterRowKey() throws IOException {
// 创建全表扫描
Scan scan = new Scan();
// 过滤器:RowKey 过滤器
RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^w"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("airen"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("Address"))));
}
}
/**
* 匹配列名前缀过滤器
*
* @throws IOException
*/
@Test
public void scanDataByFilterPrefix() throws IOException {
// 创建全表扫描
Scan scan = new Scan();
// 过滤器:PrefixFilter 过滤器
ColumnPrefixFilter PrefixFilter = new ColumnPrefixFilter(Bytes.toBytes("a"));
scan.setFilter(PrefixFilter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("airen"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("Address"))));
System.out.println("============================");
}
}
/**
* 过滤器集合
*
* @throws IOException
*/
@Test
public void scanDataByFilterList() throws IOException {
Scan scan = new Scan();
// 定义过滤器集合
FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);// Operator.MUST_PASS_ALL
// 相当于逻辑关系中的
// 且
// 过滤器:RowKey 过滤器 //Operator.MUST_PASS_ONE 相当于逻辑关系中的 或
RowFilter filterRow = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^jiang"));
// 过滤器:列值过滤器
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info1"), Bytes.toBytes("age"),
CompareOp.EQUAL, Bytes.toBytes("21"));
filterList.addFilter(filter);
filterList.addFilter(filterRow);
// 为scan添加过滤器
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("airen"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("Address"))));
System.out.println("============================");
}
}
}