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 计算平均气温的代码,绝对原创的更多相关文章

  1. Hadoop—MapReduce计算气象温度

    Hadoop-MapReduce计算气象温度 1 运行环境说明 1.1 硬软件环境 主机操作系统:Mac OS 64 bit ,8G内存 虚拟软件:Parallers Desktop12 虚拟机操作系 ...

  2. mapreduce实战:统计美国各个气象站30年来的平均气温项目分析

    气象数据集 我们要写一个气象数据挖掘的程序.气象数据是通过分布在美国各地区的很多气象传感器每隔一小时进行收集,这些数据是半结构化数据且是按照记录方式存储的,因此非常适合使用 MapReduce 程序来 ...

  3. 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行

    [TOC] 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行 程序源码 import java.io.IOException; import java.util. ...

  4. (第4篇)hadoop之魂--mapreduce计算框架,让收集的数据产生价值

    摘要: 通过前面的学习,大家已经了解了HDFS文件系统.有了数据,下一步就要分析计算这些数据,产生价值.接下来我们介绍Mapreduce计算框架,学习数据是怎样被利用的. 博主福利 给大家赠送一套ha ...

  5. Hadoop第5周练习—MapReduce计算气象温度等例子

    :对云计算的看法 内容 :使用MapReduce求每年最低温度 内容 :求温度平均值能使用combiner吗? 内容 :使用Hadoop流求最高温度(awk脚本) 内容 :使用Hadoop流求最高温度 ...

  6. hadoop 第一个 mapreduce 程序(对MapReduce的几种固定代码的理解)

    1.2MapReduce 和 HDFS 是如何工作的 MapReduce 其实是两部分,先是 Map 过程,然后是 Reduce 过程.从词频计算来说,假设某个文件块里的一行文字是”Thisis a ...

  7. Hadoop 学习笔记 (十一) MapReduce 求平均成绩

    china:张三 78李四 89王五 96赵六 67english张三 80李四 82王五    84赵六 86math张三 88李四 99王五 66赵六 77 import java.io.IOEx ...

  8. MapReduce计算之——hadoop中的Hello World

    1.  启动集群 2.  创建input路径(有关hadoop 的命令用 "hadoop fs"),input路径并不能在系统中查找到,可以使用 “hadoop fs -ls /” ...

  9. Hadoop MapReduce编程入门案例

    Hadoop入门例程简介 一个.有些指令 (1)Hadoop新与旧API差异 新API倾向于使用虚拟课堂(象类),而不是接口.由于这更easy扩展. 比如,能够无需改动类的实现而在虚类中加入一个方法( ...

随机推荐

  1. Linux expect介绍和用法

    expect时用与提供自动交互的工具.比如如果想要用ssh登陆服务器,每次都输入密码你觉得麻烦,那你就可以使用expect来做自动交互,这样的话就不用每次都输入密码了. 先看例子: #!/usr/bi ...

  2. myBatis中使用Map进行遍历

    myBatis中使用Map获取key, value的方式 第一次的配置 <trim prefix=" tags =case" suffix="end"&g ...

  3. docker swarm部署spring cloud服务

    一.准备docker swarm的集群环境 ip 是否主节点   192.168.91.13 是   192.168.91.43 否   二.准备微服务 ①eureka服务 application.y ...

  4. 图片加载控件Fresco

    使用教程:https://www.fresco-cn.org/docs/index.html  https://github.com/facebook/fresco application初始化fre ...

  5. ios 表情编码

    感受 :可以做自定义键盘时候用  很方便 还可以在textView里面看到 用户体验很好~ 但是要和服务器管理员协商好,做好解析转码工作,不然网页上是不显示的. ios表情编码 在ios中可以使用可爱 ...

  6. 动态添加select的option

    动态给select标签添加option,结合前人经验以及自己经验,现在总结三种方法供大家参考,一起交流学习! 首先是定义的select元素: //根据ID获得select元素var mySelect ...

  7. c++学习笔记(网上资料)

                                    C++笔记       2007-3-22 1. 程序 —— 可执行文件,人发送给计算机的一组指令.         硬件指令是二进制, ...

  8. 【leetcode刷题笔记】Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题解:因为题 ...

  9. Spring Cloud之Zuul负载均衡

    Zuul网关默认是实现负载均衡的,不需要任何配置.默认开启ribbon效果的 可以启启动两个服务端口,访问下.

  10. EntityFramework 学习 一 Entity Framework结构体系

    Entity Framework 架构 EDM(Entity Data Model)EDM由3个主要部分组成 Conceptual model , Mapping and Storage model. ...