Description: Appending data into a file in HDFS (Hadoop Distributed File System) means adding new data to the end of an existing file without overwriting the current content. Unlike writing data to a new file, appending preserves the original file content.
public class appendData {
public static void main(String[] args) throws Exception {
try {
Configuration conf = new Configuration();
Path path = new Path("hdfs://localhost:54310/home/WriteData");
FileSystem hdfs = path.getFileSystem(conf);
// Open the file in append mode
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(hdfs.append(path)));
br.write("Append Data to HDFS"); // append new data
br.newLine();
br.close(); // close the stream
System.out.println("Data appended successfully to HDFS.");
} catch (Exception e) {
System.out.println("Error appending to HDFS: " + e.getMessage());
}
}
}