I am trying to write a value into a file on the HDFS. Here is the code:
FileSystem fsys = FileSystem.get(new Configuration());
String fileName = "/user/root/TestData/Parameter.txt";
Path path = new Path(fileName);//(pathOfTestFile);
//fstatus.getPath();
FSDataOutputStream fos = null;
try {
fos = fsys.create(path);
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(fos));
writer.write(iterations);
writer.close();
fos.close();
But, I see that the command hadoop fs -ls shows a size of 4 , but when I do a hadoop fs -cat , the file doesn’t show any contents.
I’m assuming that iterations is an integer / short / byte. Either way the byte(s) representation of these values is zero or more 0x00 bytes, followed by a 0x04 byte. Neither 0x00 nor 0x04 are printable characters so it’s to no surprise that
hadoop fs -catshows nothing in the output.If you pipe the output to hexdump -C though, i imagine you’ll see your output:
EDIT
You’re also using BufferedWriter.write(int) which actually writes out the int as a chaarcter. You should be using FSDataOutputStream.writeInt(iterations) (it actually is a method of DataOutputStream) and then FSDataInputStream.readInt() to read it back again.