Updated on 2024-08-16 GMT+08:00

Appending HDFS File Content

Function Description

Append specific content to a specified file in HDFS. The process is as follows:

  1. Instantiate a FileSystem.
  2. Use the FileSystem instance to obtain various related resources.
  3. Add the content to the specified file in HDFS.

Close all requested resources after appending the file content.

Sample Code

The following provides code snippets. For complete codes, see the HdfsMain and HdfsWriter classes in com.huawei.bigdata.hdfs.examples.

 /**
 * Append file content.
 *
 * @throws IOException
 */
private void append() throws Exception {
     final String content = "I append this content.";
     InputStream in = (InputStream) new ByteArrayInputStream(
     content.getBytes());
   try {
     HdfsWriter writer = new HdfsWriter(fSystem, DEST_PATH
        + File.separator + FILE_NAME);
     writer.doAppend(in);
    System.out.println("success to append.");
    } finally {
// Stream resources must be closed.
    close(in);
    }
}