你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop(4)-MapReduce

[日期:2016-03-07] 来源:林俐的技术博客  作者: [字体: ]

  在之前建立的HDFS基础上,自己编写MapReduce程序,打包,并运行。

  重新打包WordCount并执行

  新建一个Maven项目,将示例程序中WordCount.java的复制到新项目中,使用mvn clean package打包为jar文件并复制到服务器。

 

  WordCount.java内容如下:

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[i]));
    }
    FileOutputFormat.setOutputPath(job,
      new Path(otherArgs[otherArgs.length - 1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

在服务器上创建一个test.txt文件,内容为:

This is a test.

将文件复制到HDFS中:

hadoop/bin/hdfs dfs -put test.txt /mrtest/input

使用下面的命令执行WorkCount:

hadoop/bin/hadoop jar hadoop-mapreduce-demo-0.0.1-SNAPSHOT.jar com.u3dspace.hadoop.mapreduce.demo.WordCount /mrtest/input /mrtest/output

查看输出结果:

hadoop/bin/hdfs dfs -cat /mrtest/output/part-r-00000

This    1
a       1
is      1
test.   1

自定义Writable

定义一个类CountWritable:

public class CountWritable implements Writable {
    private int count;
    
    public CountWritable() {
        this.count = 0;
    }
    public CountWritable(int count) {
        this.count = count;
    }
    
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    
    public void readFields(DataInput in) throws IOException {
        this.count = in.readInt();
    }
    
    public void write(DataOutput out) throws IOException {
        out.writeInt(this.count);
    }
    
    @Override
    public String toString() {
        return Integer.toString(this.count);
    }
}

将刚才示例中的IntWritable换成CountWritable,打包到服务器执行,输出的结果和上一次相同。





收藏 推荐 打印 | 录入:elainebo | 阅读:
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数
点评:
       
评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款