2017-02-14 34 views
0

我在尝试使用Java API从HDFS(Hortonworks Sandbox)读取文件时遇到问题。以下是我的代码 -使用Java API从HDFS(Hortonworks Sandbox)读取文件时出现异常

System.setProperty ("hadoop.home.dir", "/"); 
    URI uri = URI.create ("hdfs://localhost:8020/user/maria_dev/test.txt"); 

    Path path = new Path (uri); 

    Configuration conf = new Configuration(); 
    conf.set ("fs.defaultFS", "hdfs://localhost:8020"); 
    conf.set ("dfs.client.use.datanode.hostname","true"); 
    conf.set("dfs.datanode.use.datanode.hostname","true"); 
    conf.set("dfs.client.use.legacy.blockreader", "true"); 

    byte[] btbuffer = new byte[5]; 
    String s; 
    try (FileSystem fs = FileSystem.get (uri, conf)) { 
     try { 
      FSDataInputStream fileIn = fs.open (path); 
      //s = fileIn.readUTF(); 
      fileIn.read (btbuffer, 0, 20); 
      s = new String (btbuffer, Charset.forName ("UTF-8")); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    catch (Exception err){ 
     err.printStackTrace(); 
    } 

以下是我得到的例外 -

10:39:51.803 [main] WARN org.apache.hadoop.hdfs.BlockReaderFactory - I/O error constructing remote block reader. java.net.ConnectException: Connection refused . 
10:39:51.803 [main] WARN org.apache.hadoop.hdfs.DFSClient - Failed to connect to sandbox.hortonworks.com/172.17.0.2:50010 for block, add to deadNodes and continue. java.net.ConnectException: Connection refused . 
10:39:51.804 [main] INFO org.apache.hadoop.hdfs.DFSClient - Could not obtain BP-1464254149-172.17.0.2-1477381671113:blk_1073742576_1752 from any node: java.io.IOException: No live nodes contain block BP-1464254149-172.17.0.2-1477381671113:blk_1073742576_1752 after checking nodes = [172.17.0.2:50010], ignoredNodes = null No live nodes contain current block Block locations: 172.17.0.2:50010 Dead nodes: 172.17.0.2:50010. 

找不到任何有效的解决方案。任何帮助表示赞赏。

编辑: 以下是在/ etc中的条目/我的主机系统的主机(从那里我打电话作业) -

127.0.0.1  localhost  
255.255.255.255 broadcasthost  
::1    localhost  
172.17.0.2 sandbox.hortonworks.com localhost  
+0

检查'hostname'来达到sandbox.hortonworks.com

尝试并匹配与fs.defaultFS那。 – rbyndoor

+0

主机名是sandbox.hortonworks.com。将fs.defaultFS更改为sandbox.hortonworks.com似乎没有改变任何内容。 – MK22

+0

你在哪里运行这段代码?沙箱没有用户界面,那么你在终端上写这个吗?不应该在沙箱中更改配置设置**。这些都很好。 –

回答

0

这是你提到的URI URI uri = URI.create ("hdfs://localhost:8020/user/maria_dev/test.txt");

但它试图通过改变URI

+0

将URI更改为sandbox.hortonworks.com,但得到此异常 - java.lang.IllegalArgumentException:java.net.UnknownHostException:sandbox.hortonworks.com – MK22

+0

您可以发布您的/ etc/hosts条目 – BruceWayne

+0

该文件位于给定的URI正在打开,但阅读没有发生。所以我猜这个问题可能不在URI中。 – MK22

相关问题