本文介绍了如何在hbase中的原子计数器上使用过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 incr 来增加table中的计数器列。现在我需要过滤 scan 使计数器小于某个值(例如1)。 get 显示hbase shell中特定行键的计数器值(它是4),如下所示:

  column = q:counter,timestamp = 1419403701427,value = \x00\x00\x00\x00\x00 \x00\x00\x04 

现在如果使用扫描过滤器过滤从扫描中排除此记录如下

  {FILTER => SingleColumnValueFilter('q','counter',<,'binary:1',true,true),COLUMNS => ['q:counter']} 

以上指定记录的计数器值4仍然显示在扫描结果中。
我试过 true false 作为 SingleColumnValueFilter



我需要以字节格式指定1吗?

解决方案对于HBase,列值只是一个 byte [] ,因此使用HBase Shell进行查询有点棘手,因为标准的过滤语言将会将所有内容转换为字符串:



为了使它适用于整数/ longs,我们不得不认为它好像我们会用JAVA做的那样:将值转换为 byte [] ,并用CompareOp将它提供给SingleColumnValueFilter。


  1. 首先,您必须将所需的库导入到HBase Shell中

     import jav a.lang.Long 
    import org.apache.hadoop.hbase.util.Bytes
    import org.apache.hadoop.hbase.filter.SingleColumnValueFilter
    import org.apache.hadoop.hbase。 filter.CompareFilter
    import org.apache.hadoop.hbase.filter.BinaryComparator


  2. 查找具有值的行小于20L的BINARY值 \x00\x00\x00\x00\x00\x00\x00\x14

    扫描your_table,{LIMIT => 10,FILTER => SingleColumnValueFilter.new(Bytes.toBytes('Family'),Bytes.toBytes('Column'),CompareFilter :: 
  3. 对于整数列,解决方案更容易一些(不需要长解析)。要查找值小于20的BINARY值的行 \x00\x00\x00\x14

    扫描your_table,{LIMIT => 10,FILTER => SingleColumnValueFilter.new(Bytes.toBytes('Family'),Bytes.toBytes('Column'),CompareFilter :: CompareOp.valueOf('LESS '),BinaryComparator.new(Bytes.toBytes(20)))} 


有效比较运营商:


  • EQUAL

  • GREATER

  • GREATER_OR_EQUAL

  • LESS

  • LESS_OR_EQUAL

  • NO_OP

  • NOT_EQUAL


I'm using incr to increment a counter column in table.Now I need to filter certain records in scan so that counter is less than some value(say 1).get show the counter value(which is 4) for a particular row-key in hbase shell as below:

column=q:counter, timestamp=1419403701427, value=\x00\x00\x00\x00\x00\x00\x00\x04 

Now if use scan filter to filter exclude this record from scan as below

{FILTER=> "SingleColumnValueFilter('q','counter',<,'binary:1',true,true)" , COLUMNS=>['q:counter'] }

Above specify record with counter value 4 is still shown in scan result.I have tried true,false as third parameter of SingleColumnValueFilter.

Do I need to specify 1 in byte format or so?

解决方案

For HBase the column value is just a byte[] so it's a bit tricky to make the query with the HBase Shell because the standard filter language will convert everything to strings:http://www.cloudera.com/content/cloudera/en/documentation/core/latest/topics/admin_hbase_filtering.html

To make it work with integers/longs we have to think of it as if we would do with JAVA: convert the value to a byte[] and feed it to a SingleColumnValueFilter with a CompareOp.

  1. First you have to import the required libraries into HBase Shell

    import java.lang.Long
    import org.apache.hadoop.hbase.util.Bytes
    import org.apache.hadoop.hbase.filter.SingleColumnValueFilter
    import org.apache.hadoop.hbase.filter.CompareFilter
    import org.apache.hadoop.hbase.filter.BinaryComparator
    

  2. To find rows with a value LESS than BINARY value of 20L \x00\x00\x00\x00\x00\x00\x00\x14

    scan "your_table", {LIMIT => 10, FILTER => SingleColumnValueFilter.new(Bytes.toBytes('Family'), Bytes.toBytes('Column'), CompareFilter::CompareOp.valueOf('LESS'), BinaryComparator.new(Bytes.toBytes(Long.parseLong("20"))))}

  3. For integer columns the solution is a little easier (no Long parsing required). To find rows with a value LESS than BINARY value of 20 \x00\x00\x00\x14

    scan "your_table", {LIMIT => 10, FILTER => SingleColumnValueFilter.new(Bytes.toBytes('Family'), Bytes.toBytes('Column'), CompareFilter::CompareOp.valueOf('LESS'), BinaryComparator.new(Bytes.toBytes(20)))}

Valid compare operators:

  • EQUAL
  • GREATER
  • GREATER_OR_EQUAL
  • LESS
  • LESS_OR_EQUAL
  • NO_OP
  • NOT_EQUAL

这篇关于如何在hbase中的原子计数器上使用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 01:24