HBase:使用MapReduce更新行?

时间:2016-01-12 08:59:20

标签: java hadoop mapreduce hbase

我在HBase中有一个表,我想对其进行更新。例如,如果pred(row) == truepred是用Java编写的函数),我想将列更新为值。

我可以使用MapReduce吗?最初我认为我可以,但现在我看到MapReduce用于从一个表读取并写入另一个表(或磁盘)。然后我考虑实现一个并行扫描,它将使用多个线程迭代整个表,但似乎我正在重新发明轮子。

1 个答案:

答案 0 :(得分:0)

对于此任务,不需要MapReduce。您可以连接到HBASE并从Java应用程序本身完成工作。对下面的代码有点帮助

HTable table = new HTable(HBaseConfiguration.create(), "MYTABLE");
Scan scan = new Scan();

scan.addFamily(Bytes.toBytes("myfamily"));
ResultScanner scanner = table.getScanner(scan);

for (Result result = scanner.next(); (result != null); result = scanner.next()) {
    for(KeyValue keyValue : result.list()) {
    // Make use of keyValue.getKeyString() and keyValue.getValue() here
    }
}

更新特定行的代码段如下

  Put p = new Put(Bytes.toBytes("row1"));
  p.add(Bytes.toBytes("myfamily"),
  Bytes.toBytes("fieldname"),Bytes.toBytes("NEWVALUE"));
  table.put(p);