hadoop mapreduce 计算平均气温的代码,绝对原创
1901 46
1902 21
1903 48
1904 33
1905 43
1906 47
1907 31
1908 28
1909 26
1910 35
1911 30
1912 16
1913 29
1914 29
1915 5
1916 21
1917 22
1918 31
1919 27
1920 43
1921 34
1922 27
1923 26
以上为结果
package com.teset;
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.Path;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class AvgTemprature extends Configured implements Tool {
public static class AvgMapper extends
Mapper<LongWritable, Text, Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// map处理数据
String str = value.toString();
String year = null;
int temprature = 0;
StringTokenizer tokenstr = new StringTokenizer(str);
int i = 0;
while (tokenstr.hasMoreTokens()) {
String tempstr = tokenstr.nextToken();
i++;
if (i == 1) {
year = tempstr;
continue;
} else if (i == 5 && Integer.parseInt(tempstr) != -9999) {
temprature = Integer.parseInt(tempstr);
context.write(new Text(year), new IntWritable(temprature));
break;
}
}
}
}
public static class AvgReducer extends
Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
int sum=0;
int count=0;
for (IntWritable value : values) {
sum=sum+value.get();
count++;
}
if (count!=0){
context.write(key, new IntWritable(sum/count));
}
}
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new AvgTemprature(), args);
System.exit(res);
}
@Override
public int run(String[] arg0) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf, "AvgTem");// 任务名
job.setJarByClass(AvgTemprature.class);// 指定class
// 输入和输出流
job.setMapperClass(AvgMapper.class);// map
job.setReducerClass(AvgReducer.class);
FileInputFormat.addInputPath(job, new Path(arg0[0]));
FileOutputFormat.setOutputPath(job, new Path(arg0[1]));
job.setCombinerClass(AvgReducer.class);
job.setOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.waitForCompletion(true);
return job.isSuccessful() ? 0 : 1;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
hadoop mapreduce 计算平均气温的代码,绝对原创的更多相关文章
- Hadoop—MapReduce计算气象温度
Hadoop-MapReduce计算气象温度 1 运行环境说明 1.1 硬软件环境 主机操作系统:Mac OS 64 bit ,8G内存 虚拟软件:Parallers Desktop12 虚拟机操作系 ...
- mapreduce实战:统计美国各个气象站30年来的平均气温项目分析
气象数据集 我们要写一个气象数据挖掘的程序.气象数据是通过分布在美国各地区的很多气象传感器每隔一小时进行收集,这些数据是半结构化数据且是按照记录方式存储的,因此非常适合使用 MapReduce 程序来 ...
- 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行
[TOC] 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行 程序源码 import java.io.IOException; import java.util. ...
- (第4篇)hadoop之魂--mapreduce计算框架,让收集的数据产生价值
摘要: 通过前面的学习,大家已经了解了HDFS文件系统.有了数据,下一步就要分析计算这些数据,产生价值.接下来我们介绍Mapreduce计算框架,学习数据是怎样被利用的. 博主福利 给大家赠送一套ha ...
- Hadoop第5周练习—MapReduce计算气象温度等例子
:对云计算的看法 内容 :使用MapReduce求每年最低温度 内容 :求温度平均值能使用combiner吗? 内容 :使用Hadoop流求最高温度(awk脚本) 内容 :使用Hadoop流求最高温度 ...
- hadoop 第一个 mapreduce 程序(对MapReduce的几种固定代码的理解)
1.2MapReduce 和 HDFS 是如何工作的 MapReduce 其实是两部分,先是 Map 过程,然后是 Reduce 过程.从词频计算来说,假设某个文件块里的一行文字是”Thisis a ...
- Hadoop 学习笔记 (十一) MapReduce 求平均成绩
china:张三 78李四 89王五 96赵六 67english张三 80李四 82王五 84赵六 86math张三 88李四 99王五 66赵六 77 import java.io.IOEx ...
- MapReduce计算之——hadoop中的Hello World
1. 启动集群 2. 创建input路径(有关hadoop 的命令用 "hadoop fs"),input路径并不能在系统中查找到,可以使用 “hadoop fs -ls /” ...
- Hadoop MapReduce编程入门案例
Hadoop入门例程简介 一个.有些指令 (1)Hadoop新与旧API差异 新API倾向于使用虚拟课堂(象类),而不是接口.由于这更easy扩展. 比如,能够无需改动类的实现而在虚类中加入一个方法( ...
随机推荐
- CSS选择器(三)
九.CSS 伪类 (Pseudo-classes) CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪类 (Pseudo-classes)实例: 1.向文档中的超链接添加不同的颜色. <s ...
- Example 1 - XY plots
http://www.ncl.ucar.edu/Document/Manuals/Getting_Started/Examples/gsun01n.shtml Frame 1 Frame 2 Fram ...
- 【Flask】视图高级
# 视图高级笔记:### `add_url_rule(rule,endpoint=None,view_func=None)`这个方法用来添加url与视图函数的映射.如果没有填写`endpoint`,那 ...
- sqoop job从创建到执行
在学习sqoop job之前,最好先学习一下sqoop命令的导入导出 sqoop 使用 import 将 mysql 中数据导入到 hive sqoop 使用 import 将 mysql 中数据导入 ...
- hdu 4542 小明系列故事——未知剩余系 反素数 + 打表
小明系列故事——未知剩余系 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Prob ...
- jupyter- 运维
jupyter运维常见CLI 查看安装的内核和位置 jupyter kernelspec list anaconda列出所有的环境 conda info -e
- ubantu删除文件(夹)
格式:rm -rf 目录名字 -r 就是向下递归,不管有多少级目录,一并删除 -f 就是直接强行删除,不作任何提示的意思 名称 rm - 移除文件或者目录 概述 rm [选项]... 文件列表... ...
- DIV+CSS IE6/IE7/IE8/FF兼容问题大全
1. [代码][CSS]代码 1, FF下给 div 设置 padding 后会导致 width 和 height 增加, 但IE不会.(可用!important解决) 2, 居中问题. 1).垂直居 ...
- Codeforces 897C Nephren gives a riddle:模拟【珂学】
题目链接:http://codeforces.com/contest/897/problem/C 题意: 给你一些字符串: A: [What are you doing at the end of t ...
- Http请求和响应
Http请求和响应 Http协议对浏览器发出的Request格式以及对Web服务器发出的Response格式有具体的规定. 请求部分由三部分组成: Requset line:请求行,位于第一行 Req ...