gpt4 book ai didi

java - 如何在HBase Java中读取Null值

转载 作者:行者123 更新时间:2023-12-02 21:50:01 26 4
gpt4 key购买 nike

我试图从HBase表中检索Long值。但是它在检索特定列族时给出了NullPointerException。

示例代码段:

long total_size=0;
total_size +=Bytes.toLong(rr.getValue(Bytes.toBytes("size"),Bytes.toBytes("size"))));

我已存储大小值。一些尺寸值是空的。我想检索空值并将它们分配为“0”零。

请给我适当的代码段。

谢谢。

最佳答案

这是因为Bytes.toLong()方法不检查检索到的byte[]是否为null并调用其byte[].length,这将引发您的异常。您可以尝试以下解决方法:

String longValue = Bytes.toString(rr.getValue(Bytes.toBytes("size"),Bytes.toBytes("size")));
total_size += (longValue != null ? Bytes.toLong(rr.getValue(Bytes.toBytes("size"),Bytes.toBytes("size")):0l);

要么
try{
total_size +=Bytes.toLong(rr.getValue(Bytes.toBytes("size"),Bytes.toBytes("size"))));
}catch(NullPointerException e){
// no need to increment total_size by 0
}

关于java - 如何在HBase Java中读取Null值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21857655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com