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

Office Address

Social List

How to Write Data into a File in HDFS

Writing Data into HDFS

Steps for Writing Data into a File in HDFS

  • Description:
    Writing data into a file in the Hadoop Distributed File System (HDFS) involves creating or overwriting a file in HDFS and transferring the desired content using APIs or HDFS shell commands. This example demonstrates programmatic access using Java code.
Source Code
  • import java.io.*;
    import org.apache.hadoop.fs.*;
    import org.apache.hadoop.conf.*;

    public class writeData {
    public static void main(String[] args) throws Exception {
    try {
    Configuration conf = new Configuration();
    Path pt = new Path("hdfs://localhost:54310/home/WriteData");
    FileSystem fs = pt.getFileSystem(conf);
    BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(pt, true)));

    String line = "Welcome to Hadoop MapReduce";
    br.write(line);
    br.close();

    System.out.println("Data written successfully to HDFS.");
    } catch (Exception e) {
    System.out.println("Error writing to HDFS: " + e.getMessage());
    }
    }
    }
Screenshots
  • HDFS Write Data Screenshot