在Hadoop中,MapReduce起着非常重要的数据处理作用。HDFS作为数据存储的核心部件起着管理文件资源的作用,而MapReduce作为一种编程计算模型,对数据的分析和处理任务进行调度管理。
在MapReduce中,所有的机器分成两个角色,一个叫JobTracker,另外一个叫TaskTracker。同HDFS的分布式概念一样,JobTracker是用于执行总体调度的,一般运行在NameNode机器上,而TaskTracker则负责在节点DataNode上执行具体的分布式任务的计算。海量的数据的计算任务就被许多个TaskTracker将计算量分布在每一个节点上,从而最大程度的利用资源。
更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
在MapReduce的入门也有一个HelloWord的例子,源代码如下:
- package com.ant.ren.hadoop.demo;
- import java.io.IOException;
- import java.util.StringTokenizer;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- 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.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- public class WordCount extends Configured implements Tool{
- /**
- * you can create any class extends Mapper stand for map
- * in map stage, we just split input file content by StringTokenizer default token: whitespace
- * then use word as key, show up count (1) as value
- **/
- public static class Map extends Mapper<LongWritable, Text, Text, IntWritable>{
- private final static IntWritable one = new IntWritable(1);
- private Text word = new Text();
- public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
- String line = value.toString();
- StringTokenizer tokenizer = new StringTokenizer(line);
- while(tokenizer.hasMoreElements()){
- word.set(tokenizer.nextToken());
- context.write(word, one);
- }
- }
- }
- /**
- * you can create any class extends Reducer stand for reduce
- * in reduce stage, we iterate value for the same key (word), calculate the sum of their values
- * then output <word, sum> as reduce result
- **/
- public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>{
- public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
- int sum = 0;
- for(IntWritable val : values)
- sum += val.get();
- context.write(key, new IntWritable(sum));
- }
- }
- @Override
- public int run(String[] args) throws Exception {
- Job job = new Job(getConf());
- job.setJarByClass(WordCount.class);
- job.setJobName("Word Count Job");
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- job.setMapperClass(Map.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- FileInputFormat.setInputPaths(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- boolean success = job.waitForCompletion(true);
- return success ? 0 : 1;
- }
- public static void main(String[] args) throws Exception{
- int ret = ToolRunner.run(new WordCount(), args);
- System.exit(ret);
- }
- }
