2014-09-22 134 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",並沒有幫助。