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 Driver {

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 class SequenceMapper extends
            Mapper<Object, Text, Text, Text> {
        private Text word = new Text();
        public void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {
            String []sep=value.toString().split("\t");
            word.set(sep[1]+"\t"+sep[0]);
            System.out.println(value.toString());
            context.write(word,new Text(""));
        }
    }

public static class SequenceReducer extends
            Reducer<Text,Text,Text,Text> {
        public void reduce(Text key, Iterable<Text> values,
                Context context) throws IOException, InterruptedException {
            String[] sep = key.toString().split("\t");
            System.out.println( sep[0]+"++++++++="+ sep[1]);
            context.write(key,new Text(""));
        }
    }

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> <out>");
            System.exit(2);
        }
        Job job = new Job(conf, "word count");
        job.setJarByClass(Driver.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]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        job.waitForCompletion(true);

Configuration conf2 = new Configuration();
        Job job2 = new Job(conf2, "word count1");
        job2.setJarByClass(Driver.class);
        job2.setMapperClass(SequenceMapper.class);
        job2.setReducerClass(SequenceReducer.class);
        job2.setOutputKeyClass(Text.class);
        job2.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job2, new Path(otherArgs[1]));
        FileOutputFormat.setOutputPath(job2, new Path(otherArgs[2]));
        job2.waitForCompletion(true);
    }
}

mapreduce 顺序组合的更多相关文章

  1. 2018.11.20-day22 类中代码的执行顺序&组合

    1.类中代码的执行顺序 2.组合

  2. mapreduce 依赖组合

    mport java.io.IOException;import java.util.StringTokenizer; import org.apache.hadoop.conf.Configurat ...

  3. 【学习笔记】--- 老男孩学Python,day16-17 初识面向对象,类名称空间,查询顺序,组合

    面向过程 VS 面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了写程序的复 ...

  4. [大牛翻译系列]Hadoop(5)MapReduce 排序:次排序(Secondary sort)

    4.2 排序(SORT) 在MapReduce中,排序的目的有两个: MapReduce可以通过排序将Map输出的键分组.然后每组键调用一次reduce. 在某些需要排序的特定场景中,用户可以将作业( ...

  5. 【原创】MapReduce编程系列之二元排序

    普通排序实现 普通排序的实现利用了按姓名的排序,调用了默认的对key的HashPartition函数来实现数据的分组.partition操作之后写入磁盘时会对数据进行排序操作(对一个分区内的数据作排序 ...

  6. MapReduce怎么优雅地实现全局排序

    思考 想到全局排序,是否第一想到的是,从map端收集数据,shuffle到reduce来,设置一个reduce,再对reduce中的数据排序,显然这样和单机器并没有什么区别,要知道mapreduce框 ...

  7. 大数据技术 —— MapReduce 简介

    本文为senlie原创,转载请保留此地址:http://www.cnblogs.com/senlie/ 1.概要很多计算在概念上很直观,但由于输入数据很大,为了能在合理的时间内完成,这些计算必须分布在 ...

  8. 数字货币量化教程——使用itertools实现各种排列组合

    在量化数据处理中,经常使用itertools来完成数据的各种排列组合以寻找最优参数 一.数据准备 import itertools items = [1, 2, 3] ab = ['a', 'b'] ...

  9. hadoop之计数器和管道的mrunit测试

    引言 hadoop的调试真心让人灰常恼火,而且从企业实际出发,集群的资源是有限的,不可能在集群上跑一遍又一遍根据log去调试代码,那么使用MRUnit编写测试单元,显得尤为重要.MRUnit中的Map ...

随机推荐

  1. Lua手册中的string.len 不解

    Lua手册中的string.len (s) 接收一个字符串,返回其长度. 空串 "" 的长度为 0 . 内嵌零也统计在内,因此 "a\000bc\000" 的长 ...

  2. [译] Block 小测验

    本文来源于 ParseBlog 的其中一篇博文 <Objective-C Blocks Quiz> 如果您觉得我的博客对您有帮助,请通过关注我的新浪微博  MicroCai 支持我,谢谢! ...

  3. wpf 异步加载 只需6段代码

    private BackgroundWorker worker = null; ProgressBar probar = new ProgressBar(); private int percentV ...

  4. Python 学习笔记4

    我是一个艺术家. 今天继续学习python啊.争取看到python流程控制.

  5. windows任务计划程序路径设置

    用任务计划启动程序,特别是脚本,比如我要启动python脚本,其中有一句是这么写的 BasePath = removeLastSlash(os.path.abspath("..\\..\\& ...

  6. for计算100以内的奇数和

    #include "stdio.h" void main() { //for计算100以内的奇数和 步长为1,continue实现 ; ;i<=;i++) { ==) { c ...

  7. C++ 使用string一行一行读取文件

    c++ 读取文件中的一行一行数据 通用模板: std::ifstream in(dictpath); if(!in) { std::cout << __DATE__ << &q ...

  8. 《JavaScript高级程序设计》读书笔记 ---if语句

    if语句 大多数编程语言中最为常用的一个语句就是if 语句.以下是if 语句的语法:if (condition) statement1 else statement2其中的condition(条件)可 ...

  9. PAT (Top Level) Practise 1005 Programming Pattern (35)

    后缀数组.排序之后得到height数组,然后从上到下将height>=len的都分为一组,然后找到第一组个数最多的输出即可. #pragma comment(linker, "/STA ...

  10. 在 Linux 环境下报错 java.lang.reflect.InvocationTargetException

    今天开发了一个 excel 导出数据的功能,放到 linux 服务器上后发现报错. 捕获到 java.lang.reflect.InvocationTargetException 异常,这个异常不太常 ...