使用javaapi的hbase长比较过滤器

ffx8fchx  于 2021-06-08  发布在  Hbase
关注(0)|答案(1)|浏览(391)

我无法比较hbase表的某个列值中的长值。我正在使用javaapi。下面是代码片段。我清楚地看到表中有一个值满足过滤器的要求。
我也想知道什么是词典比较,我将如何执行长期比较。
这方面的任何指导都非常有用。
提前谢谢

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();
    }
toe95027

toe950271#

根据doc,您必须提供自己的比较器,它将反序列化字节数组中的长值。据我所知,默认比较是按词典编纂的。关于词典比较,你会发现这篇文章很有用。这篇文章提供了如何编写相应的比较器的线索。
更新:

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);
    }
}

我建议使用hbase utils而不是guava,因为它可能与hbase guava依赖性冲突。

相关问题