• spark实现从hbase中批量查询多个rowKey


    需求:参数是多个没有顺序的rowKey,在某张表中批量查询。一个一个rowKey查询的话,效率太低。

    实现:需要在scan中添加filter。filter中添加多个rowKey,对需要查询的rowKey进行限制。x代表rowKey。

     val rowKeyFilter=new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(x)))
    

      RowFilter用于过滤row key

    Operator Description
    LESS 小于
    LESS_OR_EQUAL 小于等于
    EQUAL 等于
    NOT_EQUAL 不等于
    GREATER_OR_EQUAL 大于等于
    GREATER 大于
    NO_OP 排除所有
    Comparator Description
    BinaryComparator 使用Bytes.compareTo()比较
    BinaryPrefixComparator 和BinaryComparator差不多,从前面开始比较
    NullComparator Does not compare against an actual value but whether a given one is null, or not  null.
    BitComparator Performs a bitwise comparison, providing a BitwiseOp class with OR, and XOR operators.
    RegexStringComparator 正则表达式
    SubstringComparator 把数据当成字符串,用contains()来判断

    在这里使用EQUAL,BinaryComparator。

    多个rowKey,rowKeyList是个rowKey的集合

    //存放rowKeyFilter的filter
    var filters = new util.ArrayList[Filter]
    rowKeyList.foreach(x=>{
          val rowKeyFilter=new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(x)))
          filters.add(rowKeyFilter)
        })
    

      然后将filters添加到 FilterList中

        val filterList: FilterList = new FilterList(FilterList.Operator.MUST_PASS_ONE,filters)

    scan.setFilter(filterList)
        FilterList.Operator.MUST_PASS_ALL --> 取交集 相当一and操作
        FilterList.Operator.MUST_PASS_ONE --> 取并集 相当于or 操作

     

  • 相关阅读:
    gitio博客搭建,hexo + NeXT
    [MIsc]JD笔试编程题
    [MATH]Big Integer +
    【Math】GCD XOR 证明
    【Math】最近点对
    【SRM】600#div2 B 枚举
    【Game】组合游戏
    【Game】找出游戏必胜态
    【DP】树形DP 记忆化搜索
    141. Linked List Cycle
  • 原文地址:https://www.cnblogs.com/xuesheng/p/9633751.html
Copyright © 2020-2023  润新知