0

I am unable to compare a Long value in one of the column values of my HBase table. I am using java api. Below is the code snippet.I clearly have a value in the table which satisfies the filter.

I would also like to know what is lexicographical comparison and how would I perform long comparison.

Any direction on this greatly helpful.

Thanks in advance

FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
SingleColumnValueFilter fil = new SingleColumnValueFilter(CF1_BYTE, VALUE_BYTE, CompareOp.LESS,new BinaryComparator(Bytes.toBytes(50)));
 Scan scan = new Scan();
 scan.addColumn(Bytes.toBytes("C1"),Bytes.toBytes("VALUE"));
    list.addFilter(fil);
    scan.setFilter(list);
    try {
        ResultScanner scanner = table.getScanner(scan);

        for(Result r : scanner){
            String VAL = Bytes.toString(r.getValue(Bytes.toBytes("C1"),Bytes.toBytes("VALUE")));
            count ++;
            if(count == 1000){
                break;
            }
            System.out.println(count +"  "+Bytes.toString(r.getRow()) + "        "+VAL );
        }
        scanner.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        table.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

1 Answers1

0

According to doc, you have to provide your own comparator which will deserialize Long values from Byte arrays. As I know default comparison is lexicographical. Regarding lexicographical comparison, you can find this post useful. And this post provide a clue how you can write the corresponding comparator.

Update:

import java.util.Comparator
import org.apache.hadoop.hbase.util.Bytes 

class LongComparator implements Comparator<byte[]> {
    public int compare(byte[] left, byte[] right) {
        long leftLong = Bytes.toLong(left);
        long rightLong = Bytes.toLong(right);
        return Long.compare(leftLong, rightLong);
    }
}

I suggest using HBase utils instead of guava since it may conflict with HBase guava dependencies.

gorros
  • 1,411
  • 1
  • 18
  • 29