Hbase 学习(二)各种filter

news/2024/5/18 22:40:38

各种filter

今天的主题是 Filter,hbase客户端查询的时候,自定义查询filter。
直接上例子吧,不多说别的了,第一个例子是RowFilter的。
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("colfam1"),
Bytes.toBytes("col-0"));
Filter filter1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
                                               new BinaryComparator(Bytes.toBytes("row-22")));
scan.setFilter(filter1);
ResultScanner scanner1 = table.getScanner(scan);
for (Result res : scanner1) {
    System.out.println(res);
}
scanner1.close();

Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,
                                              new RegexStringComparator(".*-.5"));
scan.setFilter(filter2);
ResultScanner scanner2 = table.getScanner(scan);
for (Result res : scanner2) {
     System.out.println(res);
}
scanner2.close();

Filter filter3 = new RowFilter(CompareFilter.CompareOp.EQUAL,
                                               new SubstringComparator("-5"));
scan.setFilter(filter3);
ResultScanner scanner3 = table.getScanner(scan);
for (Result res : scanner3) {
      System.out.println(res);
}
scanner3.close();
第二个例子是 QualifierFilter
Filter filter = new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
                                                  new BinaryComparator(Bytes.toBytes("col-2")));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
     System.out.println(result);
}
scanner.close();
Get get = new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result = table.get(get);
第三个例子是 ValueFilter
Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL,
                                                new SubstringComparator(".4"));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
       for (KeyValue kv : result.raw()) {
             System.out.println("KV: " + kv + ", Value: " +
             Bytes.toString(kv.getValue()));
       }
}
scanner.close();
Get get = new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result = table.get(get);
for (KeyValue kv : result.raw()) {
     System.out.println("KV: " + kv + ", Value: " +
     Bytes.toString(kv.getValue()));
}
第四个例子是 PageFilter,分页的filter
Filter filter = new PageFilter(15);
int totalRows = 0;
byte[] lastRow = null;
while (true) {
    Scan scan = new Scan();
    scan.setFilter(filter);
    if (lastRow != null) {
         byte[] startRow = Bytes.add(lastRow, POSTFIX);
        System.out.println("start row: " +Bytes.toStringBinary(startRow));
         scan.setStartRow(startRow);
    }
    ResultScanner scanner = table.getScanner(scan);  
    int localRows = 0;
    Result result;
    while ((result = scanner.next()) != null) {
        System.out.println(localRows++ + ": " + result);
         totalRows++;
         lastRow = result.getRow();
   }
   scanner.close();
   if (localRows == 0) break;
}
System.out.println("total rows: " + totalRows);
第五个例子是 InclusiveStopFilter,它是什么意思呢,就是扫描到某个rowkey就停止,下面的例子是从row-3开始扫描,一直扫描到row-5结束。
Filter filter = new
InclusiveStopFilter(Bytes.toBytes("row-5"));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("row-3"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
    System.out.println(result);
}
scanner.close();
第六个 SingleColumnValueFilter,这个是我们最常用的filter,我觉得很好用。
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("colfam1"),
Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL,
new SubstringComparator("val-5"));
filter.setFilterIfMissing(true);
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
  for (KeyValue kv : result.raw()) {
     System.out.println("KV: " + kv + ", Value: " +
     Bytes.toString(kv.getValue()));
  }
}
scanner.close();
Get get = new Get(Bytes.toBytes("row-6"));
get.setFilter(filter);
Result result = table.get(get);
System.out.println("Result of get: ");
for (KeyValue kv : result.raw()) {
     System.out.println("KV: " + kv + ", Value: " +
     Bytes.toString(kv.getValue()));
}
第七个 FilterList,它有两个选项,需要全部通过,还是一个通过就行,MUST_PASS_ALL,MUST_PASS_ONE。
public class CustomFilter extends FilterBase {
    private byte[] value = null;
    private boolean filterRow = true;

    public CustomFilter() {
        super();
    }

    public CustomFilter(byte[] value) {
        this.value = value;
    }

    @Override
    public void reset() {
        this.filterRow = true;
    }

    @Override
    public ReturnCode filterKeyValue(KeyValue kv) {
        if (Bytes.compareTo(value, kv.getValue()) == 0) {

            filterRow = false;
        }
        return ReturnCode.INCLUDE;
    }

    @Override
    public boolean filterRow() {
        return filterRow;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        Bytes.writeByteArray(dataOutput, this.value);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.value = Bytes.readByteArray(dataInput);
    }
}
这里要讲的是 自定义filter,从FilterBase继承。
public class CustomFilter extends FilterBase {
    private byte[] value = null;
    private boolean filterRow = true;

    public CustomFilter() {
        super();
    }

    public CustomFilter(byte[] value) {
        this.value = value;
    }

    @Override
    public void reset() {
        this.filterRow = true;
    }

    @Override
    public ReturnCode filterKeyValue(KeyValue kv) {
        if (Bytes.compareTo(value, kv.getValue()) == 0) {

            filterRow = false;
        }
        return ReturnCode.INCLUDE;
    }

    @Override
    public boolean filterRow() {
        return filterRow;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        Bytes.writeByteArray(dataOutput, this.value);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.value = Bytes.readByteArray(dataInput);
    }
}
然后打成jar包,要在hbase-env.sh中指明路径。
export  HBASE_CLASSPATH="/hbase/target/hbase-customfilter.jar",然后就可以在客户端中使用它了。

http://www.niftyadmin.cn/n/2210915.html

相关文章

什么是mysql事物定义_数据库事务的概念及其实现原理

以下所有内容都是针对单机事务而言,不涉及分布式事务相关的东西!关于事务原理的讲解不针对具体的某个数据库实现,所以某些地方可能和你的实践经验不符。1. 认识事务1.1 为什么需要数据库事务转账是生活中常见的操作,比如从A账户转账100元到B账号。站在用户角度而言,这是一个逻辑…

aes key iv从mysql,如何安全地处理AES“Key”和“IV”值

If I use AES (System.Security.Cryptography) to simply encrypt and decrypt blob or memo fields in a SQL server, then where do I store the “Key” and “IV” values on the server? (File, Regkey, Dbase,...)And what with the protection of those AES “Key” an…

qmake工具生成项目文件与Makefile文件

qmake 是一个协助简化跨平台进行专案开发的构建过程的工具程式,也是Qt附带工具之一。可以根据项目环境构建.pro项目文件,再根据项目文件生成Makefile文件。相对于手写一个项目Makefile来说,使用qmake生成Makefile比较简便。 查看qmake Ubu…

谈谈对Canal(增量数据订阅与消费)的理解

概述 canal是阿里巴巴旗下的一款开源项目,纯Java开发。基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了mysql(也支持mariaDB)。 起源:早期,阿里巴巴B2B公司因为存在杭州和美国…

【noi 2.6_666】放苹果 【noi 2.6_8467】鸣人的影分身(DP)

这题其实在2.6前面的专题也有出现过,我还以为我有写,结果发现,并没有。于是就现在写了。这2题其实重复了......我就按放苹果的来说。 题意:把N个苹果放在M个盘子里,允许有的盘子空着不放,问共有多少种不同的…

php 随机在文章中添加锚文本_原创文章SEO技巧——嘉定行吟科技

一、网站内容关键词的选定是根据自动分词许多SEO优化人员,对于网站页面内容的选择大多都是在关键词标签中出现的,但实际上这是一种错误的方法,其中一些只是优化人员的主观看法,没有从搜索引擎的规则上考虑,如果优化人员…

mysql中的(null)_mysql中的NULL

[mysql - rootlocalhosttestmysql.sock 23:44:48] >select * from a;------------ --------------a表中包含a,c,无NULL| a | c |------------| 2 | 1 || 3 | 1 || 1 | 2 |------------3 rows in set (0.00 sec)[mysql - rootlocalhosttestmysql.sock …

SMTP基本电子邮件发送协议原理

1.电子邮件发送客户端发送给服务器端,postfix软件提供MTA,MDA.MTA 提供SMTP服务,接受邮件(读取DNS的MX记录)--》保存MDA 传送邮件MUA 用户代理 1-1服务器使用SMTP协议将电子邮件提交至TCP端口25,或由本地客户端通过 /usr/bin/sendmail程序进行提交。如…