我是靠谱客的博主 怡然豌豆,这篇文章主要介绍用mapreduce 处理气象数据集,现在分享给大家,希望可以做个参考。

用mapreduce 处理气象数据集
编写程序求每日最高最低气温,区间最高最低气温

气象数据集下载地址为:ftp://ftp.ncdc.noaa.gov/pub/data/noaa
- 按学号后三位下载不同年份月份的数据(例如201506110136号同学,就下载2013年以6开头的数据,看具体数据情况稍有变通)
- 解压数据集,并保存在文本文件中
- 对气象数据格式进行解析
- 编写map函数,reduce函数
- 将其权限作出相应修改
- 本机上测试运行代码
- 放到HDFS上运行
- 将之前爬取的文本文件上传到hdfs上
- 用Hadoop Streaming命令提交任务
- 查看运行结果

本次的所有操作均在当前用户目录下的/temp/2018-05-09
通过wget下载压缩文件,命令如下:

复制代码
1
wget -drc --accept-regex=REGEX -P data ftp://ftp.ncdc.noaa.gov/pub/data/noaa/2015/6*

在这之前,需要配置好环境,在.bashrc中加入下面的命令

复制代码
1
2
3
4
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,将解压文件放入系统中,命令如下

复制代码
1
2
3
start-dfs.sh hdfs dfs -mkdir weather_data hdfs dfs -put weather.txt weather_data/

文件放入系统后可以编写mapper.py了,主要代码如下:

复制代码
1
2
3
4
5
import sys for line in sys.stdin: line = line.strip() print('%st%d' % (line[15:23], int(line[87:92])))

reducer.py了,主要代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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('%st%d' % (current_date, current_temperature)) current_temperature = temperature current_date = date if current_date == date: print('%st%d' % (current_date, current_temperature))

上面的reducer是求出最高气温,求出最低只需要将
if current_temperature < temperature:改为if current_temperature > temperature:

这里测试运行mapper和reducer,命令如下:

复制代码
1
2
3
4
chmod a+x mapper.py chmod a+x reducer.py cat test.txt | python mapper.py | python reducer.py

test.txt中包含了部分的天气数据
下面是运行截图:
enter description here

运行成功后可编写run.sh

复制代码
1
2
3
4
5
6
7
8
9
10
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

复制代码
1
source run.sh

最后的运行结果通过cat打印截图:
enter description here

/temp下的文件在链接中下载

最后

以上就是怡然豌豆最近收集整理的关于用mapreduce 处理气象数据集的全部内容,更多相关用mapreduce内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部