java write on hdfs code example

Example: java write on hdfs

Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://localhost:9000");
FileSystem fileSystem = FileSystem.get(configuration);
//Create a path
String fileName = "my_text.txt";
// using "/" in the start of the path will ensure to get the exact path that I want
Path hdfsWritePath = new Path("/my/dirr/" + fileName);
FSDataOutputStream fsDataOutputStream = fileSystem.create(hdfsWritePath,true);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream,StandardCharsets.UTF_8));
/** Output the results to the HDFS **/
bufferedWriter.write("Hello from java");
bufferedWriter.newLine();
bufferedWriter.close();
fileSystem.close();

Tags:

Misc Example