MapReduce例子1--wordcount

1.MapReduce编程模型

MapReduce采用的是“分而治之”的思想,把对大数据集合的操作,分发给一个主节点管理下的各个分节点共同完成,通过整合各个分节点的中间结果,得到最终结果。简单的来说MapReduce就是”任务的分解和结果的合并“。

在hadoop中,用于执行MapReduce的机器角色有两种,一是JobTraker,主要负责任务的调度,二是TaskTraker,主要负责执行任务,

        在分布式计算过程中,MapReduce框架负责处理了分布式存储,工作调度,负载均衡,容错均衡,容错处理以及网络通信等复杂操作。把处理的过程分别抽象成两个函数:map和reduce,map负责把任务分解成多个任务,而reduce把分解后的多任务结果汇总起来。

        Tips:在MapReduce处理的数据集(或任务)必须具有以下特点:待数据集都可以完全分为小数据集,而且每个小数据集都可以并行进行操作。

2.准备工作

      创建两个文本,file1.txt和fil2.txt分别对文本写入一些英文

       在集群上创建一个wordcount文件夹


把创建的两个文本上传到wordcount文件夹下


3.运行代码

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[] ioargs = new String[]{hdfs://hadoop0:9000/home/hadoop/wordcount/file1.txt,hdfs://hadoop0:9000/home/hadoop/wordcount/file2.txt,hdfs://hadoop0:9000/home/hadoop/wordcount_out};

    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

    if (otherArgs.length != 3) {

      System.err.println("Usage: wordcount <in> <out>");

      System.exit(2);

    }

    Job job = new Job(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);

    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));

	FileInputFormat.addInputPath(job, new Path(otherArgs[1]));

    FileOutputFormat.setOutputPath(job, new Path(otherArgs[2]));

    System.exit(job.waitForCompletion(true) ? 0 : 1);

}

}


4.运行结果


5.代码分析

Map过程需要继承org.apache.hadoop.mapreduce包中的Mapper类,并重写map方法,通过在map函数中添加两句把key和value输出到控制台的代码,可以发现map函数的value是文本文件的第一行(以回车为结束标记),而key值为该行首字母相对于文本首地址的偏移量。然后StringToken类把每一个行,拆分为一个单词,分别以<word,1>这样的形式输出。其余的工作交给MapReduce框架来处理。

Reduce过程需要继承org.apache.hadoop.mapreduce包中的Reducer类,并重写reduce方法,Map的过程输出<key,value>中key是单词,value为该单词的出现次数的列表,所以在reduce方法中只需要把value进行遍历求和,就可以得到某一单词的出现次数。

在MapReduce中,由Job类管理和运行一个任务,并通过一些Job的方法对计算任务进行设置,此处设置了使用TokenizerMapper类完成了Map过程的处理,使用IntSumReduce类完成了combine和Reduce过程的处理,还设置了Map过程和Reduce过程的输入输出类型,key为Text类型,vaule为IntWritable类型。任务的输入输出路径分别由FileInputFormat.addInputPath和FileOutputFormat.setOutputPath进行设定,完成相应的参数设定后,即可调用job.waitForComelption()执行任务。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值