如题,在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 }
View Code

项目工程如下所示:

有两种方式:

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  

 

 

 

posted on 2013-06-09 15:40  tiandibing  阅读(7471)  评论(0编辑  收藏  举报