【问题标题】:HBase mapreduce: write into HBase in ReducerHBase mapreduce:在Reducer中写入HBase
【发布时间】:2013-09-03 23:25:25
【问题描述】:

我正在学习 HBase。我知道如何使用 Hadoop MapReduce 编写 Java 程序并将输出写入 HDFS;但现在我想将相同的输出写入 HBase,而不是 HDFS。它应该有一些类似的代码,就像我之前在 HDFS 中所做的那样:

context.write(key,value);

谁能给我一个例子来实现这一点?

【问题讨论】:

    标签: java hadoop mapreduce hbase


    【解决方案1】:

    这是一种方法:

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

    你可以在这里阅读Table Mapper

    【讨论】:

      【解决方案2】:

      在设置作业时,您应该能够使用 TableOutputFormat,而不是使用 FileOutputFormat。

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

      你仍然需要稍微修改你的 Reducer。

      来自上一页的引述:

      转换 Map/Reduce 输出并将其写入 HBase 表。 KEY 被忽略,而输出值必须是 Put 或 Delete 实例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多