I want to store the details of current running queries, like file Names on which the queries are executing and time. for this i created one file in HDFS, tried to writing the information. But the problem is how to append the data to the existing file. Please help me. Thanks in Advance
5 Answers
First of all stop all the Hadoop daemons and add the following property in your hdfs-site.xml :
<property>
<name>dfs.support.append</name>
<value>true</value>
</property>
Now, restart the daemons and try this code :
public class HDFSAppend {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
conf.addResource(new Path("/path/to/your/hadoop/directory/conf/core-site.xml"));
conf.addResource(new Path("/path/to/your/hadoop/directory/conf/hdfs-site.xml"));
FileSystem fs = FileSystem.get(conf);
FSDataOutputStream out = fs.append(new Path("/demo.txt"));
out.writeUTF("Append demo...");
fs.close();
}
}
HTH

- 34,076
- 8
- 57
- 79
-
1going by [hdfs defaults for hadoop-2.4.1 onwards](https://hadoop.apache.org/docs/r2.4.1/hadoop-project-dist/hadoop-hdfs/hdfs-default.xml), the dfs.support.append property is by-default true, doesn't need to be set explicitly. – Shreyas Dec 28 '15 at 12:03
You can do it from the command line:
$ hadoop fs -appendToFile <local_file> <hdfs_file>
If you are using java see this question: Write a file in hdfs with Java

- 1
- 1

- 304
- 1
- 7
-
I'm using java and i used the same code given in the link. But the problem is i'm unable to append the data to the file, which is already created. – B Ganesh Babu Feb 09 '14 at 09:07
-
As Tariq suggested, you need to set the dfs.support.append property. See this link for more info [link](http://hadoop4mapreduce.blogspot.com/2012/08/two-methods-to-append-content-to-file.html) – B.J. Smegma Feb 11 '14 at 03:08
-
@B.J.Smegma not anymore. dfs.support.append defaults to true going by [hadoop-2.4.1 hdfs defaults](https://hadoop.apache.org/docs/r2.4.1/hadoop-project-dist/hadoop-hdfs/hdfs-default.xml) – Shreyas Dec 28 '15 at 12:06
The simplest way of doing it from command line would be:
echo -e 'Hello\nWorld' | hadoop dfs -put - /1.txt
Which will create a file 1.txt
in the path /
of HDFS
and will store two lines Hello\nWorld
in it.

- 515
- 7
- 26
Use command : hdfs dfs -put file_location hdfs_location
Heading
Note: hdfs_location is enable

- 11
- 2
Solved..!!
Append is supported in HDFS.
You just have to do some configurations and simple code as shown below :
Step 1: set dfs.support.append as true in hdfs-site.xml :
<property>
<name>dfs.support.append</name>
<value>true</value>
</property>
Stop all your daemon services using stop-all.sh and restart it again using start-all.sh
Step 2 (Optional): Only If you have a singlenode cluster , so you have to set replication factor to 1 as below :
Through command line :
./hdfs dfs -setrep -R 1 filepath/directory
Or you can do the same at run time through java code:
fShell.setrepr((short) 1, filePath);
Step 3 : Code for Creating/appending data into the file :
public void createAppendHDFS() throws IOException {
Configuration hadoopConfig = new Configuration();
hadoopConfig.set("fs.defaultFS", hdfsuri);
FileSystem fileSystem = FileSystem.get(hadoopConfig);
String filePath = "/test/doc.txt";
Path hdfsPath = new Path(filePath);
fShell.setrepr((short) 1, filePath);
FSDataOutputStream fileOutputStream = null;
try {
if (fileSystem.exists(hdfsPath)) {
fileOutputStream = fileSystem.append(hdfsPath);
fileOutputStream.writeBytes("appending into file. \n");
} else {
fileOutputStream = fileSystem.create(hdfsPath);
fileOutputStream.writeBytes("creating and writing into file\n");
}
} finally {
if (fileSystem != null) {
fileSystem.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
Kindly let me know for any other help.
Cheers.!!

- 101
- 1
- 4