java - MalformedURLException on reading file from HDFS -
i have following test program read file 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 = 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(); } } }
it giving java.net.malformedurlexception
exception in thread "main" java.net.malformedurlexception: unknown protocol: hdfs @ java.net.url.<init>(url.java:592) @ java.net.url.<init>(url.java:482) @ java.net.url.<init>(url.java:431) @ in.ksharma.hdfs.filereader.main(filereader.java:29)
register hadoop's url handler. standard url handler won't know how handle hdfs:// scheme.
try this:
public static void main(string[] args) throws malformedurlexception, ioexception { url.seturlstreamhandlerfactory(new fsurlstreamhandlerfactory()); string url = "hdfs://" + namenode_ip + file_path; inputstream = 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(); } }
Comments
Post a Comment