首先hadoop权威指南书中的气象数据的地址为:ftp://ftp.ncdc.noaa.gov/pub/data/gsod/YEAR/gsod_YEAR.tar
其中YEAR是年份,从1901年到1928年的数据里面都是空的,不用下载了。
使用java程序进行下载:
开始用了多线程下载---然后第四个线程启动的时候就爆了,显示 超过最大connection数(530) ,可能是全球一起超过的吧....
然后将多线程改成了单线程,有时下载10左右就不动了,感觉是网络问题,毕竟那个网站进去就很慢。
java下载代码:
package file; import javax.swing.plaf.SliderUI; public class Main { public static void main(String[] args) throws Exception{ FileThread file = new FileThread(); for (int i=1929;i<=2000;i++){ // new FileThread(new Integer(i).toString()).start(); file.setYear(new Integer(i).toString()); file.run(); } } }
package file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class FileThread extends Thread{ String year; public FileThread(){} public FileThread(String year){ this.year = year; } public void setYear(String year){ this.year = year; } @Override public void run() { InputStream input = null; FileOutputStream output = null; try { URL url = new URL("ftp://ftp.ncdc.noaa.gov/pub/data/gsod/"+year+"/gsod_"+year+".tar"); input = url.openStream(); output = new FileOutputStream(new File("F:/gsod/gsod_"+year+".tar")); int ls = 0; byte b[] = new byte[204800]; while ((ls = input.read(b, 0, 204800))> -1){ output.write(b, 0, ls); output.flush(); } output.flush(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if (output!=null) output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (input!=null) input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
然后需要解压,合并,写了一个shell脚本:
#!/bin/bash read x read y read filename for ((i=x;i<=y;i++)) do tar -xvf *${i}.tar gzip -d *.op.gz cat *.op >> ${filename} rm -f *.op done