Simple integer comparison in HBase

2019-06-14 12:30发布

问题:

I am trying out a very simple example in HBase. Following is how I create table and put data:

create 'newdb3','data'
put 'newdb3','row1','data:name','Thexxx Beatles'
put 'newdb3','row2','data:name','The Beatles'
put 'newdb3','row3','data:name','Beatles'
put 'newdb3','row4','data:name','Thexxx'
put 'newdb3','row1','data:duration',400
put 'newdb3','row2','data:duration',300
put 'newdb3','row3','data:duration',200
put 'newdb3','row4','data:duration',100

scan 'newdb3', {COLUMNS => 'data:name', FILTER => "SingleColumnValueFilter('data','duration', > ,'binaryprefix:200')"}

But the result is always all 4 columns. I tried number with or without string, and using hex values. I also tried 'binary' instead of 'binaryprefix'. How do I store and compare integer in hbase?

回答1:

Does this produce the expected output?

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter
import org.apache.hadoop.hbase.filter.SubstringComparator
import org.apache.hadoop.hbase.util.Bytes

scan 'newdb3', { FILTER => SingleColumnValueFilter.new(Bytes.toBytes('data'), \
    Bytes.toBytes('duration'), 
    CompareFilter::CompareOp.valueOf('GREATER'), \
    BinaryComparator.new(Bytes.toBytes('200'))) }

NOTE: This will do a binary comparison and for numbers it will work only if they are 0-padded