設計了rowKey,查詢列表卻沒有返回rowKey這個字段?
因公司業務的需要,返回列表數據還需要做詳情的查詢,那首選當然是根據rowKey去查,性能好呀!一頓操作猛如虎,列表使用scan加rowKey模糊匹配掃描只返回固定條數的數據,但是拿到整行數據卻沒有看到rowKey?不科學,咋可能不返回呢?搜了下,相關資料比較少,某歌還是比較強大的,來,看代碼:
Scan scan = new Scan();
FilterList filterList = new FilterList();
filterList.addFilter(new PageFilter(pageSize));
if (lastRowKey != null) {
scan.setStartRow(Bytes.toBytes(lastRowKey));
}
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
List<Map<String, String>> list = new ArrayList<>();
for (Result result : scanner) {
Map<String, String> obj = new HashMap<>();
//rowKey在這裏,Bytes.toString(result.getRow())
obj.put("id", Bytes.toString(result.getRow()));
for (Cell cell : result.listCells()) {
obj.put(Bytes.toString(CellUtil.cloneQualifier(cell)),
Bytes.toString(CellUtil.cloneValue(cell)));
}
list.add(obj);
}
scanner.close();
return list;
上述代碼中result.listCells()是整行的所有列,api裏面的設計沒有把rowKey當成一個列,而是單獨提供了getRow()方法來獲取,我這裏用的hbase-client-1.2.0.jar,這個版本里面的Cell.getRow()方法已經過時了,所以推薦時用result對象的getRow()方法來獲取該行的rowKey。
注:對hbase基本操作還不太瞭解的朋友可以參考我上一篇文章:springboot集成hbase