Whenever you want to write a file in HDFS, always pass the URI(a string of characters used to identify a resource on a computer network, of which the best-known type is the web address or URL.) while getting the filesystem.
Configuration configuration = new Configuration();
FileSystem hdfs = FileSystem.get( new URI( "hdfs://localhost:50070" ), configuration );
Path file = new Path("hdfs://localhost:50070/s2019/batch/tab.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 bw = new BufferedWriter( new OutputStreamWriter( os, "UTF-8" ) );
bw.write("Hello World");
bw.close();
hdfs.close();
Another approach to solve your problem:
You can just add the following lines to your code:
config.addResource(new Path("/HADOOP_HOME/conf/core-site.xml"));
config.addResource(new Path("/HADOOP_HOME/conf/hdfs-site.xml"));
OR just go to your Hadoop Configuration folder and define the HADOOP_CONF_DIR environment variable there.
If you have any doubt regarding the Hadoop, you can refer the following video: