import java.io.IOException;
import java.util.StringTokenizer;
import java.util.*;
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;

public class max_number {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
 
    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
    String line=value.toString();
      StringTokenizer itr = new StringTokenizer(line,"\n");
      while (itr.hasMoreTokens()) {
    StringTokenizer tkl=new StringTokenizer(itr.nextToken());
    String sKey=tkl.nextToken();
    String sNum=tkl.nextToken();
    Text key=new Text(sKey);
    int numInt=Integer.parseInt(sScore);
        context.write(key, new IntWritable(numInt));
      }
    }
  }

  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 max = -219876543;
  Iterator<IntWritable>iterator=values.iterator();
      while(iterator.hasNext())
      {
        int max1= iterator.next().get();
    if(max1>=max){max=max1;}

    
}  context.write(key, new IntWritable(max));
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "max number");
    job.setJarByClass(max_number.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(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

运行需要的设置

导入hadoop的class和java_home路径
export JAVA_HOME=/usr/java/default
export PATH=${JAVA_HOME}/bin:${PATH}
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar

--编译写好的MapReduce java程序
$ $Hadoop_Home/bin/hadoop com.sun.tools.javac.Main max_number.java
$ jar cf maxnumber.jar max_number*.class
--上传文件到hdfs
$Hadoop_Home/bin/hadoop fs --copyFromLocal /home/hadoop/hadoop/random.txt /test/random
--执行
 $Hadoop_Home/bin/hadoop jar maxnumber.jar max_number /test/random /output11/minnumber2 //练习执行的脚本

查询输出Output:

$ $Hadoop_Home/bin/hadoop fs -cat /output11/minnumber2/part-r-00000`

简单的计算最值的MapReduce程序的更多相关文章

  1. 计算均值mean的MapReduce程序Computing mean with MapReduce

    In this post we'll see how to compute the mean of the max temperatures of every month for the city o ...

  2. 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行

    [TOC] 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行 程序源码 import java.io.IOException; import java.util. ...

  3. 编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行

    今天主要来说说怎么在Hadoop2.2.0分布式上面运行写好的 Mapreduce 程序. 可以在eclipse写好程序,export或用fatjar打包成jar文件. 先给出这个程序所依赖的Mave ...

  4. [python]使用python实现Hadoop MapReduce程序:计算一组数据的均值和方差

    这是参照<机器学习实战>中第15章“大数据与MapReduce”的内容,因为作者写作时hadoop版本和现在的版本相差很大,所以在Hadoop上运行python写的MapReduce程序时 ...

  5. OpenJudge计算概论-简单算术表达式求值

    /*===================================== 简单算术表达式求值 总时间限制: 1000ms 内存限制: 65536kB 描述 2位正整数的简单算术运算(只考虑整数运 ...

  6. 从零开始学习Hadoop--第2章 第一个MapReduce程序

    1.Hadoop从头说 1.1 Google是一家做搜索的公司 做搜索是技术难度很高的活.首先要存储很多的数据,要把全球的大部分网页都抓下来,可想而知存储量有多大.然后,要能快速检索网页,用户输入几个 ...

  7. Hadoop(十三)分析MapReduce程序

    前言 刚才发生了悲伤的一幕,本来这篇博客马上就要写好的,花了我一晚上的时间.但是刚才电脑没有插电源就没有了.很难受!想哭,但是没有办法继续站起来. 前面的一篇博文中介绍了什么是MapReduce,这一 ...

  8. 第一个MapReduce程序——WordCount

    通常我们在学习一门语言的时候,写的第一个程序就是Hello World.而在学习Hadoop时,我们要写的第一个程序就是词频统计WordCount程序. 一.MapReduce简介 1.1 MapRe ...

  9. hive--构建于hadoop之上、让你像写SQL一样编写MapReduce程序

    hive介绍 什么是hive? hive:由Facebook开源用于解决海量结构化日志的数据统计 hive是基于hadoop的一个数据仓库工具,可以将结构化的数据映射为数据库的一张表,并提供类SQL查 ...

随机推荐

  1. centos6.6 LVS+keepalived

    之前有写过keepalived+mysql  和lvsDR模式的分析篇.然而LVS没有写高冗余.今天来写一篇LVS+keepalived的 LVSDR只负责转发,LVS也没有nginx后端检查功能,所 ...

  2. select的5中子句where,group by, havaing, order by, limit的使用顺序及实例

    -- 语法: SELECT select_list FROM table_name [ WHERE search_condition ] [ GROUP BY group_by_expression ...

  3. JAVA面向对象程序设计——实验报告

  4. IOS第18天(6,CAKeyframeAnimation关键帧动画)

    ******* #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...

  5. ORACLE常见错误代码的分析与解决

    (一)在使用ORACLE的过程过,我们会经常遇到一些ORACLE产生的错误,对于初学者而言,这些错误可能有点模糊,而且可能一时不知怎么去处理产生的这些错误,本人就使用中出现比较频繁的错误代码一一做出分 ...

  6. rsync组合inotify-tools完成实时同步[转]

    一. 什么是inotify inotify是一种强大的.细粒度的.异步的文件系统事件监控机制,Linux内核从2.6.13开始引入,允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件 ...

  7. Given a compiled machine-language program, which statements in the source language cause the execution of the most machine-language instructions and what is the execution time of these instr

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A  variety  of  studi ...

  8. switch多分支语句

    1.switch多分支语句的语法 switch(表达式){ case 常量值:要执行的语句; break; case 常量值:要执行的语句; break; case 常量值:要执行的语句; break ...

  9. 一种javascript链式多重继承的方式(__proto__原型链)

    var a=function(){this.foo='bar';} a.prototype={b:1}; var aa=function(){} aa.prototype={c:2,__proto__ ...

  10. 小吐槽Toolbar

    最近弄界面 要吧全部图标改成PNG格式 虽说从2010以后Delphi默认支持PNG格式图片, 但是想应用到按钮上, 似乎除了TButton意外, 也只能ToolBar可以正常显示了, 其他的, 比如 ...