从本地计算机创建并将文件写入hdfs

时间:2015-02-11 11:03:51

标签: java hadoop hdfs

我在网络中连接了两个系统。一个是hdfs运行。我想创建一个文件并从另一台机器上写入数据。

  package myorg;
  import java.io.*;
  import java.util.*;
  import java.net.*;
  import org.apache.hadoop.fs.*;
  import org.apache.hadoop.conf.*;
  import org.apache.hadoop.io.*;
  import org.apache.hadoop.mapred.*;
  import org.apache.hadoop.util.*;

  public class Write1{
    public static void main (String [] args) throws Exception{
            try{
                    System.out.println("Starting...");
                    Path pt=new Path("hdfs://10.236.173.95:8020/user/jfor/out/gwmdfd");
                    FileSystem fs = FileSystem.get(new Configuration());
                    BufferedWriter br=new BufferedWriter(new OutputStreamWriter(fs.create(pt,true)));
                                               // TO append data to a file, use fs.append(Path f)
                    String line;
                    line="Disha Dishu Daasha dfasdasdawqeqwe";
                    System.out.println(line);
                    br.write(line);
                    br.close();
            }catch(Exception e){
                    System.out.println("File not found");
            }
    }

}

我用

编译了它
   javac -classpath hadoop-0.20.1-dev-core.jar -d Write1/ Write1.java

使用

创建一个jar
  jar -cvf Write1.jar -C Write1/ .

运行命令

  hadoop jar Write1.jar myorg.Write1

如果我运行这个,我正在

 starting...
 File not found

可能是什么原因?如果我运行这个程序,在我的hadoop机器上,它工作正常[我用localhost替换了ip]。

错误发生在BufferedWriter行。它说" File Not found"。这是什么意思?我用了fs.creat。然后它应该创建,如果它不存在。 ISN'?吨

 java.lang.IllegalArgumentException: Wrong FS: hdfs://10.72.40.68:8020/user/jfor/..... expected localhost:8020                                                   

 So i modified the following line

 FileSystem fs = FileSystem.get(new URI("hdfs://<ip>:8020"),new Configuration());

It says Connection refused. What could be the reason

0 个答案:

没有答案