`
guoyunsky
  • 浏览: 863432 次
  • 性别: Icon_minigender_1
  • 来自: 上海
博客专栏
3d3a22a0-f00f-3227-8d03-d2bbe672af75
Heritrix源码分析
浏览量:208030
Group-logo
SQL的MapReduce...
浏览量:0
社区版块
存档分类
最新评论

Hadoop MapReduce 学习笔记(五) MapReduce实现类似SQL的max和min

 
阅读更多

  本博客属原创文章,转载请注明出处:http://guoyunsky.iteye.com/blog/1233726

 

       请先阅读:           

           1.Hadoop MapReduce 学习笔记(一) 序言和准备

           2.Hadoop MapReduce 学习笔记(二) 序言和准备 2

           3.Hadoop MapReduce 学习笔记(三) MapReduce实现类似SQL的SELECT MAX(ID)

           4.Hadoop MapReduce 学习笔记(四) MapReduce实现类似SQL的SELECT MAX(ID) 2 一些改进

 

    下一篇: Hadoop MapReduce 学习笔记(六) MapReduce实现类似SQL的max和min  正确写法

 

        Hadoop MapReduce 学习笔记(四) MapReduce实现类似SQL的SELECT MAX(ID) 2 一些改进 只是找出一列中的最大值,但我又想找出最小值,或者平均,或者一列的总和呢.这里也就是想多输出几个结果,之前只是一个.MapReduce该如何实现呢?具体请看代码吧:但这里是一个错误的实现,注意,输出单个值跟输出多个值的Map和Reduce写法是不一样的.

 

 

package com.guoyun.hadoop.mapreduce.study;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 或得最大和最小值,类似SQL:SELECT MAX(NUMBER),MIN(NUMBER) FROM TABLE
 * 注意:这里只有一列数据,多列请查看 @GetMaxAndMinValueMultiMapReduceTest
 * 
 * 这是个错误的写法,结果类似:
 *  maxValue 10000000
    minValue  9999999
    maxValue  9999955
    minValue  9223372036854775807
    maxValue  119
    minValue  9223372036854775807
    maxValue  9999889
    minValue  9223372036854775807
    ...
 * 会有多个maxValue和minValue
 * 正确的写法请参考 @GetMaxAndMinValueMapReduceFixTest
 */
public class GetMaxAndMinValueMapReduceTest extends MyMapReduceSIngleColumnTest{
  
  public static final Logger log=LoggerFactory.getLogger(GetMaxAndMinValueMapReduceTest.class);
  

  public GetMaxAndMinValueMapReduceTest(String outputPath) {
    super(outputPath);
    // TODO Auto-generated constructor stub
  }
  
  /**
   * Map,to get the source datas
   */
  public static class MyMapper extends Mapper<LongWritable,Text,Text,LongWritable>{
    private final Text writeKey=new Text("K");
    private LongWritable writeValue=new LongWritable(0);
    
    @Override
    protected void map(LongWritable key, Text value, Context context)
        throws IOException, InterruptedException {
      log.debug("begin to map");
      StringTokenizer tokenizer=null;
      String lineValue=null;
      
      
      tokenizer=new StringTokenizer(value.toString().trim());
      while(tokenizer.hasMoreTokens()){
        lineValue=tokenizer.nextToken().trim();
        if(lineValue.equals("")){
          continue;
        }
        try {
          writeValue.set(Long.parseLong(lineValue));
          context.write(writeKey, writeValue);
        } catch (NumberFormatException e) {
          continue;
        }
        
      }
    }
  }
  
  public static class MyCombiner
    extends Reducer<Text,LongWritable,Text,LongWritable>{
    private final Text maxValueKey=new Text("maxValue");
    private final Text minValueKey=new Text("minValue");
    
    @Override
    public void reduce(Text key, Iterable<LongWritable> values,Context context)
        throws IOException, InterruptedException {
      log.debug("begin to combine");
      long maxValue=Long.MIN_VALUE;
      long minValue=Long.MAX_VALUE;
      long valueTmp=0;
      LongWritable writeValue=new LongWritable(0); 
      for(LongWritable value:values){
        valueTmp=value.get();
        if(valueTmp>maxValue){
          maxValue=valueTmp;
        }else if(valueTmp<minValue){
          minValue=valueTmp;
        }
      }
      writeValue.set(maxValue);
      context.write(maxValueKey, writeValue);
      writeValue.set(minValue);
      context.write(minValueKey, writeValue);
    } 
    
  }
  
  
  /**
   * Reduce,to get the max value
   */
  public static class MyReducer 
    extends Reducer<Text,LongWritable,Text,LongWritable>{
    private final Text maxValueKey=new Text("maxValue");
    private final Text minValueKey=new Text("minValue");
        
    @Override
    public void reduce(Text key, Iterable<LongWritable> values,Context context)
        throws IOException, InterruptedException {
      log.debug("begin to reduce");
      long maxValue=Long.MIN_VALUE;
      long minValue=Long.MAX_VALUE;
      long valueTmp=0;
      LongWritable writeValue=new LongWritable(0); 
      System.out.println(key.toString());
      for(LongWritable value:values){
        valueTmp=value.get();
        if(valueTmp>maxValue){
          maxValue=valueTmp;
        }else if(valueTmp<minValue){
          minValue=valueTmp;
        }
      }
      writeValue.set(maxValue);
      context.write(maxValueKey, writeValue);
      writeValue.set(minValue);
      context.write(minValueKey, writeValue);
    } 
  }
  
  /**
   * @param args
   */
  public static void main(String[] args) {
    MyMapReduceTest mapReduceTest=null;
    Configuration conf=null;
    Job job=null;
    FileSystem fs=null;
    Path inputPath=null;
    Path outputPath=null;
    long begin=0;
    String output="testDatas/mapreduce/MROutput_SingleColumn_getMaxAndMin";
    
    
    try {
      mapReduceTest=new GetMaxAndMinValueMapReduceTest(output);
      
      inputPath=new Path(mapReduceTest.getInputPath());
      outputPath=new Path(mapReduceTest.getOutputPath());
      
      conf=new Configuration();
      job=new Job(conf,"getMaxAndMinValue");
      
      fs=FileSystem.getLocal(conf);
      if(fs.exists(outputPath)){
        if(!fs.delete(outputPath,true)){
          System.err.println("Delete output file:"+mapReduceTest.getOutputPath()+" failed!");
          return;
        }
      }
      
      
      job.setJarByClass(GetMaxAndMinValueMapReduceTest.class);
      job.setMapOutputKeyClass(Text.class);
      job.setMapOutputValueClass(LongWritable.class);
      job.setOutputKeyClass(Text.class);
      job.setOutputValueClass(LongWritable.class);
      job.setMapperClass(MyMapper.class);
      job.setCombinerClass(MyCombiner.class);
      job.setReducerClass(MyReducer.class);
      
      job.setNumReduceTasks(2);
      
      FileInputFormat.addInputPath(job, inputPath);
      FileOutputFormat.setOutputPath(job, outputPath);
      
      
      begin=System.currentTimeMillis();
      job.waitForCompletion(true);
      
      System.out.println("===================================================");
      if(mapReduceTest.isGenerateDatas()){
        System.out.println("The maxValue is:"+mapReduceTest.getMaxValue());
        System.out.println("The minValue is:"+mapReduceTest.getMinValue());
      }
      System.out.println("Spend time:"+(System.currentTimeMillis()-begin));
      // Spend time:12334
      
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    

  }

}
 

更多技术文章、感悟、分享、勾搭,请用微信扫描:

分享到:
评论

相关推荐

    Hadoop MapReduce实现tfidf源码

    本篇文章将详细讲解如何利用Hadoop MapReduce实现TF-IDF(Term Frequency-Inverse Document Frequency)算法,这是一种在信息检索和文本挖掘中用于评估一个词在文档中的重要性的统计方法。 首先,我们要理解TF-IDF...

    Hadoop mapreduce实现wordcount

    【标题】Hadoop MapReduce 实现 WordCount ...通过理解和实践 Hadoop MapReduce 的 WordCount 示例,开发者可以快速掌握 MapReduce 的基本工作原理,为进一步学习和应用大数据处理技术打下坚实基础。

    大数据 hadoop mapreduce 词频统计

    MapReduce是一种分布式计算模型,由Google提出,Hadoop对其进行了实现。在MapReduce中,数据处理分为两个主要阶段:Map阶段和Reduce阶段。Map阶段将原始数据分解成小块,然后对每个小块进行并行处理;Reduce阶段则...

    Hadoop MapReduce Cookbook 源码

    通过本书的学习,读者不仅能掌握MapReduce的基本操作,还能了解到如何通过实践提升Hadoop系统的效率和稳定性。书中提供的源码对于理解MapReduce的工作流程至关重要,读者可以通过实际运行和修改这些代码,加深对概念...

    Hadoop MapReduce实战手册(完整版)

    总之,《Hadoop MapReduce实战手册》全面覆盖了MapReduce的基本概念、工作流程、编程模型以及在大数据处理中的实际应用,是学习和理解大数据处理技术的理想读物。通过深入阅读,读者可以提升在大数据环境下的编程和...

    Hadoop mapreduce 实现KMeans

    在 Hadoop MapReduce 中实现 KMeans,我们可以将这个过程分解为两个主要步骤:Map 和 Reduce。 **Map 阶段:** - 输入:原始数据集,每个数据点为一行。 - 输出:&lt;(质心ID, 数据点)&gt; 键值对,其中质心ID表示当前...

    python hadoop mapreduce 相似用户|mapreduce.rar

    本文将深入探讨如何使用Python来编写Hadoop MapReduce程序,以实现微博关注者之间的相似用户分析。这个任务的关键在于理解并应用分布式计算原理,以及熟悉Python编程语言在大数据环境下的应用。 首先,Hadoop ...

    Hadoop MapReduce v2 Cookbook, 2nd Edition-Packt Publishing(2015) 高清完整版PDF下载

    总之,《Hadoop MapReduce v2 Cookbook》第二版深入介绍了Hadoop MapReduce V2的相关技术和实践方法,适合于想要深入了解和掌握Hadoop MapReduce V2的开发者和技术人员阅读。通过本书的学习,读者不仅可以了解Hadoop...

    基于Hadoop Mapreduce 实现酒店评价文本情感分析(python源码+项目说明).zip

    基于Hadoop Mapreduce 实现酒店评价文本情感分析(python源码+项目说明).zip基于Hadoop Mapreduce 实现酒店评价文本情感分析(python源码+项目说明).zip基于Hadoop Mapreduce 实现酒店评价文本情感分析(python...

    Java操作Hadoop Mapreduce基本实践源码

    在大数据处理领域,Hadoop MapReduce是一个至关重要的组件,它为海量数据的并行处理提供了分布式计算框架。本文将深入探讨如何使用...通过深入学习和实践,开发者可以利用Hadoop MapReduce解决大数据处理中的各种问题。

    基于Apriori算法的频繁项集Hadoop mapreduce

    在大数据处理领域,Apriori算法与Hadoop MapReduce的结合是实现大规模数据挖掘的关键技术之一。Apriori算法是一种经典的关联规则学习算法,用于发现数据集中频繁出现的项集,进而挖掘出有趣的关联规则。而Hadoop ...

    基于Hadoop Mapreduce 实现酒店评价文本情感分析(python开发源码+项目说明).zip

    基于Hadoop Mapreduce 实现酒店评价文本情感分析(python开发源码+项目说明).zip基于Hadoop Mapreduce 实现酒店评价文本情感分析(python开发源码+项目说明).zip基于Hadoop Mapreduce 实现酒店评价文本情感分析...

    Hadoop MapReduce.md

    本章介绍了 Hadoop MapReduce,同时发现它有以下缺点: 1、程序设计模式不容易使用,而且 Hadoop 的 Map Reduce API 太过低级,很难提高开发者的效率。 2、有运行效率问题,MapReduce 需要将中间产生的数据保存到...

    Hadoop mapreduce 实现MatrixMultiply矩阵相乘

    本主题将深入探讨如何使用Hadoop MapReduce来实现MatrixMultiply,即矩阵相乘,这是一个基础且重要的数学运算,尤其在数据分析、机器学习以及高性能计算中有着广泛应用。 首先,理解矩阵相乘的基本原理至关重要。在...

    Hadoop之MapReduce编程实例完整源码

    一个自己写的Hadoop MapReduce实例源码,网上看到不少网友在学习MapReduce编程,但是除了wordcount范例外实例比较少,故上传自己的一个。包含完整实例源码,编译配置文件,测试数据,可执行jar文件,执行脚本及操作...

    Hadoop mapreduce 实现InvertedIndexer倒排索引

    Hadoop mapreduce 实现InvertedIndexer倒排索引,能用。

    Hadoop mapreduce 实现MR_DesicionTreeBuilder 决策树

    Hadoop MapReduce 是 Apache Hadoop 的核心组件之一,它提供了一种处理和存储大规模数据集的并行计算模型。Map 阶段将输入数据分割成小块,并在各个节点上并行运行映射任务。Reduce 阶段则负责收集映射阶段的结果,...

    Hadoop MapReduce v2 Cookbook (第二版)

    Hadoop MapReduce v2 Cookbook (第二版), Packt Publishing

Global site tag (gtag.js) - Google Analytics