Appending Data to a File
Function
Append data to a specified file in the Hadoop distributed file system (HDFS). The process of appending data to a file is
- Use the append method in the FileSystem instance to obtain the output stream of the appended data.
- Use the output stream to add the content to be appended behind the specified file in the HDFS.
![](https://support.huaweicloud.com/eu/devg3-mrs/public_sys-resources/note_3.0-en-us.png)
Close all requested resources after appending data.
Example Codes
The following is code snippets. For complete codes, see HdfsMain and HdfsWriter classes in com.huawei.bigdata.hdfs.examples.
/** * Append data to a file. * * @throws IOException */ private void append() throws IOException { final String content = "I append this content."; FSDataOutputStream out = null; try { out = fSystem.append(new Path(DEST_PATH + File.separator + FILE_NAME)); out.write(content.getBytes()); out.hsync(); LOG.info("success to append."); } finally { // make sure the stream is closed finally. IOUtils.closeStream(out); } }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.