#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 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 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