Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Append Data into a File in HDFS

Appending Data in HDFS

Steps for Appending Data into a File in HDFS

  • 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.
Source Code
  • import java.io.*;
    import org.apache.hadoop.fs.*;
    import org.apache.hadoop.conf.*;

    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());
    }
    }
    }
Screenshots
  • HDFS Append Data Screenshot 1