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?
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
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.
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.