下载气象数据并自动解压整合

首先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





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值