HBase Filter 过滤器之 ValueFilter 详解[通俗易懂]

HBase Filter 过滤器之 ValueFilter 详解[通俗易懂]前言: 本文详细介绍了 HBase ValueFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考。ValueFilter 基于列值进行过滤,在工作中涉及到

HBase Filter 过滤器之 ValueFilter 详解

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

一。Java Api

头部代码

/**
 * 用于列值过滤。
 */
public class ValueFilterDemo {
    private static boolean isok = false;
    private static String tableName = "test";
    private static String[] cfs = new String[]{"f1","f2"};
    private static String[] data = new String[]{
            "row-1:f1:c1:abcdefg", 
			"row-2:f1:c2:abc", 
			"row-3:f2:c3:abc123456", 
			"row-4:f2:c4:1234abc567"
    };
    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();

代码100分

中部代码

向右滑动滚动条可查看输出结果。

1. BinaryComparator 构造过滤器

代码100分        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("abc"))); // [row-2:f1:c2:abc]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("abc"))); // [row-1:f1:c1:abcdefg, row-3:f2:c3:abc123456, row-4:f2:c4:1234abc567]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("abc"))); // [row-1:f1:c1:abcdefg, row-3:f2:c3:abc123456]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("abc1"))); // [row-1:f1:c1:abcdefg, row-3:f2:c3:abc123456]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("abc"))); // [row-4:f2:c4:1234abc567]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("abc"))); // [row-2:f1:c2:abc, row-4:f2:c4:1234abc567]

2. BinaryPrefixComparator 构造过滤器

        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("123"))); // [row-4:f2:c4:1234abc567]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("ab"))); // [row-4:f2:c4:1234abc567]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.GREATER, new BinaryPrefixComparator(Bytes.toBytes("ab"))); // [] 只比较prefix长度的字节
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("ab"))); // [row-1:f1:c1:abcdefg, row-2:f1:c2:abc, row-3:f2:c3:abc123456]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.LESS, new BinaryPrefixComparator(Bytes.toBytes("abc"))); // [row-4:f2:c4:1234abc567]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("abc"))); // [row-1:f1:c1:abcdefg, row-2:f1:c2:abc, row-3:f2:c3:abc123456, row-4:f2:c4:1234abc567]

3. SubstringComparator 构造过滤器

代码100分        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("123")); // [row-3:f2:c3:abc123456, row-4:f2:c4:1234abc567]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("def")); // [row-2:f1:c2:abc, row-3:f2:c3:abc123456, row-4:f2:c4:1234abc567]```

4. RegexStringComparator 构造过滤器

        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.NOT_EQUAL, new RegexStringComparator("4[a-z]")); // [row-1:f1:c1:abcdefg, row-2:f1:c2:abc, row-3:f2:c3:abc123456]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("4[a-z]")); // [row-4:f2:c4:1234abc567]
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("abc")); // [row-1:f1:c1:abcdefg, row-2:f1:c2:abc, row-3:f2:c3:abc123456, row-4:f2:c4:1234abc567]

尾部代码

		scan.setFilter(valueFilter);
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        LinkedList<String> keys = new LinkedList<>();
        while (iterator.hasNext()) {
            String key = "";
            Result result = iterator.next();
            for (Cell cell : result.rawCells()) {
                byte[] rowkey = CellUtil.cloneRow(cell);
                byte[] family = CellUtil.cloneFamily(cell);
                byte[] column = CellUtil.cloneQualifier(cell);
                byte[] value = CellUtil.cloneValue(cell);
                key = Bytes.toString(rowkey) + ":" + Bytes.toString(family) + ":" + Bytes.toString(column) + ":" + Bytes.toString(value);
                keys.add(key);
            }
        }
        System.out.println(keys);
        scanner.close();
        table.close();
        connection.close();
    }
}

二。Shell Api

1. BinaryComparator 构造过滤器

方式一:

hbase(main):006:0> scan "test",{FILTER=>"ValueFilter(=,"binary:abc")"}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-2                                           column=f1:c2, timestamp=1589453592471, value=abc                                                                                              
1 row(s) in 0.0240 seconds

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

方式二:

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

hbase(main):010:0> scan "test",{FILTER => ValueFilter.new(CompareFilter::CompareOp.valueOf("EQUAL"), BinaryComparator.new(Bytes.toBytes("abc")))}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-2                                           column=f1:c2, timestamp=1589453592471, value=abc                                                                                              
1 row(s) in 0.0230 seconds

支持的比较运算符:LESSLESS_OR_EQUALEQUALNOT_EQUALGREATERGREATER_OR_EQUAL,不再一一举例。

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

2. BinaryPrefixComparator 构造过滤器

方式一:

hbase(main):011:0> scan "test",{FILTER=>"ValueFilter(=,"binaryprefix:ab")"}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-1                                           column=f1:c1, timestamp=1589453592471, value=abcdefg                                                                                          
 row-2                                           column=f1:c2, timestamp=1589453592471, value=abc                                                                                              
 row-3                                           column=f2:c3, timestamp=1589453592471, value=abc123456                                                                                        
3 row(s) in 0.0430 seconds

方式二:

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

hbase(main):013:0> scan "test",{FILTER => ValueFilter.new(CompareFilter::CompareOp.valueOf("EQUAL"), BinaryPrefixComparator.new(Bytes.toBytes("ab")))}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-1                                           column=f1:c1, timestamp=1589453592471, value=abcdefg                                                                                          
 row-2                                           column=f1:c2, timestamp=1589453592471, value=abc                                                                                              
 row-3                                           column=f2:c3, timestamp=1589453592471, value=abc123456                                                                                        
3 row(s) in 0.0440 seconds

其它同上。

3. SubstringComparator 构造过滤器

方式一:

hbase(main):014:0> scan "test",{FILTER=>"ValueFilter(=,"substring:123")"}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-3                                           column=f2:c3, timestamp=1589453592471, value=abc123456                                                                                        
 row-4                                           column=f2:c4, timestamp=1589453592471, value=1234abc567                                                                                       
2 row(s) in 0.0340 seconds

方式二:

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

hbase(main):016:0> scan "test",{FILTER => ValueFilter.new(CompareFilter::CompareOp.valueOf("EQUAL"), SubstringComparator.new("123"))}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-3                                           column=f2:c3, timestamp=1589453592471, value=abc123456                                                                                        
 row-4                                           column=f2:c4, timestamp=1589453592471, value=1234abc567                                                                                       
2 row(s) in 0.0240 seconds

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

4. RegexStringComparator 构造过滤器

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

hbase(main):018:0> scan "test",{FILTER => ValueFilter.new(CompareFilter::CompareOp.valueOf("EQUAL"), RegexStringComparator.new("4[a-z]"))}
ROW                                              COLUMN+CELL                                                                                                                                   
 row-4                                           column=f2:c4, timestamp=1589453592471, value=1234abc567                                                                                       
1 row(s) in 0.0290 seconds

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

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

ValueFilter 不支持使用 LongComparator 比较器,且 BitComparatorNullComparator 比较器用之甚少,也不再介绍。

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8355.html

(0)
上一篇 2023-03-01
下一篇 2023-03-01

相关推荐

  • PostgreSQL 13 new feature

    PostgreSQL 13 new featurehttps://www.postgresql.org/docs/13/release-13.html 1. A big performance leap from 10 to 11. No much…

    2023-04-01
    187
  • Python中int的用法

    Python中int的用法int是一种Python中的数据类型,代表整数。在Python中,int类型的数据是不可变的,即无法更改它们的值。

    2024-08-04
    27
  • Oracle重命名sys或system操作记录[通俗易懂]

    Oracle重命名sys或system操作记录[通俗易懂]oracle等保中有条要求:应重命名或删除默认账户,修改默认账户的默认口令。给的建议是修改sys,system默认账户名称,避免使用常见用户名称;以下记录了两种方法来实现上面的要求:Oracle重命…

    2023-04-05
    161
  • Python编程异常:’return’出现在函数外部的解决方案

    Python编程异常:’return’出现在函数外部的解决方案在Python编程过程中,我们在编写函数时常常会遇到一个问题,即“return”出现在函数外部的异常。该异常是由于程序的控制流程异常所引起,导致程序无法正常执行。

    2023-12-31
    136
  • oracle体系结构简介[通俗易懂]

    oracle体系结构简介[通俗易懂]oracle体系结构简介 一、物理存储结构 1、数据文件 存放数据库数据,以dbf为扩展名。将数据放在多个数据文件中, 再将数据文件分放在不同的硬盘中,可以提高存取速度。数据文 件由数据块构成,块大小

    2022-12-20
    136
  • oracle配置环境变量tns_协议分析

    oracle配置环境变量tns_协议分析本系列重点分析TNS 314下的客户端与服务端之间的通讯,通过抓包分析,查看在不同客户端,不同服务端情况下传输方式的不同,尝试还原其协议细节,实现对协议中一些关键内容的解析,如登录用户名,协议版本,o

    2023-02-26
    163
  • 提高工作效率的Python工具库

    提高工作效率的Python工具库数据处理是Python的一个强项,对于数据分析和处理,Python提供了许多好用的工具库。例如,Pandas库可以帮助我们方便地读取数据、进行数据清洗、转换和分析等。此外,Python还可以通过其他库来进行数据自动化处理,例如:

    2024-03-15
    78
  • TDSQL-A与CK的对比[通俗易懂]

    TDSQL-A与CK的对比[通俗易懂]CK介绍 CK是目前社区里面比较热门的,应用场景也比较广泛。 首先,在架构上,集群内划分为多个分片,通过分片的线性扩展能力,支持海量数据的分布式存储计算,每个分片内包含一定数量的节点Node,即进程,

    2023-04-21
    157

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注