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. simhash-- 一种文档去重的算法

    最早看数学之美的时候,书中就提到了这个算法,当时没有做过相关地工作,没什么具体的印象.一年前转岗时面试时别人提到了这个算法,知道了simhash可以用来解决网页等海量数据的去重问题,很高效. 然后自己 ...

  2. sql not in 优化问题

    问题情境: not in 耗时过长.想用join或exits代替.结果并不明显,这里先记录3种写法,以后探讨速度问题. sql语句: // not exists sql = @"select ...

  3. 一本通1628X-factor Chain

    1628:X-factor Chain 时间限制: 1000 ms         内存限制: 524288 KB [题目描述] 原题来自 POJ 3421 输入正整数 x,求 x 的大于 1 的因子 ...

  4. [代码]--GridControl使用技巧总结,更新中...

    1如何禁用GridControl中单击列弹出右键菜单 设置Run Design->OptionsMenu->EnableColumnMenu 设置为:false 2如何定位到第一条数据/记 ...

  5. 再谈Scala集合

    集合!集合!一个现代语言平台上的程序员每天代码里用的最多的大概就是该语言上的集合类了,Scala的集合丰富而强大,至今无出其右者,所以这次再回过头再梳理一下. 本文原文出处:  还是先上张图吧,这是我 ...

  6. 循环取月的三位英语名 Jan Feb

    CultureInfo ci = new CultureInfo("en-US"); DateTime now = DateTime.Now; for (int i = 0; i ...

  7. jQuery EasyUI API 中文文档 - 消息框(Messager)

    http://www.cnblogs.com/Philoo/archive/2011/11/15/jeasyui_api_messager.html Messager  消息框 博客园 风流涕淌 (p ...

  8. Hadoop生态圈-Kafka配置文件详解

    Hadoop生态圈-Kafka配置文件详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.默认kafka配置文件内容([yinzhengjie@s101 ~]$ more /s ...

  9. [应用篇]第二篇 JSP自带标签介绍

    JSP 有以下三类标签: 指令:JSP Directive 指令标签用于设置与整个 JSP 页面相关的属性,非常常用. 下面的三种标签是我们使用频率最高的 标签 jsp标签 描述 <%@ pag ...

  10. c# yield关键字原理详解

    c# yield关键字的用法 1.yield实现的功能 yield return: 先看下面的代码,通过yield return实现了类似用foreach遍历数组的功能,说明yield return也 ...