Can someone explain me how write data into HDFS using Java?

0 votes

I wish to create a file in HDFS and write Data into it. My code for the same is as follows:

#    Configuration config = new Configuration();     
#    FileSystem fs = FileSystem.get(config); 
#    Path filenamePath = new Path("input.txt");  
#    try {
#        if (fs.exists(filenamePath)) {
#        fs.delete(filenamePath, true);
#        }
#    FSDataOutputStream fin = fs.create(filenamePath);
#    fin.writeUTF("hello");
#    fin.close();
#    }

This code successfully creates the file but am still unable to write any data into the File, is there a requirement where I need to take the file permission to write data into the file? If Yes, please help me out.

Thank you.

Jun 4 in Big Data Hadoop by nitinrawat895
• 10,690 points
51 views

1 answer to this question.

0 votes

I understand your issue, I would drop down a code which should help you find the solution.

#   Configuration configuration = new Configuration();
#   FileSystem hdfs = FileSystem.get( new URI( "hdfs://localhost:54310" ), configuration );
#   Path file = new Path("hdfs://localhost:54310/s2013/batch/table.html");
#   if ( hdfs.exists( file )) { hdfs.delete( file, true ); } 
#   OutputStream os = hdfs.create( file,
#      new Progressable() {
#          public void progress() {
#              out.println("...bytes written: [ "+bytesWritten+" ]");
#          } });
#   BufferedWriter br = new BufferedWriter( new OutputStreamWriter( os, "UTF-8" ) );
#   br.write("Hello World");
#   br.close();
#   hdfs.close();

This code should help it.

answered Jun 4 by ravikiran
• 4,560 points

Related Questions In Big Data Hadoop

0 votes
1 answer

How can I write text in HDFS using CMD?

Hadoop put & appendToFile only reads standard ...READ MORE

answered Apr 27, 2018 in Big Data Hadoop by Shubham
• 13,300 points
88 views
0 votes
1 answer

How to write a file in HDFS using Java Programming language?

Define the HADOOP_CONF_DIR environment variable to your Hadoop configuration ...READ MORE

answered Sep 28, 2018 in Big Data Hadoop by Frankie
• 9,810 points
239 views
0 votes
1 answer

How to transfer data from Netezza to HDFS using Apache Sqoop?

Remove the --direct option. It gives issue ...READ MORE

answered Apr 23, 2018 in Big Data Hadoop by kurt_cobain
• 9,260 points
321 views
0 votes
1 answer

How can anyone recover deleted HDFS data?

Yes, there is a trash policy in ...READ MORE

answered May 15, 2018 in Big Data Hadoop by Shubham
• 13,300 points
1,562 views
0 votes
1 answer

Moving files in Hadoop using the Java API?

I would recommend you to use FileSystem.rename(). ...READ MORE

answered Apr 15, 2018 in Big Data Hadoop by Shubham
• 13,300 points
864 views
0 votes
1 answer

Hadoop giving java.io.IOException, in mkdir Java code.

I am not sure about the issue. ...READ MORE

answered May 3, 2018 in Big Data Hadoop by Shubham
• 13,300 points
452 views
0 votes
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,690 points
3,097 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,690 points
349 views
0 votes
1 answer

I need to copy data from one HDFS to another HDFS. Can you help me do so?

I understood your issue. Let me help you ...READ MORE

answered May 16 in Big Data Hadoop by ravikiran
• 4,560 points
60 views
0 votes
1 answer

Explain to me how to get Hadoop configuration in Java Util.

In order to get access to the File ...READ MORE

answered May 24 in Big Data Hadoop by ravikiran
• 4,560 points
51 views