...
Code Block | ||
---|---|---|
| ||
//==== Create folder if not exists Path workingDir=fs.getWorkingDirectory(); Path newFolderPath= new Path(path); if(!fs.exists(newFolderPath)) { // Create new Directory fs.mkdirs(newFolderPath); logger.info("Path "+path+" created."); } |
How to
...
write a file
...
to HDFS with Java?
Code example
Code Block | ||
---|---|---|
| ||
//==== Write file logger.info("Begin Write file into hdfs"); //Create a path Path hdfswritepath = new Path(newFolderPath + "/" + fileName); //Init output stream FSDataOutputStream outputStream=fs.create(hdfswritepath); //Cassical output stream usage outputStream.writeBytes(fileContent); outputStream.close(); logger.info("End Write file into hdfs"); |
How to
...
read a file
...
from HDFS with Java?
Code example
Code Block | ||
---|---|---|
| ||
//==== Read file logger.info("Read file intofrom hdfs"); //Create a path Path hdfsreadpath = new Path(newFolderPath + "/" + fileName); //Init input stream FSDataInputStream inputStream = fs.open(hdfsreadpath); //Classical input stream usage String out= IOUtils.toString(inputStream, "UTF-8"); logger.info(out); inputStream.close(); fs.close(); |
...