从HDFS读取文件时出现MalformedURLException

时间:2014-09-22 09:58:21

标签: java hadoop

我有以下测试程序从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)

3 个答案:

答案 0 :(得分:9)

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

试试这个:

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

答案 1 :(得分:2)

在编写用于从hadoop 2.6上的hdfs读取的Java应用程序时,我遇到了同样的问题。 我的解决方案是:添加

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

答案 2 :(得分:2)

在我们的案例中,我们必须将其与其他答案结合起来:
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"并没有帮助。