本程序主要实现的是下载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);
}
}
扫描二维码关注公众号,回复:
5118457 查看本文章