Write a file in hdfs with Java Write a file in hdfs with Java hadoop hadoop

Write a file in hdfs with Java


an alternative to @Tariq's asnwer you could pass the URI when getting the filesystem

import org.apache.hadoop.fs.FileSystemimport org.apache.hadoop.conf.Configurationimport java.net.URIimport org.apache.hadoop.fs.Pathimport org.apache.hadoop.util.Progressableimport java.io.BufferedWriterimport java.io.OutputStreamWriterConfiguration 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();

See example:https://github.com/mpereira-dev/MapReduce-with-Accumulo/blob/master/src/main/java/batch/BatchScan2Html.java


Either define the HADOOP_CONF_DIR environment variable to your Hadoop configuration folder or add the following 2 lines in your code :

config.addResource(new Path("/HADOOP_HOME/conf/core-site.xml"));config.addResource(new Path("/HADOOP_HOME/conf/hdfs-site.xml"));

If you don't add this, your client will try to write to the local FS, hence resulting into the permission denied exception.


This should do the trick

import org.apache.commons.io.IOUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import java.io.*;import java.nio.charset.StandardCharsets;public static void writeFileToHDFS() throws IOException {        Configuration configuration = new Configuration();        configuration.set("fs.defaultFS", "hdfs://localhost:9000");        configuration.addResource(new Path("/HADOOP_HOME/conf/core-site.xml"));        configuration.addResource(new Path("/HADOOP_HOME/conf/hdfs-site.xml"));        FileSystem fileSystem = FileSystem.get(configuration);        //Create a path        String fileName = "input.txt";        Path hdfsWritePath = new Path("/user/yourdesiredpath/" + fileName);        FSDataOutputStream fsDataOutputStream = fileSystem.create(hdfsWritePath,true);        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream,StandardCharsets.UTF_8));        bufferedWriter.write("Java API to write data in HDFS");        bufferedWriter.close();        fileSystem.close();    }