简体   繁体   中英

HBase: use MapReduce to update rows?

I have a table in HBase that I'd like to perform an update on. For example, I'd like to update a column to a value if pred(row) == true ( pred is a function written in Java).

Can I use MapReduce for this? 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). 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. You can connect to HBASE and get the work done from java application itself. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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