Hadoop示例程序WordCount详解及实例http://blog.csdn.net/xw13106209/article/details/6116323

hadoop中使用MapReduce编程实例(转)http://eric-gcm.iteye.com/blog/1807468

【mapreduce进阶编程二】奇偶行分别求和http://www.aboutyun.com/forum.php?mod=viewthread&tid=9360

hadoop2.2.0 MapReduce求和并排序http://www.cnblogs.com/mengyao/p/4151509.html

MapReduce求最大值http://blog.csdn.net/lzm1340458776/article/details/43227759

慢慢看!

求最大值:

package org.apache.hadoop.examples;

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.Partitioner;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class Score2 {
private static String SPILT = " ";
private static int max = -10000;

public static class ScoreMapper extends Mapper<LongWritable, Text, Text, Text> {

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

int num = Integer.valueOf(String.valueOf(value));

if (max < num)
max = num;
context.write(new Text(String.valueOf(max)), new Text(""));
}
}

public static class MaxReducer extends Reducer<Text, Text, Text, Text> {
@Override
public void reduce(Text key, Iterable<Text> value, Context context) throws IOException, InterruptedException {
int num = Integer.valueOf(String.valueOf(key));

if (num == max) {
context.write(new Text("max:"), new Text(String.valueOf(num)));
}
}
}

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.out.println("please input at least 2 arguments");
System.exit(2);
}

Job job = Job.getInstance(conf, "max");
job.setJarByClass(Score2.class);
job.setMapperClass(ScoreMapper.class);
job.setReducerClass(MaxReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setNumReduceTasks(1);

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

 
啊啊啊啊啊啊

import java.io.IOException;
import java.util.Iterator;
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.DoubleWritable;
import org.apache.hadoop.io.DoubleWritable.Comparator;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.GenericOptionsParser;

public class max {
  public static class Map1 extends

Mapper<Object, Text, Text, IntWritable> {

public void map(Object key, Text value, Context context)

throws IOException, InterruptedException {
          int count=0;
          String line = value.toString();
          String s[]=line.split(" ");
          for (int i=0;i<s.length;i++)
          {
           //if (Integer.parseInt(s[i])<min)//s[i]bian int
            //min=Integer.parseInt(s[i]);

char c[] = s[i].toCharArray();
           if (c[c.length-1]=='h')
            count++;

}
          context.write(new Text("1"), new IntWritable(count));
          //context.write(new Text(key.tostring),new IntWritable(max));

}

}

public static class Reduce1 extends
  Reducer<Text, IntWritable, Text, IntWritable> {
      public void reduce(Text key, Iterable<IntWritable> values,
      Context context) throws IOException, InterruptedException {
       //int min=1000000;
       int sum=0;
          Iterator<IntWritable> iterator = values.iterator();  //
          while (iterator.hasNext()) {
           int n=iterator.next().get();

// if (n<min)
           // min=n;
         sum=sum+n;
          }
      context.write(new Text(" "), new IntWritable(sum));
      }
  }

public static void main(String[] args) throws Exception {

Configuration conf = new Configuration();
         FileSystem fs = FileSystem.get(conf);
         conf.set("mapred.job.tracker", "127.0.0.1:9000");
         String[] otherArgs = new GenericOptionsParser(conf, args)
                 .getRemainingArgs();
         if (otherArgs.length != 2) { //input output

System.err.println("Usage: Data Deduplication <in> <out><out>");
             System.exit(2);
         }
         Job job = Job.getInstance(conf,"min");

job.setJarByClass(max.class);

job.setMapperClass(Map1.class);
         job.setMapOutputKeyClass(Text.class);
         job.setMapOutputValueClass(IntWritable.class);

job.setReducerClass(Reduce1.class);
         job.setOutputKeyClass(Text.class);
         job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
         Path outpath = new Path(otherArgs[1]);
         if (fs.exists(outpath))
         {
          fs.delete(outpath,true);
         }
         FileOutputFormat.setOutputPath(job, outpath);

if (job.waitForCompletion(true))
         {

System.exit(0);

}

}

}

云计算-MapReduce的更多相关文章

  1. 王家林的“云计算分布式大数据Hadoop实战高手之路---从零开始”的第十一讲Hadoop图文训练课程:MapReduce的原理机制和流程图剖析

    这一讲我们主要剖析MapReduce的原理机制和流程. “云计算分布式大数据Hadoop实战高手之路”之完整发布目录 云计算分布式大数据实战技术Hadoop交流群:312494188,每天都会在群中发 ...

  2. 【云计算 Hadoop】Hadoop 版本 生态圈 MapReduce模型

    忘的差不多了, 先补概念, 然后开始搭建集群实战 ... . 一 Hadoop版本 和 生态圈 1. Hadoop版本 (1) Apache Hadoop版本介绍 Apache的开源项目开发流程 : ...

  3. 云计算大会有感—MapReduce和UDF

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 1.參会有感       首先还是非常感谢CSDN能给我票,让我有机会參加这次中国云计算峰会.感觉不写点什么对不 ...

  4. 换个角度理解云计算之MapReduce

    上一篇简单讲了一下HDFS,简单来说就是一个叫做“NameNode”的大哥,带着一群叫做“DataNode”的小弟,完成了一坨坨数据的存储,其中大哥负责保存数据的目录,小弟们负责数据的真正存储,而大哥 ...

  5. 云计算(6)--一些MapReduce的例子

    例1:文件的字符串查找 这里reduce不做merge的工作,因为每行都是不一样的,不能merge. 与传统的grep程序相比,使用MapReduce可以加快处理,因为1它是Distributed的, ...

  6. 云计算(5)---MapReduce

    什么是MapReduce 例如用MapReduce如何计算12+22+32+42 用MapReduce执行Wordcount 步骤1:Map map task1 和map task2是独立,并行进行 ...

  7. 云计算——实验一 HDFS与MAPREDUCE操作

    1.虚拟机集群搭建部署hadoop 利用VMware.centOS-7.Xshell(secureCrt)等软件搭建集群部署hadoop 远程连接工具使用Xshell: HDFS文件操作 2.1 HD ...

  8. 换个角度理解云计算之MapReduce(二)

    接上篇 3.Combiner操作 前面讲完Map操作,总结一下就是:一个大文件,分成split1~5,对应于Map1~5,每一个Map处理一个split,每一个split的每一行,会用每一个Map的m ...

  9. 云计算(8)--MapReduce如何处理fault

    一些常见的故障 NM周期性的给RM发送heartbeats,如果RM发现server fails,则它会让所有与这个server有关的AM知道,让受影响的job的AM采取一些action,重新分配它的 ...

随机推荐

  1. 用IDEA开发简单的Servlet

    最近学习java,主要是servlet相关的内容.IDEA和servlet之前都没有碰过,所以做了一下小实验,走了一些弯路:这里把一个完整的步骤写出来,加深一下印象. IDEA创建项目步骤 1. 在i ...

  2. js生成uuid代码

    function uuid() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 3 ...

  3. js 复制到剪切板

    function copyTextToClipboard(text) { var copyFrom = $('<textarea/>'); copyFrom.text(text); $(' ...

  4. Spring之c3p0连接池xml配置和使用举例

    1.导入jar包 c3p0-0.9.5.2.jar mchange-commons-java-0.2.11.jar 2.源码: beans.xml <beans xmlns="http ...

  5. Eclipse中项目上有小红叉,但就是找不到报错文件(总结,持续更新)

    1.jdk问题解决:jdk配置参考:http://blog.csdn.net/superit401/article/details/72847110 2.build path:项目右键——Build ...

  6. (转)C# Aop简单扫盲及ORM实体类属性拦截示例

    转自: http://www.cnblogs.com/cyq1162/archive/2012/05/30/2526573.html 先说下场景,C#中为什么要使用Aop,而我又是在哪里使用Aop? ...

  7. 在eclipse中使用git的pull功能时报错解决办法

    打开项目的 .git/config文件,参照以下进行编辑 [core] symlinks = false repositoryformatversion = 0 filemode = false lo ...

  8. Linux上shell脚本,字符串转ASCII码

    在shell脚本里,将字符串转ASCII码的方法: [keysystem@localhost ~]$ printf "%d" "'A" [keysystem@l ...

  9. python学习(26)分析ajax请求抓取今日头条cosplay小姐姐图片

    分析ajax请求格式,模拟发送http请求,从而获取网页代码,进而分析取出需要的数据和图片.这里分析ajax请求,获取cosplay美女图片. 登陆今日头条,点击搜索,输入cosplay 下面查看浏览 ...

  10. 五大常见的MySQL高可用方案

      1. 概述 我们在考虑MySQL数据库的高可用的架构时,主要要考虑如下几方面: 1.1 如果数据库发生了宕机或者意外中断等故障,能尽快恢复数据库的可用性,尽可能的减少停机时间,保证业务不会因为数据 ...