ncdc 数据分析报告范文是什么数据分析报告范文

文档内容:
  1:下载《hadoop权威指南》中的气象数据
  2:对下载的气象数据归档整理并读取数据
  3:对气象数据进行map reduce进行处理
关键词:《Hadoop权威指南》气象数据  map reduce  python  matplotlib可视化
一:下载《hadoop权威指南》一书中的气象数据
  《hadoop权威指南》一书中的气象数据位于&,
  新建 getdata.py文件,&并加入如下代码:
1 #http://my.oschina.net/chyileon/blog/134915
2 import urllib
3 import urllib2
4 from bs4 import BeautifulSoup
5 import re
6 import os
7 import shutil
9 def getdata():
year = 1901
endYear = 1921
urlHead = 'http://ftp3.ncdc.noaa.gov/pub/data/noaa/'
while year & endYear:
if os.path.isdir(str(year)):
shutil.rmtree(str(year))
os.mkdir(str(year))
page = urllib2.urlopen(urlHead+str(year))
soup = BeautifulSoup(page, from_encoding="gb18030")
for link in soup.findAll('a'):
if link.getText().find('.gz') != -1:
filename = link.getText()
urllib.urlretrieve(urlHead+str(year)+'/'+filename, str(year)+'/'+filename)
30 def main():
33 if __name__=="__main__":
  运行getdata.py,将在当前目录下生成数据文件
二:对下载的气象数据归档整理并读取数据
  说明:上一步骤在当前目录下生成【1901】~【1921】 共20文件,文件里是压缩的气象数据,本步骤知识将数据移动data文件夹下 
  新建 movedata.py文件,&并加入如下代码:
1 import os
2 import shutil
4 def movedata():
curpath = os.getcwd()
list = os.listdir(curpath)
datapath = os.path.join(curpath, "data")
print(datapath)
for line in list:
filepath = os.path.join(curpath, line)
if os.path.isdir(filepath):
shutil.move(filepath,datapath)
15 def main():
movedata()
18 if __name__=="__main__":
三:对气象数据进行map reduce进行处理
  说明:这里要读取文件中的数据内容,并通过将数据map reduce 化获取每年的最高、低温度
  1: 将文件中的数据内容逐行读出
    新建reader.py文件,并加入如下代码:
1 import os
2 import gzip
4 def reader():
curpath = os.getcwd()
datapath = os.path.join(curpath, r"data")
for yearlist in os.listdir(datapath):
oneyearpath = os.path.join(datapath, yearlist)
datalist = os.listdir(oneyearpath)
for line in datalist:
onedatapath = os.path.join(oneyearpath, line)
with gzip.open(onedatapath, 'rb') as pf:
print (pf.read())
17 def main():
20 if __name__=="__main__":
    测试上面代码:在命令行运行 reader.py,查看输出
  2:通过mapper方法把数据处理成 "year \n temperature"的输出形式,如 "1901  242",其中&242 表示温度为24.2度
   新建mapper.py文件,并加入如下代码: 
1 import sys
3 def mapper(inlist):
for line in inlist:
if len(line) & 92:
year = (line[15:19])
if line[87] == '+':
temperataure = line[88:92]
temperataure = line[87:92]
print year, temperataure
13 def main(inlist):
mapper(inlist)
16 if __name__=="__main__":
inlist = []
for line in sys.stdin:
inlist.append(line)
main(inlist)
  测试上面代码:在命令行运行 &reader.py | mapper.py ,查看输出。(注:这是是利用管道,把reader.py的输出作为mapper.py的输入)
  3:通过reducer方法将mapper的输出数据整理并计算每年的最高、低温度,并输出
   新建reducer.py文件,并加入如下代码:
1 import sys
3 def reducer(inlist):
cur_year = None
maxtemp = None
mintemp = None
for line in inlist:
year, temp = line.split()
temp = int(temp)
except ValueError:
if cur_year == year:
if temp & maxtemp:
maxtemp = temp
if temp & mintemp:
mintemp = temp
if cur_year != None:
print cur_year, maxtemp, mintemp
cur_year = year
maxtemp = temp
mintemp = temp
print cur_year, maxtemp, mintemp
26 def main(inlist):
reducer(inlist)
29 if __name__=="__main__":
inlist = []
for line in sys.stdin:
inlist.append(line)
main(inlist)
  测试上面代码:在命令行运行 &reader.py | mapper.py | reducer.py,查看输出。
  4:使用matplotlib对每年的最高、低数据进行可视化
    新建drawer.py文件,并加入如下代码:
1 import sys
2 import matplotlib.pyplot as plt
4 def drawer(inlist):
yearlist = []
maxtemplist = []
mintemplist = []
for line in inlist:
year, maxtemp, mintemp = line.split()
year = int(year)
maxtemp = int(maxtemp) / 10.
if(maxtemp) & 50:
maxtemp = 50
mintemp = int(mintemp) / 10.
except ValueError:
yearlist.append(year)
maxtemplist.append(maxtemp)
mintemplist.append(mintemp)
plt.plot(yearlist, maxtemplist, 'bd--')
plt.plot(yearlist, mintemplist, 'rp:')
plt.xlim()
plt.ylim(-60, 80)
plt.title('min-max temperature for ')
plt.xlabel('year')
plt.ylabel('temperature')
plt.legend(('max temp','min temp'), loc='upper right')
plt.show()
print(yearlist, maxtemplist, mintemplist)
32 def main(inlist):
drawer(inlist)
35 if __name__=="__main__":
inlist = []
for line in sys.stdin:
inlist.append(line)
main(inlist)
  测试上面代码:在命令行运行 &reader.py | mapper.py | reducer.py | drawer.py,查看输出。
  显示效果如下图:(注:在前面处理的数据中, 可能由于采样的错误,会有出现999.9度的最高温度, 显然不符常理。在本例中,没有对此种错误进行深究,一致将超度50度的温度处理成50度)
  1. 本例中,其实第二步&对下载的气象数据归档整理并读取数据 是多余的, 可以直接在第一步中修改文件存储目录跳过第二步。但为了熟悉python对文件的操作,还是将第二步的代码保留了下来。
  2. 本例中,代码能运行得到实验目标,但并为对代码进行优化。请读者根据需要自行更改。
  3. python代码的一大特点就是看起来像伪代码,又本例python代码比较简单,故没有给出注释。
阅读(...) 评论()Code is Poetry-我们不是码农,我们是诗人,我们书写代码,我们书写诗篇
hadoop-hive查询ncdc天气数据实例
使用hive查询ncdc天气数据
在hive中将ncdc天气数据导入,然后执行查询shell,可以让hive自动生成mapredjob,快速去的想要的数据结果。
1. 在hive中创建ncdc表,这个表用来存放ncdc的数据
create table ncdc (
year string,
month string,
data string,
time string,
air string,
)ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;
TERMINATED
BY '\t' 是说这个表子,使用tab键分割。
2. 处理原始的数据,因为原始的数据是这样的:
中间不是制表符,而是空格键,所以写了一个java程序,将文件夹中的 所有的数据统计,转换到一个文件中。
import java.io.BufferedR
import java.io.BufferedW
import java.io.F
import java.io.FileR
import java.io.FileW
import java.io.IOE
import java.util.L
import java.util.StringT
public class removeAnno {
static String ofile="summary";
static BufferedWriter bw=
public static void main(String[] args) throws Exception {
bw = new BufferedWriter(new FileWriter(ofile));
File file1 = new File("C:\\Users\\Administrator\\ncdc2");
File[] listfile = file1.listFiles();
for (int i=0;i&listfile.i++){
rm("C:\\Users\\Administrator\\ncdc2\\"+listfile[i].getName());
//System.out.println(listfile[i].getName());
static void rm(String filename) throws Exception{
File file = new File(filename);
BufferedReader br = new BufferedReader(new FileReader(file));
String str=br.readLine();
while(str!=null){
//进行分割处理
String tmp="";
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()){
tmp=tmp+st.nextToken()+"\t";
bw.write(tmp+"\n");
bw.flush();
str=br.readLine();
3. 导入数据到hive中
load data local inpath '/opt/software/ncdc/summary'
into table ncdc
4. 查询数据
可以查询每一年的平均气温,最高气温,最低气温等等,也可以使用分组函数,和MySQL操作差不多
select year,avg(air) fro
hadoop学习笔记(七)——hadoop权威指南中天气数据运行
Hadoop 实例1---通过采集的气象数据分析每年的最高温度
Hadoop经典案例Spark实现(一)——通过采集的气象数据分析每年的最高温度
没有更多推荐了,
(window.slotbydup=window.slotbydup || []).push({
id: '5865575',
container: s,
size: '300,250',
display: 'inlay-fix'2017年03月
确定要删除当前文章?

我要回帖

更多关于 ncdc 的文章

 

随机推荐