Hadoop学习之NCDC天气数据获取

mac2022-06-30  77

期望目的

下载《Hadoop权威教程》里用到的NCDC天气数据,供后续在此数据基础上跑mapred程序。

操作过程

步骤一、编写简单的shell脚本,下载数据文件到本地文件系统

已知NCDC天气数据父目录地址为ftp://ftp.ncdc.noaa.gov/pub/data/noaa/,编写如下脚本下载1901年到1950年的天气数据:

#!/bin/sh uri=ftp://ftp.ncdc.noaa.gov/pub/data/noaa/ start=1901 end=1950 for((i=start;i<=end;i++)) do target_url="$uri$i/" echo "start download $target_url" >> ./log if [ ! -d "./$i" ] then mkdir "./$i" fi wget -r -nd -P "./$i" $target_url done

关于shell脚本,可以去这里花1、2个小时大概过一遍:https://www.runoob.com/linux/linux-shell.html

创建目录存放将要下载的数据

cd ~ mkdir ncdc cd ncdc

为上面写的脚本赋予执行权限

chmod +x download.sh

执行脚本

./download.sh

然后就是漫长的等待,由于这个ftp服务器在国外,链接也很不稳定,下载会经常超时中断,但是wget会自动重试,我们只需要不定时瞅一眼是不是还在下载就行了,

 步骤二、将天气数据合并成每年一个文件

步骤一下载下来的文件是以年份、站点为文件单位组织的数据,文件体积较小,不便于发挥HDFS的块存储特性,这一步把天气数据按年组织成一个文本文件,用到的shell脚本如下:

#!/bin/bash dir="raw" if [ ! -e $dir -o ! -d $dir ] then mkdir $dir fi for file in `ls` do if [ -d $file -a $file != $dir ] then for subfile in `ls $file` do if [[ $subfile == *.gz ]] then echo "find gzip file $subfile" filename="${dir}/${file}.txt" `gzip -d -c ${file}/${subfile} >> $filename 2>>error.log` fi done fi done

大致解释一下脚本的功能:遍历下载目录下的每个年份目录,将年份目录中的记录文件通过gzip工具解压,输出到以年份命名的txt文件中。

为该脚本赋予执行权限后,执行即可。

步骤三、将合并后的数据文件存入HDFS中

利用hdfs dfs命令,将处理好的数据存入hdfs中

hdfs dfs -mkdir /ncdc hdfs dfs -put ~/ncdc/raw/ /ncdc

总结

至此,数据整合、存储入HDFS完毕,可通过hdfs命令查看 hdfs dfs -ls -R 

转载于:https://www.cnblogs.com/duanzi6/p/11348960.html

相关资源:Hadoop权威指南(中文版)2015上传.rar
最新回复(0)