简体   繁体   English

HBase:使用MapReduce更新行?

[英]HBase: use MapReduce to update rows?

I have a table in HBase that I'd like to perform an update on. 我在HBase中有一个要对其进行更新的表。 For example, I'd like to update a column to a value if pred(row) == true ( pred is a function written in Java). 例如,如果pred(row) == truepred是用Java编写的函数pred(row) == true ,我想将列更新为一个值。

Can I use MapReduce for this? 我可以为此使用MapReduce吗? Initially I thought I could but now I see that MapReduce is used to read from one table and write to another (or to disk). 最初我以为可以,但是现在我看到MapReduce用于读取一个表并写入另一个表(或磁盘)。 I then considered implementing a parallel scan which will iterate over the entire table using multiple threads, but it seems as though I'm reinventing the wheel. 然后,我考虑实现一个并行扫描,该扫描将使用多个线程在整个表上进行迭代,但是似乎在重新发明轮子。

For this task, MapReduce is not needed. 对于此任务,不需要MapReduce。 You can connect to HBASE and get the work done from java application itself. 您可以连接到HBASE并从Java应用程序本身完成工作。 A little help with the code below 以下代码有一点帮助

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
    }
}

Code snippet to update a particular row is as below 更新特定行的代码段如下

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM