簡體   English   中英

從HDFS讀取文件時出現MalformedURLException

[英]MalformedURLException on reading file from HDFS

我有以下測試程序從HDFS讀取文件。

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)

注冊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();
        }
    }

在編寫用於從hadoop 2.6上的hdfs讀取的Java應用程序時,我遇到了同樣的問題。 我的解決方案是:添加

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

在我們的例子中,我們必須將它與其他答案結合起來:
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"在我們的依賴項中並沒有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM