Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing file to HDFS using Java

I'm trying to write a file to HDFS, the file get created but it is empty on the cluster, however when I run the code locally it works like a charm.

here's my code :

FSDataOutputStream recOutputWriter = null;
FileSystem fs = null;
try {
    //OutputWriter = new FileWriter(outputFileName,true);
    Configuration configuration = new Configuration();
    fs = FileSystem.get(configuration);
    Path testOutFile = new Path(outputFileName);
    recOutputWriter = fs.create(testOutFile);

    //outputWriter = new FileWriter(outputFileName,true);
} catch (IOException e) {
    e.printStackTrace();
}
recOutputWriter.writeBytes("======================================\n");
recOutputWriter.writeBytes("OK\n");
recOutputWriter.writeBytes("======================================\n");

if (recOutputWriter != null) {
    recOutputWriter.close();
}
fs.close();

did I miss something ?

like image 720
Jay Avatar asked Nov 08 '22 12:11

Jay


1 Answers

In order to write data to a file after creating it on the cluster I had to add :

System.setProperty("HADOOP_USER_NAME", "vagrant");

Refrence - Writing files to Hadoop HDFS using Scala

like image 116
Jay Avatar answered Nov 14 '22 22:11

Jay