Hbase Filter+Scan 查询效率优化

Hbase Filter+Scan 查询效率问题

众所周知,Hbase利用filter过滤器查询时候会进行全表扫描,查询效率低下,如果没有二级索引,在项目中很多情况需要利用filter,下面针对这种情况尝试了几种优化的方案,仅供参考,欢迎交流。

根据业务要求,作者需要根据时间范围搜索所需要的数据,所以作者设计的rowKey是以时间戳为起始字符串的。

正确尝试:
1.scan 设置 开始行和结束行

            Scan scan = new Scan();
            scan.setStartRow("startRowKey".getBytes());
            scan.setStopRow("stopRowKey".getBytes());

结论:查询效率明显提升

2.查询的数量越小越好
总数据量越大,查询越耗时,所以为保证效率,开始行和结束行之间需要遍历的数据总量越少越好。

需要说明的是,在filter 中RowFilter设置开始行(前缀)和结束行(前缀)
并不能提升查询效率,因为还是全表扫描。

        FilterList filters = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        //开头大于等于starTm的行
      
         Filter starTime = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,
                   new BinaryPrefixComparator(DateUtil.formatDate(starTime, "yyyyMMddHHmmssSSS").getBytes()));
         filters.addFilter(starTime);
      

        //开头小于等于endTime的行
        
          Filter endTime = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
                    new BinaryPrefixComparator(DateUtil.formatDate(endTime, "yyyyMMddHHmmssSSS").getBytes()));
          filters.addFilter(endTime);
       

结论:利用san+filter方式查询hbase时,一定要设置starRow 和stopRow

猜你喜欢

转载自blog.csdn.net/u013465194/article/details/83044910