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‘开头的行。

转载于:https://www.cnblogs.com/zhaobingqing/p/8243347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值