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, 2019 in Big Data Hadoop by nitinrawat895
• 11,380 points
1,318 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, 2019 by ravikiran
• 4,620 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,490 points
2,277 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,830 points
2,859 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,350 points
1,752 views
0 votes
1 answer

How can anyone recover deleted HDFS data?

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

answered May 16, 2018 in Big Data Hadoop by Shubham
• 13,490 points
10,913 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,490 points
2,669 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,490 points
2,543 views
+1 vote
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
• 11,380 points
11,028 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
• 11,380 points
2,536 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, 2019 in Big Data Hadoop by ravikiran
• 4,620 points
2,492 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, 2019 in Big Data Hadoop by ravikiran
• 4,620 points
2,193 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP