zoukankan      html  css  js  c++  java
  • 任务类MapReduce例子1wordcount

    这两天个人几篇文章介绍了改任务类的文章. 关联文章的地址

        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()执行任务。

        

    文章结束给大家分享下程序员的一些笑话语录: 开发时间
      项目经理: 如果我再给你一个人,那可以什么时候可以完工?程序员: 3个月吧!项目经理: 那给两个呢?程序员: 1个月吧!
    项目经理: 那100呢?程序员: 1年吧!
    项目经理: 那10000呢?程序员: 那我将永远无法完成任务.

  • 相关阅读:
    smarty语法
    combobox里面显示checkbox
    requirejs打包项目
    datagrid中用tooltip
    combobox默认值为第一个数据,修改为空值
    easyui-textbox高为0
    C++并发编程 异步任务
    C++并发编程 互斥和同步
    C++并发编程 thread
    C++智能指针
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3074089.html
Copyright © 2011-2022 走看看