java write on hdfs

Solutions on MaxInterview for java write on hdfs by the best coders in the world

showing results for - "java write on hdfs"
María Fernanda
30 Jul 2018
1Configuration configuration = new Configuration();
2configuration.set("fs.defaultFS", "hdfs://localhost:9000");
3FileSystem fileSystem = FileSystem.get(configuration);
4//Create a path
5String fileName = "my_text.txt";
6// using "/" in the start of the path will ensure to get the exact path that I want
7Path hdfsWritePath = new Path("/my/dirr/" + fileName);
8FSDataOutputStream fsDataOutputStream = fileSystem.create(hdfsWritePath,true);
9BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream,StandardCharsets.UTF_8));
10/** Output the results to the HDFS **/
11bufferedWriter.write("Hello from java");
12bufferedWriter.newLine();
13bufferedWriter.close();
14fileSystem.close();