hbase - scan - filter 示例

public static void main(String[] args) throws IOException {
        //Scan类常用方法说明
        //指定需要的family或column ,如果没有调用任何addFamily或Column,会返回所有的columns; 
        // scan.addFamily(); 
        // scan.addColumn();
        // scan.setMaxVersions(); //指定最大的版本个数。如果不带任何参数调用setMaxVersions,表示取所有的版本。如果不掉用setMaxVersions,只会取到最新的版本.
        // scan.setTimeRange(); //指定最大的时间戳和最小的时间戳,只有在此范围内的cell才能被获取.
        // scan.setTimeStamp(); //指定时间戳
        // scan.setFilter(); //指定Filter来过滤掉不需要的信息
        // scan.setStartRow(); //指定开始的行。如果不调用,则从表头开始;
        // scan.setStopRow(); //指定结束的行(不含此行);
        // scan.setBatch(); //指定最多返回的Cell数目。用于防止一行中有过多的数据,导致OutofMemory错误。
        
        //过滤器
        //1、FilterList代表一个过滤器列表
        //FilterList.Operator.MUST_PASS_ALL -->and
        //FilterList.Operator.MUST_PASS_ONE -->or
        //eg、FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        //2、SingleColumnValueFilter
        //3、ColumnPrefixFilter用于指定列名前缀值相等
        //4、MultipleColumnPrefixFilter和ColumnPrefixFilter行为差不多,但可以指定多个前缀。
        //5、QualifierFilter是基于列名的过滤器。
        //6、RowFilter
        //7、RegexStringComparator是支持正则表达式的比较器。
        //8、SubstringComparator用于检测一个子串是否存在于值中,大小写不敏感。
        
        HTable table=(HTable) getHTablePool().getTable("tb_stu");
        Scan scan=new Scan();
        scan.setMaxVersions();
        //指定最多返回的Cell数目。用于防止一行中有过多的数据,导致OutofMemory错误。
        scan.setBatch(1000);

        //scan.setTimeStamp(NumberUtils.toLong("1370336286283"));
        //scan.setTimeRange(NumberUtils.toLong("1370336286283"), NumberUtils.toLong("1370336337163"));
        //scan.setStartRow(Bytes.toBytes("quanzhou"));
        //scan.setStopRow(Bytes.toBytes("xiamen"));
        //scan.addFamily(Bytes.toBytes("info")); 
        //scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("id"));
        
        //查询列镞为info,列id值为1的记录
        //方法一(单个查询)
        // Filter filter = new SingleColumnValueFilter(
        //         Bytes.toBytes("info"), Bytes.toBytes("id"), CompareOp.EQUAL, Bytes.toBytes("1")); 
        // scan.setFilter(filter);
        
        //方法二(组合查询)
        //FilterList filterList=new FilterList();
        //Filter filter = new SingleColumnValueFilter(
        //    Bytes.toBytes("info"), Bytes.toBytes("id"), CompareOp.EQUAL, Bytes.toBytes("1"));
        //filterList.addFilter(filter);
        //scan.setFilter(filterList);
        
        ResultScanner rs = table.getScanner(scan);
        
        for (Result r : rs) {
            for (KeyValue kv : r.raw()) {
                System.out.println(String.format("row:%s, family:%s, qualifier:%s, qualifiervalue:%s, timestamp:%s.", 
                        Bytes.toString(kv.getRow()), 
                        Bytes.toString(kv.getFamily()), 
                        Bytes.toString(kv.getQualifier()), 
                        Bytes.toString(kv.getValue()),
                        kv.getTimestamp()));
            }
        }
        
        rs.close();
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HBase中的Scan操作默认情况下是按照行键升序排列的,如果您想要将结果倒序排列,可以使用ReverseFilter来实现。 ReverseFilter是一个HBase中的过滤器(Filter)类型,它可以反转Scan操作的结果顺序。以下是一个示例代码片段,演示如何在Scan操作中使用ReverseFilter来实现结果倒序排列: ``` import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.ReverseFilter; import org.apache.hadoop.hbase.util.Bytes; ... Table table = connection.getTable(TableName.valueOf("mytable")); Scan scan = new Scan(); scan.setReversed(true); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); filterList.addFilter(new ReverseFilter()); scan.setFilter(filterList); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { // process results } scanner.close(); table.close(); ``` 在此示例中,我们首先创建了一个名为“mytable”的表,并设置了一个Scan操作。然后,我们创建了一个FilterList对象,并向其中添加了一个ReverseFilter对象,以实现结果倒序排列。最后,我们使用Table对象的getScanner方法来获取一个ResultScanner对象,并遍历ResultScanner对象来处理扫描结果。 需要注意的是,在上面的示例代码中,我们在Scan对象中使用了setReversed(true)方法来启用倒序扫描。同时,我们还将ReverseFilter对象添加到了FilterList对象中。由于FilterList对象中的过滤器(Filter)将按照添加的顺序逐一作用于结果集,因此必须将ReverseFilter添加到FilterList的最前面,以确保结果顺序正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值