java实现将hdfs上的文件下载到本地系统上

版权声明:学习交流为主,未经博主同意禁止转载,禁止用于商用。 https://blog.csdn.net/u012965373/article/details/51765002
/***
 * 将hdfs上的文件下载到本地系统上1.0版本
 */
package itcast;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class HDFSDemo {
	public static void main(String[] args) throws IOException, URISyntaxException {
		// TODO Auto-generated method stub
		FileSystem fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration());
		InputStream in = fs.open(new Path("/Demo"));
		OutputStream out = new FileOutputStream("E://Demo");
		IOUtils.copyBytes(in, out, 4096, true);
	}
}



/***
 * 将hdfs上的文件下载到本地系统上2.0版本
 */
package itcast;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;

public class HDFSDemo {
	FileSystem fs = null;
	
	@Before
	public void init() throws IOException, URISyntaxException{
		fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration());
		
	}
	public static void main(String[] args) throws IOException, URISyntaxException {
		// TODO Auto-generated method stub
		FileSystem fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration());
		InputStream in = fs.open(new Path("/Demo"));
		OutputStream out = new FileOutputStream("E://Demo");
		IOUtils.copyBytes(in, out, 4096, true);
	}
}


展开阅读全文

没有更多推荐了,返回首页