Hadoop之MapReduce

2019-03-28 13:54|来源: 网络

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的例子,源代码如下:

  1. package com.ant.ren.hadoop.demo;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.StringTokenizer;  
  5.   
  6. import org.apache.hadoop.conf.Configured;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IntWritable;  
  9. import org.apache.hadoop.io.LongWritable;  
  10. import org.apache.hadoop.io.Text;  
  11. import org.apache.hadoop.mapreduce.Job;  
  12. import org.apache.hadoop.mapreduce.Mapper;  
  13. import org.apache.hadoop.mapreduce.Reducer;  
  14. import org.apache.hadoop.util.Tool;  
  15. import org.apache.hadoop.util.ToolRunner;  
  16. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  17. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  18. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  19. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  20.   
  21. public class WordCount extends Configured implements Tool{  
  22.     /** 
  23.     * you can create any class extends Mapper stand for map 
  24.     * in map stage, we just split input file content by StringTokenizer default token: whitespace 
  25.     * then use word as key, show up count (1) as value 
  26.     **/  
  27.     public static class Map extends Mapper<LongWritable, Text, Text, IntWritable>{  
  28.         private final static IntWritable one = new IntWritable(1);  
  29.         private Text word = new Text();  
  30.           
  31.         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{  
  32.             String line = value.toString();  
  33.             StringTokenizer tokenizer = new StringTokenizer(line);  
  34.               
  35.             while(tokenizer.hasMoreElements()){  
  36.                 word.set(tokenizer.nextToken());  
  37.                 context.write(word, one);  
  38.             }  
  39.         }  
  40.     }  
  41.       
  42.     /** 
  43.     * you can create any class extends Reducer stand for reduce 
  44.     * in reduce stage, we iterate value for the same key (word), calculate the sum of their values 
  45.     * then output <word, sum> as reduce result 
  46.     **/  
  47.     public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>{  
  48.         public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{  
  49.             int sum = 0;  
  50.             for(IntWritable val : values)  
  51.                 sum += val.get();  
  52.             context.write(key, new IntWritable(sum));  
  53.         }  
  54.     }  
  55.       
  56.     @Override  
  57.     public int run(String[] args) throws Exception {  
  58.         Job job = new Job(getConf());  
  59.         job.setJarByClass(WordCount.class);  
  60.         job.setJobName("Word Count Job");  
  61.           
  62.         job.setOutputKeyClass(Text.class);  
  63.         job.setOutputValueClass(IntWritable.class);  
  64.           
  65.         job.setMapperClass(Map.class);  
  66.         job.setReducerClass(Reduce.class);  
  67.           
  68.         job.setInputFormatClass(TextInputFormat.class);  
  69.         job.setOutputFormatClass(TextOutputFormat.class);  
  70.           
  71.         FileInputFormat.setInputPaths(job, new Path(args[0]));  
  72.         FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  73.           
  74.         boolean success = job.waitForCompletion(true);  
  75.         return success ? 0 : 1;  
  76.     }  
  77.       
  78.     public static void main(String[] args) throws Exception{  
  79.         int ret = ToolRunner.run(new WordCount(), args);  
  80.         System.exit(ret);  
  81.     }     
  82. }  

相关问答

更多
  • 首先,一个job具体启动多少个map,是由你配置的inputformat来决定的。inputformat在分配任务之前会对输入进行切片。最终启动的map数目,就是切片的结果数目。具体来看 一、如果使用是自定义的inputformat,那么启动多少个map,是由你实现的public InputSplit[] getSplits(JobConf job, int numSplits)方法决定的,返回的切片有多少个就启动多少个map任务。 二、如果是使用系统系统的TextInputFormat(或FileInpu ...
  • 你参考下这个吧eclipse中开发Hadoop2.x的Map/Reduce项目汇总
  • 它们被分离出来,因为这两个包都代表2个不同的API。 org.apache.hadoop.mapred是旧的API, org.apache.hadoop.mapreduce是新的。 这样做是为了让程序员以更方便,更简单和复杂的方式编写MapReduce作业。 您可能会发现此演示文稿很有用,其中详细讨论了不同之处。 希望这回答你的问题。 They are separated out because both of these packages represent 2 different APIs. org.a ...
  • 本教程提到: 下载Hadoop-core-1.2.1.jar,用于编译和执行MapReduce程序。 访问以下链接http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/1.2.1下载jar。 所以在这里你可以找到不同版本的所有罐子 This tutorial mentions : Download Hadoop-core-1.2.1.jar, which is used to compile and execute the MapRe ...
  • 在Hadoop中,您处理输入拆分而不是块。 输入拆分是完整的数据集。 您希望避免一个映射器超过两个拆分的情况,因为这会降低性能并创建流量。 在文本世界中,假设您在block1中并且您有一个句子,例如“我是一个哈”,而block2继续“doop developer”,那么这会创建网络流量,因为我们始终必须在一个完整的节点上工作输入拆分和一些数据必须转移到另一个节点。 In Hadoop you work on input splits and not on blocks. An input split is ...
  • 您可以将job1的输出作为输入链接到job2。 inputdir1 - > outputdir1 - > outputdir2 ... - > outputdir9 - > outputdir10 You can just chain the output of job1 as the input to job2. inputdir1 -> outputdir1 -> outputdir2 ... -> outputdir9 -> outputdir10
  • 您可以将LIMIT与任务规范一起使用。 但是,如果您必须一次又一次地执行此操作,那么更好的自动化解决方案是使用OOZIE(hadoop的工作流编辑器),可以在hive中为您的数据创建分区。 You can use LIMIT with task specification. However if you have to do it again and again then a better automated solution is to use OOZIE (work flow editor for ha ...
  • mapper的输出键和值类型应该是reducer的输入类型,因此在你的情况下,reducer必须继承自 Reducer setOutputKeyClass和setOutputValueClass设置作业输出的类型,即map和reduce。 如果要为映射器指定其他类型,则应使用方法setMapOutputKeyClass和setMapOutputValueClass 。 作为旁注,当您不希望输出中的真值时,为什么要从 ...
  • Mapper接口按以下顺序需要4个类型参数:Map输入键,Map输入值,Map输出键和Map输出值。 在您的情况下,由于您正在处理4个整数,其中3个构成您的值,1个是您的密钥,因此使用IntWritable作为Map输入键并且应该使用Text而错误。 此外,您在MapClass定义中指定的类型与传递给Map函数的类型不匹配。 鉴于您正在处理文本文件,您的MapClass应定义如下: public static class MapClass extends MapReduceBase implements M ...
  • MapReduce的作用可以称为“执行引擎”。 Pig作为一个系统正在将Pig Latin命令转换为一个或多个MR Jobs。 Pig本身没有能力运行它 - 它将这项工作委托给Hadoop。 我会在编译器和操作系统之间建立类比。 OS执行时编译器创建程序。 在这个比喻中,Pig是编译器,Hadoop是OS。 猪做的更多 - 它运行作业,监视它们等等。所以除了编译器之外,它可以被视为“shell”。 在我的理解中,从以下角度看,Pig不是100%编译器 - 它不会根据命令编译MR作业。 它传递有关应该对已存在 ...