您的位置:首页 > 编程语言 > Java开发

java从hdfs中下载文件到本地

2017-08-15 14:10 218 查看
hdfs 在linux 搭建好,之后启动hdfs

hdfs核心机制看可这 篇
http://www.cnblogs.com/thinkpad/p/5173705.html
接下来新建java项目 引入HDFS所需要的包,这里的jar包可以从hadoop安装包里拿

解压haddoop gz进入 hadoop-2.4.1\share\hadoop\hdfs



进入hadoop-2.4.1\share\hadoop\common



导入java项目 编写如下demo

package hdfs.zanghan;

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 HdfsUtils {

public static void main(String[] args) throws IOException {

//上传文件到HDFS
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(conf);
Path src=new Path("hdfs://zanghan02:9000/aa.txt");
FSDataInputStream inputStream=fs.open(src);
FileOutputStream os=new FileOutputStream("F://aa.txt");
IOUtils.copy(inputStream,os);

}
}


首先你现在的东西在hdfs上是有的

conf配置文件从etc/hadoop/拿。
core-site.xml

hafs-site.xml

放到src目录下



运行,查看F://aa.txt文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: