简体   繁体   English

使用Java Api的Hbase Long比较过滤器

[英]Hbase Long Comparison Filter using Java Api

I am unable to compare a Long value in one of the column values of my HBase table. 我无法在我的HBase表的列值之一中比较Long值。 I am using java api. 我正在使用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();
    }

According to doc , you have to provide your own comparator which will deserialize Long values from Byte arrays. 根据doc ,您必须提供自己的比较器,该比较器将从Byte数组中反序列化Long值。 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. 我建议使用HBase utils代替guava,因为它可能与HBase guava依赖项冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM