IDEA使用java远程链接HDFS

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、编写测试代码:

  1. import java.net.URI;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.FSDataInputStream;  
  5. import org.apache.hadoop.fs.FileSystem;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.io.IOUtils;  
  8.   
  9.   
  10. public class Test {  
  11.     public static void main(String[] args) throws Exception {  
  12.         String uri="hdfs://IP/test/test.txt";  
  13.         Configuration configuration=new Configuration();  
  14.         FileSystem fileSystem=FileSystem.get(URI.create(uri), configuration);  
  15.         FSDataInputStream in=null;  
  16.         in=fileSystem.open(new Path(uri));   
  17.         IOUtils.copyBytes(in, System.out, 4096false);  
  18.         IOUtils.closeStream(in);  
  19.     }  
  20.       

             

猜你喜欢

转载自blog.csdn.net/able202/article/details/79107258