分享

学习hadoop2.7 API:通过eclipse查看hadoop2.7 包及 wordcount例子源码


问题导读


1.获取查看hadoop2.7 jar包对应的源码?
2.如何获取hadoop2.7 wordcount例子?





参考:
从零教你如何获取hadoop2.4源码并使用eclipse关联hadoop2.4源码




下面我们导入hadoop2.7 包及hadoop2.7自带例子

添加外部包.png


在添加外部包的时候,在路径中

[mw_shl_code=bash,true]/usr/hadoop-2.7.0/share/hadoop/mapreduce[/mw_shl_code]
找到

hadoop-mapreduce-examples-2.7.0.jar


lujing.png


添加进来。
这时候我们双击 .class文件,我们即可看到源码
1.png


然后添加zip包
hadoop2.7源码
链接:http://pan.baidu.com/s/1mgsxuNA 密码:8kqm

2.png


kan到源码.png


同样这也是正宗的hadoop2.7 api
[mw_shl_code=bash,true]/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{
   
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
      
    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }
  
  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length < 2) {
      System.err.println("Usage: wordcount <in> [<in>...] <out>");
      System.exit(2);
    }
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    for (int i = 0; i < otherArgs.length - 1; ++i) {
      FileInputFormat.addInputPath(job, new Path(otherArgs));
    }
    FileOutputFormat.setOutputPath(job,
      new Path(otherArgs[otherArgs.length - 1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}
[/mw_shl_code]

这样我们就可以在原先的基础上,改造hadoop2.7 wordcount了





已有(3)人评论

跳转到指定楼层
modmas 发表于 2015-7-9 15:09:55
在windows下报空指针异常怎么解决啊。
回复

使用道具 举报

Alkaloid0515 发表于 2015-7-9 15:11:57
modmas 发表于 2015-7-9 15:09
在windows下报空指针异常怎么解决啊。



首先确保程序是没有错的。
第二个可能性就是使用了错误版本的api造成的
参考
Linux下eclipse运行mapreduce问题记录【hadoop2.7】
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条