Updated on 2022-09-14 GMT+08:00

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

  1. Use the append method in the FileSystem instance to obtain the output stream of the appended data.
  2. Use the output stream to add the content to be appended behind the specified file in the HDFS.

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);
    }
}