HBase之旅四:HBase MapReduce实例分析(转自:Taobao QA Team)

引言
跟Hadoop的无缝集成使得使用MapReduce对HBase的数据进行分布式计算非常方便,本文将以前面的blog示例,介绍HBase下MapReduce开发要点。很好理解本文前提是你对Hadoop MapReduce有一定的了解,如果你是初次接触Hadoop MapReduce编程,可以参考http://qa.taobao.com/?p=10523 这篇文章来建立基本概念。
HBase MapReduce核心类介绍
首先一起来回顾下MapReduce的基本编程模型,

可以看到最基本的是通过Mapper和Reducer来处理KV对,Mapper的输出经Shuffle及Sort后变为Reducer的输入。除了Mapper和Reducer外,另外两个重要的概念是InputFormat和OutputFormat,定义了Map-Reduce的输入和输出相关的东西。HBase通过对这些类的扩展(继承)来方便MapReduce任务来读写HTable中的数据。

实例分析
我们还是以最初的blog例子来进行示例分析,业务需求是这样:找到具有相同兴趣的人,我们简单定义为如果author之间article的tag相同,则认为两者有相同兴趣,将分析结果保存到HBase。除了上面介绍的blog表外,我们新增一张表tag_friend,RowKey为tag,Value为authors,大概就下面这样。


我们省略了一些跟分析无关的Column数据,上面的数据按前面描述的业务需求经过MapReduce分析,应该得到下面的结果

实际的运算过程分析如下
代码实现
有了上面的分析,代码实现就比较简单了。只需以下几步

  • 定义Mapper类继承TableMapper,map的输入输出KV跟上面的分析一致。

    public static class Mapper extends TableMapper <ImmutableBytesWritable, ImmutableBytesWritable> {
     public Mapper() {}
     @Override
     public void map(ImmutableBytesWritable row, Result values,Context context) throws IOException {
        ImmutableBytesWritable value = null;
        String[] tags = null;
        for (KeyValue kv : values.list()) {
          if ("author".equals(Bytes.toString(kv.getFamily()))
          && "nickname".equals(Bytes.toString(kv.getQualifier()))) {
          value = new ImmutableBytesWritable(kv.getValue());
          }
          if ("article".equals(Bytes.toString(kv.getFamily()))
          && "tags".equals(Bytes.toString(kv.getQualifier()))) {
            tags = Bytes.toString(kv.getValue()).split(",");
          }
       }
        for (int i = 0; i < tags.length; i++) {
          ImmutableBytesWritable key = new ImmutableBytesWritable(
          Bytes.toBytes(tags[i].toLowerCase()));
          try {
              context.write(key,value);
          } catch (InterruptedException e) {
             throw new IOException(e);
            }
          }
       }
    }

  • 定义Reducer类继承TableReducer,reduce的输入输出KV跟上面分析的一致。


    public static class Reducer extends TableReducer <ImmutableBytesWritable, ImmutableBytesWritable, ImmutableBytesWritable> {
     @Override
     public void reduce(ImmutableBytesWritable key,Iterable values,
       Context context) throws IOException, InterruptedException {
      String friends="";
      for (ImmutableBytesWritable val : values) {
       friends += (friends.length()>0?",":"")+Bytes.toString(val.get());
      }
      Put put = new Put(key.get());
      put.add(Bytes.toBytes("person"), Bytes.toBytes("nicknames"),
      Bytes.toBytes(friends));
      context.write(key, put);
     }
    }

  • 在提交作业时设置inputFormat为TableInputFormat,设置outputFormat为TableOutputFormat,可以借助TableMapReduceUtil类来简化编码。

    public static void main(String[] args) throws Exception {
     Configuration conf = new Configuration();
     conf = HBaseConfiguration.create(conf);
     Job job = new Job(conf, "HBase_FindFriend");
     job.setJarByClass(FindFriend.class);
     Scan scan = new Scan();
     scan.addColumn(Bytes.toBytes("author"),Bytes.toBytes("nickname"));
     scan.addColumn(Bytes.toBytes("article"),Bytes.toBytes("tags"));
     TableMapReduceUtil.initTableMapperJob("blog", scan,FindFriend.Mapper.class,
      ImmutableBytesWritable.class, ImmutableBytesWritable.class, job);
     TableMapReduceUtil.initTableReducerJob("tag_friend",FindFriend.Reducer.class, job);
     System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

    小结
    本文通过实例分析演示了使用MapReduce分析HBase的数据,需要注意的这只是一种常规的方式(分析表中的数据存到另外的表中),实际上不局限于此,不过其他方式跟此类似。如果你进行到这里,你肯定想要马上运行它看看结果,在下篇文章中将介绍如何在模拟集群环境下本机运行MapReduce任务进行测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值