HBase mapreduce: write into HBase in Reducer

2019-09-05 07:28发布

I am learning the HBase. I know how to write a Java program using Hadoop MapReduce and write the output into HDFS; but now I want to write the same output into HBase, instead of HDFS. It should have some similar code like I did before in HDFS thing:

context.write(key,value);

Could anyone show me an example to achieve this?

2条回答
萌系小妹纸
2楼-- · 2019-09-05 07:53

Instead of using the FileOutputFormat when setting up your job, you should be able to use the TableOutputFormat.

http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/mapreduce/TableOutputFormat.html

You will still have to modify your Reducer a little.

A quote from the page above:

Convert Map/Reduce output and write it to an HBase table. The KEY is ignored while the output value must be either a Put or a Delete instance.

查看更多
地球回转人心会变
3楼-- · 2019-09-05 08:01

Here's one way to do this:

public static class MyMapper extends TableMapper<ImmutableBytesWritable, Put>  {

public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException {
    // this example is just copying the data from the source table...
    context.write(row, resultToPut(row,value));
}

private static Put resultToPut(ImmutableBytesWritable key, Result result) throws IOException {
    Put put = new Put(key.get());
    for (KeyValue kv : result.raw()) {
        put.add(kv);
     }
    return put;
   }
}

You can read here about Table Mapper

查看更多
登录 后发表回答