如題,在eclipse編寫hadoop程序並運行時,會出現Hadoop HDFS Wrong FS: hdfs:/ expected file:///的錯誤。經過網上baidu,google后發現,上面的一些方法不適用於我所寫的程序。故而將解決方法在此和大家分享一些,希望有所幫助。
首先,先看一下代碼:
1 package com.bing.file; 2 3 import org.apache.hadoop.conf.Configuration; 4 import org.apache.hadoop.fs.FileStatus; 5 import org.apache.hadoop.fs.FileSystem; 6 import org.apache.hadoop.fs.Path; 7 8 public class UploadFile 9 { 10 public void uploadFile(String srcFileName, String cloudFileName) throws Exception 11 { 12 String srcPath = "D:\\" + srcFileName; 13 14 String destPath = "hdfs://10.64.44.113:9000/user/haishi/" + cloudFileName; 15 16 Configuration conf = new Configuration(); 17 /* conf.set("mapred.jop.tracker", "hdfs://10.64.44.113:9001"); 18 conf.set("fs.default.name", "hdfs://10.64.44.113:9000"); 19 */ 20 FileSystem fs = FileSystem.get(conf); 21 22 Path src = new Path(srcPath); 23 Path dest = new Path(destPath); 24 25 fs.copyFromLocalFile(src, dest); 26 27 System.out.println("Upload to " + conf.get("fs.default.name")); 28 29 FileStatus[] files = fs.listStatus(dest); 30 31 for(FileStatus file : files) 32 { 33 System.out.println(file.getPath()); 34 } 35 36 } 37 38 public static void main(String[] args) 39 { 40 UploadFile up = new UploadFile(); 41 try 42 { 43 up.uploadFile("test.txt", "test_new.txt"); 44 } catch (Exception e) 45 { 46 e.printStackTrace(); 47 } 48 } 49 50 }
項目工程如下所示:

有兩種方式:
1) 通過configuration conf 中的conf.set操作來設置fs.default.name為core-site.xml中的對應的內容,即
conf.set("mapred.jop.tracker", "hdfs://10.64.44.113:9001");
conf.set("fs.default.name", "hdfs://10.64.44.113:9000");
可以解決,並試驗成功。
2) 通過引用xml文檔。
即引入hadoop的配置文檔,core-site.xml,mapred-site.xml,hdfs-site.xml。
core-site.xml文檔如下所示:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Put site-specific property overrides in this file. --> <configuration> <property> <name>fs.default.name</name> <value>hdfs://10.64.44.113:9000</value> </property> </configuration>
mapred-site.xml文檔:
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>hdfs://10.64.44.113:9001</value>
</property>
</configuration>
hdfs-site.xml文檔:
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
</configuration>
上面中dfs.permissions設置為false,即在hadoop集群里的操作時不進行權限驗證。
以上是在win7下進行的未引用eclipse-hadoop插件下進行的。
當然,如果是在插件下進行程序的開發,那么就可以在
視圖下進行,並進行相關的配置就可以進行。在這里就不啰嗦了。
可參考相關的書或者網站博客:
書目:實戰hadoop
