HBase过滤器[RowFilter]之正则,子串包含

数据源见

1.正则过滤器

//过滤器练习
    @Test
    public void rowFilterTest(){



        String[] qualifiers = new String[]{"size"};

          //row 行过滤器
        //rowFilter("FileTable","fileInfo",qualifiers,"rowkey3",CompareOperator.LESS_OR_EQUAL);

        //正则过滤器

        //正则表达式,匹配以0结尾的
        String regex  = ".*0$";

        rowFilterRegex("FileTable","fileInfo",qualifiers,regex,CompareOperator.EQUAL);

    }

    //基于行键过滤器
    public void rowFilterRegex(String tableName,String cfName,String[] qualifiers,String regex,CompareOperator operator){


        try(Table table = HBaseConn.getTable(tableName)){

            Scan scan = new Scan();


            //为了输出特定列族特定列
            for(String name:qualifiers){

                scan.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(name));
            }

            Filter filter = new RowFilter(operator,new RegexStringComparator(regex));

            scan.setCaching(1000);

            scan.setFilter(filter);

            ResultScanner scanner = table.getScanner(scan);

            //输出打印
            resultlog(scanner);

            scanner.close();



        }catch (IOException e){
            e.printStackTrace();
        }



    }


    //打印过滤
    public void resultlog(ResultScanner scanner){
        scanner.forEach(result -> {

            if (result != null){
                System.out.println("rowkey="+Bytes.toString(result.getRow()));
                System.out.println("fileName="+Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"),Bytes.toBytes("name"))));
                System.out.println("szie="+Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"),Bytes.toBytes("size"))));
            }

        });
    }

结果:

rowkey=rowkey0
fileName=null
szie=1014
rowkey=rowkey10
fileName=null
szie=1876

2.子串包含

 //子串包含
        rowFilterSubString("FileTable","fileInfo",qualifiers,"1",CompareOperator.EQUAL);
//基于行键过滤器
    public void rowFilterSubString(String tableName,String cfName,String[] qualifiers,String substr,CompareOperator operator){


        try(Table table = HBaseConn.getTable(tableName)){

            Scan scan = new Scan();

            for(String name:qualifiers){

                scan.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(name));
            }

            Filter filter = new RowFilter(operator,new SubstringComparator(substr));

            scan.setCaching(1000);

            scan.setFilter(filter);

            ResultScanner scanner = table.getScanner(scan);


            resultlog(scanner);

            scanner.close();



        }catch (IOException e){
            e.printStackTrace();
        }



    }

log:

rowkey=rowkey1
fileName=null
szie=1024
rowkey=rowkey10
fileName=null
szie=1876
    原文作者:拥抱月亮的大星星
    原文地址: https://www.jianshu.com/p/2db8f6875664
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞