Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to append data into file in HDFS?

Description

Data into the HDFS is written using the FileSystem and OutputStreamWriter Object available in hadoop.fs and java.io package that writes the data as string.FSDataOutputStream and FileSystem objects are used to append the data into the existing file.

Sample Code

import java.util.*;
import java.net.*;

import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

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);

BufferedReader bfr = new BufferedReader(new InputStreamReader(hdfs.open(path))); //open file first

String str = null;

BufferedWriter br = new BufferedWriter(new OutputStreamWriter(hdfs.create(path, true)));

while ((str = bfr.readLine()) != null) {

br.write(str); // write file content

br.newLine();

 

}

br.write(“Append Data to HDFS”); // append into file

br.newLine();

br.close(); // close it

} catch (Exception e) {

System.out.println(“File not found”);

}

}

}

Screenshots

Append data into file in HDFS