hadoop------Linux下开发

本程序主要实现的是下载hdfs中的文件

一.导入jar包

需要导入common和hdfs中所有的lib中的包和各自的核心包

二.导入core-site.xml和hdfs-site.xml文件(或者可用set方法指定)

三.编写程序

package hdfs;

import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class HdfsUtil {
	public static void main(String[] args) throws IOException {
		
		//download file from hdfs
		
		//create Configuration   ,for load core-site.xml and hdfs-site.xml
		Configuration conf = new Configuration();
		
		//create filesystem 
		FileSystem fs = FileSystem.get(conf);
		
		//appoint path
		Path path = new Path("hdfs://hadoop-01:9000/jdk-7u65-linux-i586.tar.gz");
		
		//according path to open inputstream
		FSDataInputStream inputStream = fs.open(path);
		
		
		//create local outputstream   ,write remote file to local 
		FileOutputStream outputStream = new FileOutputStream("/home/hadoop/download/jdk.tar.gz"); 
		
		//use tool to write
		IOUtils.copy(inputStream, outputStream);

	}

}

猜你喜欢

转载自blog.csdn.net/ltblll/article/details/85318960