美文网首页
HBase过滤器[RowFilter]之正则,子串包含

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

作者: 拥抱月亮的大星星 | 来源:发表于2018-08-06 11:53 被阅读251次

    数据源见

    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
    

    相关文章

      网友评论

          本文标题:HBase过滤器[RowFilter]之正则,子串包含

          本文链接:https://www.haomeiwen.com/subject/uxkuvftx.html