Command like SQL LIMIT in HBase

前端 未结 5 1059
暗喜
暗喜 2021-01-31 13:55

Does HBase have any command that works like SQL LIMIT query?

I can do it by setStart and setEnd, but I do not want to iterate all

5条回答
  •  感情败类
    2021-01-31 14:36

    A guaranteed way is to do the limiting on the client side, inside the iterator loop. This is the approach taken in the HBase Ruby Shell. From table.rb ($HBASE_HOME/hbase-shell/src/main/ruby/hbase/table.rb): Line 467:

      # Start the scanner
      scanner = @table.getScanner(_hash_to_scan(args))
      iter = scanner.iterator
    
      # Iterate results
      while iter.hasNext
        if limit > 0 && count >= limit
          break
        end
    
        row = iter.next
        ...
     end
    

    It can be made a bit more efficient by adding scan.setFilter(new PageFilter(limit)) and scan.setCaching(limit), and then table.getScanner(scan). The page filter will ensure that each region server will return at most limit rows, the scan caching limit will ensure that each region server will read ahead and cache at most 'limit' rows, and then the client loop limit checking can break the loop after getting the first 'limit' rows in the order received by the client.

提交回复
热议问题