map reduce程序示例

package test2;

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; import java.io.IOException; /**
样例数据中包含了年份和温度,提出年份里温度最大的
(0, 0067011990999991950051507+0000+),
(33, 0043011990999991950051512+0022+),
(66, 0043011990999991950051518-0011+),
(99, 0043012650999991949032412+0111+),
(132, 0043012650999991949032418+0078+),
(165, 0067011990999991937051507+0001+),
(198, 0043011990999991937051512-0002+),
(231, 0043011990999991945051518+0001+),
(264, 0043012650999991945032412+0002+),
(297, 0043012650999991945032418+0078+),
* */
public class mytest { static String INPUT_PATH="input/t1_num.txt"; //待统计的文件路径
static String OUTPUT_PATH="output/t1_num"; //统计结果存放的路径 static class MyMapper extends Mapper <Object,Object,Text,IntWritable> { //定义继承mapper类
protected void map(Object key, Object value, Context context) throws IOException, InterruptedException{ //定义map方法 String[] arr=value.toString().split("\\),"); //文件中的单词是以“),”分割的,并将每一行定义为一个数组
for(int i=0;i<arr.length;i++){ //遍历循环每一行,统计单词出现的数量
String line = arr[i].toString();
String year = line.substring(line.length()-16, line.length()-12);
String airTemperature = line.substring(line.length()-6, line.length()-1);
context.write(new Text(year),new IntWritable(Integer.valueOf(airTemperature)));
}
/**
map过程中,通过对字符串的解析,得到年-温度的key-value对作为输出
(1950, 0)
(1950, 22)
(1950, -11)
(1949, 111)
(1949, 78)
(1937, 1)
(1937, -2)
(1945, 1)
(1945, 2)
(1945, 78)
*/
}
} static class MyReduce extends Reducer<Text,IntWritable,Text,IntWritable>{ //定义继承reducer类
protected void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException{ //定义reduce方法
int max = 0;
for(IntWritable c:values){ //统计同一个单词的数量
if(c.get()>max){
max = c.get();//获取value值
}
}
IntWritable outValue=new IntWritable(max);//挨个输出
context.write(key,outValue);
}
/**
在reduce过程,将map过程中的输出,按照相同的key(年份)将value放到同一个列表中作为reduce的输入
(1950, [0, 22, –11])
(1949, [111, 78])
(1937, [1, -2])
(1945, [1, 2, 78]) 在reduce过程中,在列表中选择出最大的温度,将年-max温度的key-value作为输出:
(1950, 22)
(1949, 111)
(1937, 1)
(1945, 78)
*/ } public static void main(String[] args) throws Exception{ //main函数
System.setProperty("hadoop.home.dir", "D:\\hadoop-2.7.6");//这一行一定要
Path outputpath=new Path(OUTPUT_PATH); //输出路径
Configuration conf=new Configuration();
Job job=Job.getInstance(conf); //定义一个job,启动任务
FileInputFormat.setInputPaths(job, INPUT_PATH);
FileOutputFormat.setOutputPath(job,outputpath); job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.waitForCompletion(true);
} }

map reduce程序示例的更多相关文章

  1. Hadoop学习笔记2 - 第一和第二个Map Reduce程序

    转载请标注原链接http://www.cnblogs.com/xczyd/p/8608906.html 在Hdfs学习笔记1 - 使用Java API访问远程hdfs集群中,我们已经可以完成了访问hd ...

  2. eclipse 中运行 Hadoop2.7.3 map reduce程序 出现错误(null) entry in command string: null chmod 0700

    运行map reduce任务报错: (null) entry in command string: null chmod 0700 解决办法: 在https://download.csdn.net/d ...

  3. 使用Python实现Map Reduce程序

    使用Python实现Map Reduce程序 起因 想处理一些较大的文件,单机运行效率太低,多线程也达不到要求,最终采用了集群的处理方式. 详细的讨论可以在v2ex上看一下. 步骤 MapReduce ...

  4. 第一个map reduce程序

    完成了第一个mapReduce例子,记录一下. 实验环境: hadoop在三台ubuntu机器上部署 开发在window7上进行 hadoop版本2.2.0 下载了hadoop-eclipse-plu ...

  5. Hadoop 使用Combiner提高Map/Reduce程序效率

    众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value>键值对,再网络节点间对其进行整理(shuffle),然后使用Reducer处理数据并进行最终输出. 在上述过 ...

  6. Hadoop实战:使用Combiner提高Map/Reduce程序效率

    好不easy算法搞定了.小数据測试也得到了非常好的结果,但是扔到进群上.挂上大数据就挂了.无休止的reduce不会结束了. .. .. .... .. ... .. ================= ...

  7. Hadoop Map/Reduce教程

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/mapred_tutorial.html 目的 先决条件 概述 输入与输出 例子:WordCount v1.0 ...

  8. Map/Reduce 工作机制分析 --- 作业的执行流程

    前言 从运行我们的 Map/Reduce 程序,到结果的提交,Hadoop 平台其实做了很多事情. 那么 Hadoop 平台到底做了什么事情,让 Map/Reduce 程序可以如此 "轻易& ...

  9. Map/Reduce个人实战--生成数据测试集

    背景: 在大数据领域, 由于各方面的原因. 有时需要自己来生成测试数据集, 由于测试数据集较大, 因此采用Map/Reduce的方式去生成. 在这小编(mumuxinfei)结合自身的一些实战经历, ...

随机推荐

  1. java ArrayList、Vector、LinkedList区别

  2. WebBrowser控件的NavigateToString()方法 参数 为中文时乱码问题的解决。

    public static string ConvertExtendedASCII(string HTML) { StringBuilder str = new StringBuilder(); ch ...

  3. Windows下return,exit和ExitProcess的区别和分析

    通常,我们为了使自己的程序结束,会在主函数中使用return或调用exit().在windows下还有ExitProcess()和TerminateProcess()等函数. 本文的目的是比较以上几种 ...

  4. Tour HDU - 3488 有向环最小权值覆盖 费用流

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 给一个无源汇的,带有边权的有向图 让你找出一个最小的哈密顿回路 可以用KM算法写,但是费用流也行 思路 1 ...

  5. safarai - loading.close() 无效问题

    代码环境: vue + elenment 问题描述: 上传文件时,显示loading动画:上传成功后,隐藏loading动画.window 下常用的浏览正常,safari 下的chrome浏览器(目前 ...

  6. CF 489C 暴力处理

    题意: 给你 数的长度 m, 数的每个数的和 Sum: 输出 这个数最小值 和最大值 #include<bits/stdc++.h> using namespace std; int ma ...

  7. [记录]一个有趣的url请求(nodejs)

    1 前言 IDE是webstrom,跟项目编程语言,应该没有多大关系. 2 现象 两个看起来是一样的url,但是一个能访问一个不能访问. 然后,复制url到console中发现了差异,分别是:file ...

  8. [MySQL]多表关联查询技巧

    示例表A: author_id author_name 1 Kimmy 2 Abel 3 Bill 4 Berton 示例表B: book_id author_id start_date end_da ...

  9. 基于官方mysql镜像构建自己的mysql镜像

    参考文章:https://www.jb51.net/article/115422.htm搭建步骤 1.首先创建Dckerfile: 1 2 3 4 5 6 7 8 9 10 11 12 FROM my ...

  10. awk-for循环简单用法

    文本: [root@VM_0_84_centos ~]# cat sshd.txt 1 2 3 4 5 6 7 8 9 循环打印上述文本 for 循环的固定格式   i=1设置i的初始变量  i< ...