我有 spark streaming 工作,在这个过程中有些人正在做一些聚合,现在我想将该记录插入到 HBase 但它不是典型的插入我想做 UPSERT 如果 rowkey 可用而不是列值 sum(newvalue+oldvalue ) 应该发生。 有人在 java 中共享伪代码吗?我该如何实现?
最佳答案
像这样的……
byte[] rowKey = null; // Provided
Table table = null; // Provided
long newValue = 1000; // Provided
byte[] FAMILY = new byte[]{0}; // Defined
byte[] QUALIFIER = new byte[]{1}; // Defined
try {
Get get = new Get(rowKey);
Result result = table.get(get);
if (!result.isEmpty()) {
Cell cell = result.getColumnLatestCell(FAMILY, QUALIFIER);
newValue += Bytes.bytesToLong(cell.getValueArray(),cell.getValueOffset());
}
Put put = new Put(rowKey);
put.addColumn(FAMILY,QUALIFIER,Bytes.toBytes(newValue));
table.put(put);
} catch (Exception e) {
// Handle Exceptions...
}
我们(Splice Machine[开源])有一些非常棒的教程,使用 Spark Streaming 在 HBase 中存储数据。
检查 it出去。可能很有趣。
关于hadoop - Hbase Upsert 与 Spark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849976/