用mapreduce 处理气象数据集
用mapreduce 处理气象数据集
编写程序求每日最高最低气温,区间最高最低气温
气象数据集下载地址为:ftp://ftp.ncdc.noaa.gov/pub/data/noaa
- 按学号后三位下载不同年份月份的数据(例如201506110136号同学,就下载2013年以6开头的数据,看具体数据情况稍有变通)
- 解压数据集,并保存在文本文件中
- 对气象数据格式进行解析
- 编写map函数,reduce函数
- 将其权限作出相应修改
- 本机上测试运行代码
- 放到HDFS上运行
- 将之前爬取的文本文件上传到hdfs上
- 用Hadoop Streaming命令提交任务
- 查看运行结果
本次的所有操作均在当前用户目录下的/temp/2018-05-09中
通过wget下载压缩文件,命令如下:
wget -drc --accept-regex=REGEX -P data ftp://ftp.ncdc.noaa.gov/pub/data/noaa/2015/6*
在这之前,需要配置好环境,在.bashrc中加入下面的命令
export PATH=$PATH:/usr/local/hbase/bin:/usr/local/hadoop/sbin:/usr/local/hadoop/bin
export HADOOP_HOME=/usr/local/hadoop
export STREAM=$HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar
下载后解压,之后启动hdfs,将解压文件放入系统中,命令如下
start-dfs.sh
hdfs dfs -mkdir weather_data
hdfs dfs -put weather.txt weather_data/
文件放入系统后可以编写mapper.py了,主要代码如下:
import sys
for line in sys.stdin:
line = line.strip()
print('%s\t%d' % (line[15:23], int(line[87:92])))
reducer.py了,主要代码如下:
from operator import itemgetter
import sys
current_date = None
current_temperature = 0
date = None
for line in sys.stdin:
line = line.strip()
date, temperature = line.split('\t', 1)
try:
temperature = int(temperature)
except ValueError:
continue
if current_date == date:
if current_temperature < temperature:
current_temperature = temperature
else:
if current_date:
print('%s\t%d' % (current_date, current_temperature))
current_temperature = temperature
current_date = date
if current_date == date:
print('%s\t%d' % (current_date, current_temperature))
上面的reducer是求出最高气温,求出最低只需要将
if current_temperature < temperature:改为 if current_temperature > temperature:
这里测试运行mapper和reducer,命令如下:
chmod a+x mapper.py
chmod a+x reducer.py
cat test.txt | python mapper.py | python reducer.py
test.txt中包含了部分的天气数据
下面是运行截图:

运行成功后可编写run.sh
hadoop jar $STREAM \
-D stream.non.zero.exit.is.failure=false \
-file /home/hadoop/temp/2018-05-09/mapper.py \
-mapper 'python /home/hadoop/temp/2018-05-09/mapper.py' \
-file /home/hadoop/temp/2018-05-09/reducer.py \
-reducer 'python /home/hadoop/temp/2018-05-09/reducer.py' \
-input /user/hadoop/weather_data/*.txt \
-output /user/hadoop/weather_output
运行run.sh
source run.sh
最后的运行结果通过cat打印截图:

/temp下的文件在链接中下载
用mapreduce 处理气象数据集的更多相关文章
- MapReduce处理气象数据
老师:MissDu 提交作业 1. 用Python编写WordCount程序并提交任务 程序 WordCount 输入 一个包含大量单词的文本文件 输出 文件中每个单词及其出现次数(频数),并按照单 ...
- Hadoop第5周练习—MapReduce计算气象温度等例子
:对云计算的看法 内容 :使用MapReduce求每年最低温度 内容 :求温度平均值能使用combiner吗? 内容 :使用Hadoop流求最高温度(awk脚本) 内容 :使用Hadoop流求最高温度 ...
- Hadoop—MapReduce计算气象温度
Hadoop-MapReduce计算气象温度 1 运行环境说明 1.1 硬软件环境 主机操作系统:Mac OS 64 bit ,8G内存 虚拟软件:Parallers Desktop12 虚拟机操作系 ...
- 《Hadoop权威指南》(Hadoop:The Definitive Guide) 气象数据集下载脚本
已过时,无法使用 从网上找到一个脚本,修改了一下 #!/bin/bash CURRENT_DIR=$(cd `dirname $0`; pwd) [ -e $CURRENT_DIR/ncdc ] || ...
- Hadoop和MapReduce初识
我们生活在大数据时代!!!微博.微信.云存储等大数据的需求,Hadoop由此诞生. 以下面部分数据为例: 1)Facebook存储着约100亿张照片,约1PB存储容量: 2)纽约证券交易所每天产生1T ...
- mapreduce实战:统计美国各个气象站30年来的平均气温项目分析
气象数据集 我们要写一个气象数据挖掘的程序.气象数据是通过分布在美国各地区的很多气象传感器每隔一小时进行收集,这些数据是半结构化数据且是按照记录方式存储的,因此非常适合使用 MapReduce 程序来 ...
- 关于 MapReduce
继续摘抄<Hadoop 权威指南>第二章,跳过不少于我复杂的东西,但依然是捉急的效率,开始觉得看不完另外一本全英文的书,大概每天要看5页吧... 以上. MapReduce 是一种可用于数 ...
- MapReduce原理及操作
注意:本实验是对前述实验的延续,如果直接点开始实验进入则需要按先前学习的方法启动hadoop 部署节点操作系统为CentOS,防火墙和SElinux禁用,创建了一个shiyanlou用户并在系统根目录 ...
- Hadoop阅读笔记(一)——强大的MapReduce
前言:来园子已经有8个月了,当初入园凭着满腔热血和一脑门子冲动,给自己起了个响亮的旗号“大数据 小世界”,顿时有了种世界都是我的,世界都在我手中的赶脚.可是......时光飞逝,岁月如梭~~~随手一翻 ...
随机推荐
- 多线程学习系列二(使用System.Threading)
一.什么是System.Threading.Thread?如何使用System.Threading.Thread进行异步操作 System.Threading.Thread:操作系统实现线程并提供各种 ...
- 学习 JavaScript (五)核心概念:语句
语句 语句被称作是流控制语句,通常有标志性的一个或者多个关键字,if . do-while. while.for. for-in. label. break.continue.with.switch. ...
- Java笔记(day12)
包: 对类文件进行分类管理:给类提供多层命名(名称)空间:写在程序文件的第一行:类名的全称是 包名.类名包也是一种封装形式: package protected必须是成为其子类,才能继承import导 ...
- 新手必看!Office Web Apps 2013 安装与配置(实战)
分享人:广州华软 星尘 一. 前言 Office Web Apps Server 是Office 服务器产品,它可提供在Sharepoint 2013网站中在线浏览和编辑 Word.PowerPoin ...
- Oracle数据库升级注意事项
1 备份配置参数 数据库升级前的配置参数要备份,如PGA大小 这样数据库升级后还可以升级前的配置,而不至于使用安装升级时的默认配置 2 检查版本兼容 确认数据库升级后是否对生产环境上的代码有影响,如果 ...
- Sql Server 复制数据库
确实很实用 https://www.cnblogs.com/ggll611928/p/7451558.html
- linux 进程概念
1,pcb:进程控制块结构体:/usr/src/linux-headers-4.15.0-29/include/linux/sched.h 进程id:系统中每个进程有唯一的id,在c语言中用pid_t ...
- July 08th. 2018, Week 28th. Sunday
Make each day your masterpiece. 让生命中的每一天都成为杰作. From John Wooden. Try to focus on what we are doing r ...
- Storm入门(十三)Storm Trident 教程
转自:http://blog.csdn.net/derekjiang/article/details/9126185 英文原址:https://github.com/nathanmarz/storm/ ...
- Java三种方式实现栈和队列
栈:LIFO(后进先出) 队列:FIFO(先进先出) 1.栈:LIFO(后进先出) 1.1.栈的顺序存储结构实现: /** * 基于数组实现的顺序栈 * @param <E> */ pub ...