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.

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

}

}}