Appending File Content
Function Description
Append specific content to a specified file in HDFS. The process is as follows:
- Instantiate a FileSystem.
- Use the FileSystem instance to obtain various related resources.
- 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);
}
} Last Article: Writing Data to a File
Next Article: Reading a File
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.