Writing to HDFS from Java, getting “could only be replicated to 0 nodes instead of minReplication”

后端 未结 11 882
时光取名叫无心
时光取名叫无心 2021-02-01 18:59

I’ve downloaded and started up Cloudera\'s Hadoop Demo VM for CDH4 (running Hadoop 2.0.0). I’m trying to write a Java program that will run from my windows 7 machine (The same

11条回答
  •  心在旅途
    2021-02-01 19:19

    Here is how I create files in the HDFS:

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    
    FileSystem hdfs = FileSystem.get(context.getConfiguration());
    Path outFile=new Path("/path to store the output file");
    
    String line1=null;
    
    if (!hdfs.exists(outFile)){
                OutputStream out = hdfs.create(outFile);
                BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
                br.write("whatever data"+"\n");
                br.close();
                hdfs.close();
            }
    else{
                String line2=null;
                BufferedReader br1 = new BufferedReader(new InputStreamReader(hdfs.open(outFile)));
                while((line2=br1.readLine())!=null){
                    line1=line1.concat(line2)+"\n";
                }
                br1.close();
                hdfs.delete(outFile, true);
                OutputStream out = hdfs.create(outFile);
                BufferedWriter br2 = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
                br2.write(line1+"new data"+"\n");
                br2.close();
                hdfs.close();
            }
    

提交回复
热议问题