一、Hbase过滤器的介绍
HBase为筛选数据提供了一组过滤器,通过这个过滤器可以在HBase中的数据的多个维度(行,列,数据版本)上进行对数据的筛选操作,也就是说过滤器最终能够筛选的数据能够细化到具体的一个存储单元格上(由行键,列明,时间戳定位)。
以下为RowFilter行键过滤器、ColumnFilter 列过滤器以及FilterList 过滤列表部分代码显示。
二、Hbase过滤器代码
2.1 RowFilter行键过滤器
public class EduRowFilter {
public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.1.149");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn=HConnectionManager.createConnection(conf);
HTableInterface table=conn.getTable("study2");
Scan scan=new Scan();
scan.setCacheBlocks(false);
//设置filter
Filter filter=new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("abc"))); //对值过滤
Filter filter=new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator(Bytes.toBytes("ab"))); //对前缀过滤
Filter filter=new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringCompatator("b")); //包含b进行过滤
scan.setfilter(filter);
ResultScanner scanner=table.getScanner(scan);
for(Result result : scanner){
List<Cell> cells=result.listcells();for(Cell cell : cells){
String rowkey =new String(CellUtil.cloneRow(cell));
String column=new String(CellUtil.cloneQualifier(cell));
String family =new String(CellUtil.cloneFamily(cell));
String value =new String(CellUtil.cloneValue(cell));
System.out.println(rowkey+" "+family+" "+column+" "+value);
}
}
scanner.close();
table.close();
}
}
2.2 ColumnFilter 列过滤器
public class EduColumnFilter {
public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.1.149");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn=HConnectionManager.createConnection(conf);
HTableInterface table=conn.getTable("study2");
Scan scan=new Scan();
scan.setCacheBlocks(false);//设置filter
Filter filter=new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("abc"))); //对值过滤
scan.setfilter(filter);
ResultScanner scanner=table.getScanner(scan);
for(Result result : scanner){
List<Cell> cells=result.listcells;
for(Cell cell : cells){
String rowkey =new String(CellUtil.cloneRow(cell));
String column=new String(CellUtil.cloneQualifier(cell));
String family =new String(CellUtil.cloneFamily(cell));
String value =new String(CellUtil.cloneValue(cell));
System.out.println(rowkey+" "+family+" "+column+" "+value);
}}scanner.close();table.close();}}
2.3 FilterList 过滤列表(多个过滤器一起起作用)
public class EduFilterList {
public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.1.149");
conf.set("hbase.zookeeper.property.clientPort","2181");
HConnection conn=HConnectionManager.createConnection(conf);
HTableInterface table=conn.getTable("study2");
Scan scan=new Scan();
scan.setCacheBlocks(false);
//设置filter1和filter2
SingleColumnValueFilter filter1=new SingleColumnValueFilter(
Bytes.toBytes("d"),
Bytes.toBytes("b"),
CompareFilter.CompareOp.EQUAL,
new BinaryPrefixComparator(Bytes.toBytes("abc")));
filter1.setFilterIfMissing(true);
SingleColumnValueFilter filter2=new SingleColumnValueFilter(
Bytes.toBytes("d"),
Bytes.toBytes("c"),
CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("1|2|3"));
filter2.setFilterIfMissing(true);
List<Filter> filters=new ArrayList<Filter>();
filters.add(filter1);
filters.add(filter2);
filterList filterlist=new FilterList(filters); //默认多个过滤是“与”的关系
ResultScanner scanner=table.getScanner(scan);
for(Result result : scanner){
List<Cell> cells =result.listcells();
for(Cell cell : cells){
String rowkey =new String(CellUtil.cloneRow(cell));
String column=new String(CellUtil.cloneQualifier(cell));
String family =new String(CellUtil.cloneFamily(cell));
String value =new String(CellUtil.cloneValue(cell));
System.out.println(rowkey+" "+family+" "+column+" "+value);
}
}
scanner.close();
table.close();
conn.close();
}
}