文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

HBase Filter 过滤器之RowFilter详解

2017-12-16 13:45

关注

HBase Filter 过滤器之RowFilter详解

前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考。RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase Rowkey进行数据过滤时可以考虑使用它。比较器细节及原理请参照之前的更文:HBase Filter 过滤器之比较器 Comparator 原理及源码学习

一。Java Api

头部代码

public class RowFilterDemo {

    private static boolean isok = false;
    private static String tableName = "test";
    private static String[] cfs = new String[]{"f"};
    private static String[] data = new String[]{"row-ac:f:c1:v1", "row-ab:f:c2:v2", "row-bc:f:c3:v3", "row-abc:f:c4:v4"};

    public static void main(String[] args) throws IOException {

        MyBase myBase = new MyBase();
        Connection connection = myBase.createConnection();
        if (isok) {
            myBase.deleteTable(connection, tableName);
            myBase.createTable(connection, tableName, cfs);
            myBase.putRows(connection, tableName, data); // 造数据
        }
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();

中部代码
向右滑动滚动条可查看输出结果。

1. BinaryComparator 构造过滤器

        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ac]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc, row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ac, row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("row-ac"))); // [row-ab, row-abc, row-ac]

2. BinaryPrefixComparator 构造过滤器

        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac, row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // []
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("row-a"))); // [row-ab, row-abc, row-ac]

3. SubstringComparator 构造过滤器

        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("ab")); // [row-ab, row-abc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("ab")); // [row-ac, row-bc]

4. RegexStringComparator 构造过滤器

        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new RegexStringComparator("abc")); // [row-ab, row-ac, row-bc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("abc")); // [row-abc]
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("a")); // [row-ab, row-abc, row-ac]

5. NullComparator 构造过滤器

        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new NullComparator()); // []
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new NullComparator()); // [row-ab, row-abc, row-ac, row-bc]

尾部代码

        scan.setFilter(rowFilter);
        ResultScanner scanner = table.getScanner(scan);
        Iterator iterator = scanner.iterator();
        LinkedList rowkeys = new LinkedList<>();
        while (iterator.hasNext()) {
            Result result = iterator.next();
            String rowkey = Bytes.toString(result.getRow());
            rowkeys.add(rowkey);
        }
        System.out.println(rowkeys);
        scanner.close();
        table.close();
        connection.close();
    }
}

二。Shell Api

1. BinaryComparator 构造过滤器

方式一:

hbase(main):006:0> scan "test",{FILTER=>"RowFilter(=,"binary:row-ab")"}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-ab                                          column=f:c2, timestamp=1588156704669, value=v2                                                                                                
1 row(s) in 0.0140 seconds

支持的比较运算符:= != > >= < <=,不再一一举例。

方式二:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.BinaryComparator
import org.apache.hadoop.hbase.filter.RowFilter

hbase(main):016:0> scan "test",{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf("EQUAL"), BinaryComparator.new(Bytes.toBytes("row-ab")))}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-ab                                          column=f:c2, timestamp=1588156704669, value=v2                                                                                                
1 row(s) in 0.0310 seconds

支持的比较运算符:LESS、LESS_OR_EQUAL、EQUAL、NOT_EQUAL、GREATER、GREATER_OR_EQUAL,不再一一举例。

推荐使用方式一,更简洁方便。

2. BinaryPrefixComparator 构造过滤器

方式一:

hbase(main):023:0> scan "test",{FILTER=>"RowFilter(=,"binaryprefix:row-ab")"}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-ab                                          column=f:c2, timestamp=1588156704669, value=v2                                                                                                
 row-abc                                         column=f:c4, timestamp=1588156704669, value=v4                                                                                                
2 row(s) in 0.0360 seconds

方式二:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator
import org.apache.hadoop.hbase.filter.RowFilter

hbase(main):027:0> scan "test",{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf("EQUAL"), BinaryPrefixComparator.new(Bytes.toBytes("row-ab")))}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-ab                                          column=f:c2, timestamp=1588156704669, value=v2                                                                                                
 row-abc                                         column=f:c4, timestamp=1588156704669, value=v4                                                                                                
2 row(s) in 0.0110 seconds

其它同上。

3. SubstringComparator 构造过滤器

方式一:

hbase(main):001:0> scan "test",{FILTER=>"RowFilter(=,"substring:row-ab")"}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-ab                                          column=f:c2, timestamp=1588156704669, value=v2                                                                                                
 row-abc                                         column=f:c4, timestamp=1588156704669, value=v4                                                                                                
2 row(s) in 0.3200 seconds

方式二:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.SubstringComparator
import org.apache.hadoop.hbase.filter.RowFilter

hbase(main):007:0> scan "test",{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf("EQUAL"), SubstringComparator.new("row-ab"))}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-ab                                          column=f:c2, timestamp=1588156704669, value=v2                                                                                                
 row-abc                                         column=f:c4, timestamp=1588156704669, value=v4                                                                                                
2 row(s) in 0.0230 seconds

区别于上的是这里直接传入字符串进行比较,且只支持EQUAL和NOT_EQUAL两种比较符。

4. RegexStringComparator 构造过滤器

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.RegexStringComparator
import org.apache.hadoop.hbase.filter.RowFilter

hbase(main):007:0> scan "test",{FILTER => RowFilter.new(CompareFilter::CompareOp.valueOf("EQUAL"), RegexStringComparator.new("row-ab"))}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-ab                                          column=f:c2, timestamp=1588156704669, value=v2                                                                                                
 row-abc                                         column=f:c4, timestamp=1588156704669, value=v4                                                                                                
2 row(s) in 0.0230 seconds

该比较器直接传入字符串进行比较,且只支持EQUAL和NOT_EQUAL两种比较符。若想使用第一种方式可以传入regexstring试一下,我的版本有点低暂时不支持,不再演示了。

注意这里的正则匹配指包含关系,对应底层find()方法。

此外,RowFilter 不支持使用LongComparator比较器,且BitComparator、NullComparator 比较器用之甚少,也不再介绍。

查看文章全部源代码请访以下GitHub地址:

https://github.com/zhoupengbo/demos-bigdata/blob/master/hbase/hbase-filters-demos/src/main/java/com/zpb/demos/RowFilterDemo.java

扫描二维码关注博主公众号

转载请注明出处!欢迎关注本人微信公众号【HBase工作笔记】

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯