HBase第三章 过滤器

1  列值过滤器

  SingleColumnValueFilter 对列值进行过滤。

    @Test
    public void scanDataByFilter() throws IOException {
        Table table = connection.getTable(TableName.valueOf("user"));
        Scan scan = new Scan();
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info1"),
                Bytes.toBytes("name"), CompareOp.GREATER, Bytes.toBytes("lisi"));
        scan.setFilter(singleColumnValueFilter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            byte[] name = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
            byte[] sex = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("sex"));
            byte[] age = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"));
            byte[] address = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("address"));
            System.out.println("name=" + Bytes.toString(name) + ",sex=" + Bytes.toInt(sex) + ",age=" + Bytes.toInt(age)
                    + ",address=" + Bytes.toString(address));

        }
    }

   扫描全表,用过滤器进行匹配,找出出满足过滤条件的元素。

  SingleColumnValueFilter

  参数:列族、列名、操作符、列值

  操作符可以为:

  CompareOp.LESS:小于

  CompareOp.LESS_OR_EQUAL:小于或者等于

  CompareOp.EQUAL:等于

  CompareOp.NOT_EQUAL:不等于

  CompareOp.GREATER_OR_EQUAL:大于或者等于

  CompareOp.GREATER:大于

  CompareOp.NO_OP:不比较

2 列名前缀过滤器

  ColumnPrefixFilter 对列名进行过滤

@Test
    public void scanDataByFilter2() throws IOException {
        Table table = connection.getTable(TableName.valueOf("user"));
        Scan scan = new Scan();
        ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes("name_"));
        scan.setFilter(columnPrefixFilter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            
            Cell[] rawCells = result.rawCells();
            for (Cell cell : rawCells) {
                System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("family = " + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("qualifier = " + Bytes.toString(CellUtil.cloneQualifier(cell)));
            }
        }
    }

 

  找出user表中,以'name_'开头的列

3 多个列值前缀过滤器

    @Test
    public void testMultipleColumnPrefixFilter() throws IOException {
        Table table = connection.getTable(TableName.valueOf("user"));
        Scan scan = new Scan();
        byte[][] prefixes = new byte[][] { Bytes.toBytes("name"), Bytes.toBytes("age") };
        MultipleColumnPrefixFilter multipleColumnPrefixFilter = new MultipleColumnPrefixFilter(prefixes);
        scan.setFilter(multipleColumnPrefixFilter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {

            Cell[] rawCells = result.rawCells();
            for (Cell cell : rawCells) {
                System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("family = " + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("qualifier = " + Bytes.toString(CellUtil.cloneQualifier(cell)));
            }
        }
    }

  用于匹配多列,找出以‘name’和‘age’开头的列

4 rowKey过滤器  

@Test
    public void testRowFilter() throws IOException {
        Table table = connection.getTable(TableName.valueOf("user"));
        Scan scan = new Scan();
        RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^00004"));
        scan.setFilter(rowFilter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {

            Cell[] rawCells = result.rawCells();
            for (Cell cell : rawCells) {
                System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("family = " + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("qualifier = " + Bytes.toString(CellUtil.cloneQualifier(cell)));
            }
        }
    }

 

  RegexStringComparator("^00004")正则计较器,支持正则表达式。过滤rowkey是以‘’00004‘开头的行。

posted @ 2018-01-08 16:25  IT-執念  阅读(1038)  评论(0编辑  收藏  举报