Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to write 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.

Sample Code

import java.io.*;
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 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;

line = “Welcome to Hadoop MapReduce”;

br.write(line);

br.close();

} catch (Exception e) {

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

}

}}


Screenshots

Write data into file in HDFS