map reduce程序示例
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程序示例的更多相关文章
- Hadoop学习笔记2 - 第一和第二个Map Reduce程序
转载请标注原链接http://www.cnblogs.com/xczyd/p/8608906.html 在Hdfs学习笔记1 - 使用Java API访问远程hdfs集群中,我们已经可以完成了访问hd ...
- 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 ...
- 使用Python实现Map Reduce程序
使用Python实现Map Reduce程序 起因 想处理一些较大的文件,单机运行效率太低,多线程也达不到要求,最终采用了集群的处理方式. 详细的讨论可以在v2ex上看一下. 步骤 MapReduce ...
- 第一个map reduce程序
完成了第一个mapReduce例子,记录一下. 实验环境: hadoop在三台ubuntu机器上部署 开发在window7上进行 hadoop版本2.2.0 下载了hadoop-eclipse-plu ...
- Hadoop 使用Combiner提高Map/Reduce程序效率
众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value>键值对,再网络节点间对其进行整理(shuffle),然后使用Reducer处理数据并进行最终输出. 在上述过 ...
- Hadoop实战:使用Combiner提高Map/Reduce程序效率
好不easy算法搞定了.小数据測试也得到了非常好的结果,但是扔到进群上.挂上大数据就挂了.无休止的reduce不会结束了. .. .. .... .. ... .. ================= ...
- Hadoop Map/Reduce教程
原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/mapred_tutorial.html 目的 先决条件 概述 输入与输出 例子:WordCount v1.0 ...
- Map/Reduce 工作机制分析 --- 作业的执行流程
前言 从运行我们的 Map/Reduce 程序,到结果的提交,Hadoop 平台其实做了很多事情. 那么 Hadoop 平台到底做了什么事情,让 Map/Reduce 程序可以如此 "轻易& ...
- Map/Reduce个人实战--生成数据测试集
背景: 在大数据领域, 由于各方面的原因. 有时需要自己来生成测试数据集, 由于测试数据集较大, 因此采用Map/Reduce的方式去生成. 在这小编(mumuxinfei)结合自身的一些实战经历, ...
随机推荐
- springboot+layui实现PC端用户的增删改查 & 整合mui实现app端的自动登录和用户的上拉加载 & HBuilder打包app并在手机端下载安装
springboot整合web开发的各个组件在前面已经有详细的介绍,下面是用springboot整合layui实现了基本的增删改查. 同时在学习mui开发app,也就用mui实现了一个简单的自动登录和 ...
- SpringSecurity自定义用户认证逻辑
⒈处理用户信息获取逻辑 用户信息的获取逻辑是被SpringSecurity封装到UserDetailsService接口里面的 package org.springframework.security ...
- go 单引号,双引号,反引号区别
go里面双引号是字符串,单引号是字符,不存在单引号字符串. 但有反引号字符串,就是esc键下面1键左边tab键上面那个键,区别是反引号字符串允许换行符
- PTA L1题目合集(更新至2019.3)
L1-001 Hello World (5 分) 链接:https://pintia.cn/problem-sets/994805046380707840/problems/9948051471320 ...
- use snippet save dom to excel
1.打开贴吧 http://tieba.baidu.com/f?ie=utf-8&kw=python&fr=search 2.打开console执行下面命令 Array.prototy ...
- EMOS之邮件服务器
作者:邓聪聪 EMOS一键自动安装镜像,邮件服务器配置
- 在 mingw32 上编译 libvpx 1.7.0 时的注意事项
in the vp8/common/theading.h Just need to add 1 line:#include <sys/types.h>before the last occ ...
- Android-创建一个简单的用户接口-(补day2内容)
如果按照之前的布局设置,那么输入框和按钮组件的大小就会是刚好满足它们的内容的.如图1. 图1.输入框和按钮宽度设置为”wrap_content” 这样的设置是可以满足按钮的,但不能满足输入框的要求,因 ...
- Vue 介绍,优点,实例
一 认识vue 1.什么是vue 以数据驱动的web渐进式框架 三大主流框架: Angular React Vue 设计模式:MVVM 2.vue优点 - 以数据驱动,不直接操作DOM,效率高- 单页 ...
- php7静态方法的链式调用
2018-1-11 20:25:48 星期四 情景: 以前想要链式调用必须先 new 一个对象, 然后 $obj->aa()->bb()... 现在PHP7 (php7.0.13 php ...