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

IDEA使用java远程链接HDFS

2018-01-19 15:37 183 查看
1、将hadoop集群中任一节点下的core-site.xml和hdfs-site.xml添加到IDEA工程的resources目录下
2、修改本机hosts文件:C:\Windows\System32\drivers\etc\hosts,设置hadoop域名和ip地址的映射。
3、pom.xml文件添加如下依赖:hadoop-common、hadoop-hdfs、hadoop-mapreduce-client-core
4、编写测试代码:

import java.net.URI;  
  
import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.fs.FSDataInputStream;  
import org.apache.hadoop.fs.FileSystem;  
import org.apache.hadoop.fs.Path;  
import org.apache.hadoop.io.IOUtils;  
  
  
public class Test {  
    public static void main(String[] args) throws Exception {  
        String uri="hdfs://IP/test/test.txt";  
        Configuration configuration=new Configuration();  
        FileSystem fileSystem=FileSystem.get(URI.create(uri), configuration);  
        FSDataInputStream in=null;  
        in=fileSystem.open(new Path(uri));   
        IOUtils.copyBytes(in, System.out, 4096, false);  
        IOUtils.closeStream(in);  
    }  
      


             
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: