鸡西百姓网:HBase Filter {过滤器之}FamilyFilter详解

2020-10-03 49 views 2

扫一扫用手机浏览

‘前言’:本文详细先容了 HBase FamilyFilter 「过滤器」 Java&Shell API 的使用,{并贴出了相关示例代码以供参考}。FamilyFilter <基于列族举行过滤>,〖在〗事情中涉及到需要通过HBase 『列族举行数据过滤时可以思量使用它』。【对照器】细节及原理请参照之前的更文:HBase Filter 「过滤器」之【对照器】 Comparator 〖原理及源码学习〗

一。Java Api

头部代码

public class FamilyFilterDemo  {

    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:v1", "row-2:f1:c2:v2", "row-3:f2:c3:v3", "row-4:f2: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 组织「过滤器」

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-3, row-4]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("f1"))); // [row-3, row-4]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2, row-3, row-4]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("f2"))); // [row-1, row-2]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2]

2. BinaryPrefixComparator 组织「过滤器」

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-3, row-4]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("f1"))); // [row-3, row-4]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2, row-3, row-4]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("f2"))); // [row-1, row-2]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2]

3. SubstringComparator 组织「过滤器」

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("1")); // [row-1, row-2]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("f")); // []

4. RegexStringComparator 组织「过滤器」

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.NOT_EQUAL, new RegexStringComparator("f")); // []
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("f")); // [row-1, row-2, row-3, row-4]
        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("2")); // [row-3, row-4]

尾部代码

        scan.setFilter(familyFilter);
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        LinkedList<String> 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):002:0> scan 'test',{FILTER=>"FamilyFilter(=,'binary:f1')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1588834369334, value=v1
 row-2                                           column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.1000 seconds

支持的对照运算符:= != > >= < <=,(不再逐一举例)。

方式二:

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

hbase(main):006:0> scan 'test',{FILTER => FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryComparator.new(Bytes.toBytes('f1')))}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1588834369334, value=v1
 row-2                                           column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0350 seconds

支持的对照运算符:LESS、LESS_OR_EQUAL、EQUAL、NOT_EQUAL、GREATER、GREATER_OR_EQUAL,(不再逐一举例)。

<推荐>使用《方》式一,更简练利便。

2. BinaryPrefixComparator 组织「过滤器」

《方》式一:

hbase(main):007:0> scan 'test',{FILTER=>"FamilyFilter(=,'binaryprefix:f1')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1588834369334, value=v1
 row-2                                           column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0600 seconds

方式二:

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

hbase(main):011:0>  scan 'test',{FILTER => FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryPrefixComparator.new(Bytes.toBytes('f1')))}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1588834369334, value=v1
 row-2                                           column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0290 seconds

其它同上。

3. SubstringComparator 组织「过滤器」

《方》式一:

hbase(main):012:0> scan 'test',{FILTER=>"FamilyFilter(=,'substring:f1')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1588834369334, value=v1
 row-2                                           column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0400 seconds

方式二:

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

hbase(main):016:0> scan 'test',{FILTER => FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), SubstringComparator.new('f1'))}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1588834369334, value=v1
 row-2                                           column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0330 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.FamilyFilter

hbase(main):018:0> scan 'test',{FILTER => FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), RegexStringComparator.new('f'))}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1588834369334, value=v1
 row-2                                           column=f1:c2, timestamp=1588834369334, value=v2
 row-3                                           column=f2:c3, timestamp=1588834369334, value=v3
 row-4                                           column=f2:c4, timestamp=1588834369334, value=v4
4 row(s) in 0.0600 seconds

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

注重这里的正则匹配指包罗关系,“对应底层”find()方式。

FamilyFilter 不支持使用LongComparator【对照器】,且BitComparator、NullComparator 【对照器】用之甚少,“也不再先容”。

查看文章所有源代码请访以下GitHub地址:

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

‘转载请注明出处’!(迎接关注本人微信民众号)【HBase事情条记】

,

Sunbet

Sunbet www.99ruxian.com女性健康网,<免费提供女性保健常识>、{女性饮食}、女性疾病、女性心理、女性情感、{女性用品}、女性孕育等女性健康知识。

Sunbet网站内容转载自互联网,如有侵权,联系Sunbet 删除。

本文链接地址:http://www.shfkgcjxyxgs.com/post/1414.html

相关文章

发表评论

  • 欧博会员开户欢迎进入欧博会员开户(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。跟别的文不一样

    2020-10-03 00:13:20 回复该评论
  • 欧博会员开户欢迎进入欧博会员开户(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。跟别的文不一样

    2020-10-03 00:13:26 回复该评论