2014-09-22 135 views
4

我有以下测试程序从HDFS读取文件。从HDFS读取文件时出现MalformedURLException

public class FileReader { 
    public static final String NAMENODE_IP = "172.32.17.209"; 
    public static final String FILE_PATH = "/notice.html"; 

    public static void main(String[] args) throws MalformedURLException, 
      IOException { 
     String url = "hdfs://" + NAMENODE_IP + FILE_PATH; 

     InputStream is = new URL(url).openStream(); 
     InputStreamReader isr = new InputStreamReader(is); 
     BufferedReader br = new BufferedReader(isr); 
     String line = br.readLine(); 
     while(line != null) { 
      System.out.println(line); 
      line = br.readLine(); 
     } 
    } 
} 

这是给java.net.MalformedURLException

Exception in thread "main" java.net.MalformedURLException: unknown protocol: hdfs 
    at java.net.URL.<init>(URL.java:592) 
    at java.net.URL.<init>(URL.java:482) 
    at java.net.URL.<init>(URL.java:431) 
    at in.ksharma.hdfs.FileReader.main(FileReader.java:29) 

回答

6

注册Hadoop的URL处理器。标准的Url处理程序将不知道如何处理hdfs://方案。

试试这个:

public static void main(String[] args) throws MalformedURLException, 
      IOException { 
     URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); 

     String url = "hdfs://" + NAMENODE_IP + FILE_PATH; 

     InputStream is = new URL(url).openStream(); 
     InputStreamReader isr = new InputStreamReader(is); 
     BufferedReader br = new BufferedReader(isr); 
     String line = br.readLine(); 
     while(line != null) { 
      System.out.println(line); 
      line = br.readLine(); 
     } 
    } 
+0

我试过这段代码,但仍然收到异常:'unlnown protocol:hdfs'。请让我知道你是如何解决这个问题的。 – user182944 2015-01-17 08:29:41

1

我得到同样的问题,而从HDFS读取Hadoop的2.6编写Java应用程序。 我的解决办法是:添加

hadoop-2.X/share/hadoop/hdfs/hadoop-hdfs-2.X.jar to your classpath. 
+0

这是删除错误的必需步骤。我没有看到为什么这个票数被低估。为我工作。 – 2016-10-16 11:17:56

1

在我们的例子中,我们不得不把它与其他答案相结合:
https://stackoverflow.com/a/21118824/1549135

所以,首先我们HDFS设置类Scala code):

val hadoopConfig: Configuration = new Configuration() 
hadoopConfig.set("fs.hdfs.impl", classOf[DistributedFileSystem].getName) 
hadoopConfig.set("fs.file.impl", classOf[LocalFileSystem].getName) 

后来,像在公认的答案:
https://stackoverflow.com/a/25971334/1549135

URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory) 
Try(new URL(path)) 

旁注:

我们已经有:在我们依赖 "org.apache.hadoop" % "hadoop-hdfs" % "2.8.0",并没有帮助。