2012-11-27 63 views
9

我想從Hadoop文件系統中讀取文件。hdfs中的文件路徑

爲了實現文件的正確路徑,我需要hdfs的主機名和端口地址。

所以最後我的文件的路徑看起來像

Path path = new Path("hdfs://123.23.12.4344:9000/user/filename.txt") 

現在我想知道提取主機名=「123.23.12.4344」 &端口:9000?

基本上,我想訪問Amazon EMR上的FileSystem,但是當我使用

 FileSystem fs = FileSystem.get(getConf());
時,我得到了
 
You possibly called FileSystem.get(conf) when you should have called FileSystem.get(uri, conf) to obtain a file system supporting your path 
所以我決定使用URI。 (我必須使用URI),但我不確定如何訪問URI。

回答

13

您可以使用以下兩種方法之一來解決您的錯誤。

1.

String infile = "file.txt"; 
Path ofile = new Path(infile); 
FileSystem fs = ofile.getFileSystem(getConf()); 

2.

Configuration conf = getConf(); 
System.out.println("fs.default.name : - " + conf.get("fs.default.name")); 
// It prints uri as : hdfs://10.214.15.165:9000 or something 
String uri = conf.get("fs.default.name"); 
FileSystem fs = FileSystem.get(uri,getConf()); 
+0

感謝這個!它有幫助,你能解釋爲什麼這是嗎? – ruralcoder

+4

Hadoop有一個FileSystem工廠。它根據URI方案創建正確的文件系統,也可能是URI的權威部分。你可以看看你是否打開文件://,s3://或hdfs://你需要爲每個文件系統創建一個不同的文件系統。這就是爲什麼你需要包含URI – Wheezil

+1

fs.default.name已被棄用,每個這個問題的讀者應該使用fs.defaultFS來代替。 – chomp