gpt4 book ai didi

java - hbase 扫描仪 .next() 返回 null

转载 作者:行者123 更新时间:2023-11-30 07:51:28 27 4
gpt4 key购买 nike

我正在扫描 hbase 中的 md5 字符串,如果找到字符串,我必须执行一些操作。问题是,如果我第一次检查它,它会返回 true (并且在 log 处是值),但是当我在 if 或 for 语句中使用相同的表达式时,我得到了错误的结果。

        Scan scanCookiesData = new Scan();

scanCookiesData.addColumn(Bytes.toBytes("md5data"), Bytes.toBytes("md5data"));
ResultScanner scannerCookiesData = dbHelper.getTable("userInfo").getScanner(scanCookiesData);
if (scannerCookiesData.next() != null) {
response.getWriter().println("if (scannerCookiesData.next() != null)");

for (Result r = scannerCookiesData.next(); r != null; r = scannerCookiesData.next()) {
response.getWriter().println("INSIDE FOR!!!");

if (md5data.equals(Bytes.toString(r.value()))) {
// Here we will assign ID to the user.
response.getWriter().println(assignID(Bytes.toString(r.value())));
response.setStatus(200);
} else {
response.getWriter().println("ELSE");
}
}
} else {
response.getWriter().println(createNewUser(json));
response.setStatus(200);
}

在这段代码中,首先如果它得到“true”,但是在 for 中没有被调用,因为它会显示“false”..但是扫描仪不是空(在 db 中是那个 md5 字符串),所以它应该得到“真实”..

最佳答案

我现在能想到的最好的理由是只为您的查询找到 1 个结果。错误在于您对调用 next() 的误解:该调用返回下一个值(如果存在)并向前移动一步,没有办法继续返回

通过调用 if (scannerCookiesData.next() != null) 您实际上检索到该结果,并跳到下一个结果(如果存在)。这意味着您从未真正使用过第一个结果,除了空检查之外,因为它没有存储在任何地方。当您到达 for 循环时,您将开始处理第二个结果,我认为您的情况中不存在该结果。

当扫描仪没有找到更多结果时,将返回 null 而不是 Result 对象,表示结果集结束。

关于java - hbase 扫描仪 .next() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33297631/

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