zoukankan      html  css  js  c++  java
  • Hadoop_MapReduce中Mapper类和Reduce类

    在权威指南中,有个关于处理温度的MapReduce类,具体如下:

    第一部分:Map

    public class MaxTemperatureMapper extends MapReduceBase 

          implements Mapper<LongWritable,Text,Text,IntWritable>{

    //其他代码

    public void map(LongWritable key, Text value, OutputCollector<Text,IntWritable> output, Report reporter){

    //分析一下这四个参数

    该Mapper 接口是一个泛型类型,有四个形参类型,分别为:

      LongWritable key   Map函数的输入键

      Text value            Map函数的输入值

    OutputCollector<Text,IntWritable> output    输出键

    Report reporter   输出值

    }

    }

    PS :Hadoop 自身提供一套可优化网络序列化传输的基本类型,而不直接使用Java内嵌的类型。这些类型均在 org.apache.hadoop.io 包中。

      LongWritable 类型相当于Java中的Long类型

      Text类型相当于Java中的String类型

      IntWritable 类型相当于Java中的Integer类型

    第二部分:Reduce

    Reducer类的定义和使用

    public class MaxTemperatureReducer extends MapReduceBase 

            implements Reducer<Text, IntWritable, Text, IntWritable>{

    public void reduce(Text key, Iterable<IntWritabloe> values, Context context){

    }

    }

    pS: reduce 函数也有四个形式参数类型用于指定输入和输出类型。

    reduce函数的输入类型必须匹配map函数的输出类型。

    即Text类型和IntWritable 类型。

    第三部:运行MapReduce作业

    调用Job类的方法即可。

    Job对象指定作业执行规范。

  • 相关阅读:
    CF1454F Array Partition
    leetcode1883 准时抵达会议现场的最小跳过休息次数
    leetcode1871 跳跃游戏 VII
    leetcode1872 石子游戏VIII
    CF1355C Count Triangles
    CF1245D Shichikuji and Power Grid
    CF1368C Even Picture
    CF1368D AND, OR and square sum
    CF1395C Boboniu and Bit Operations
    SpringBoot和开发热部署
  • 原文地址:https://www.cnblogs.com/yytlmm/p/4837767.html
Copyright © 2011-2022 走看看