Help Center/
MapReduce Service/
Developer Guide (Normal_Earlier Than 3.x)/
HDFS Development Guide/
Developing an HDFS Application/
Reading an HDFS File
Updated on 2024-08-16 GMT+08:00
Reading an HDFS File
Function Description
Read data from a specified file in HDFS.
Close all requested resources after reading a file.
Sample Code
The following provides code snippets for reading a file. For complete codes, see the HdfsMain class in com.huawei.bigdata.hdfs.examples.
/** * Read a file. * * @throws IOException */ private void read() throws IOException { String strPath = DEST_PATH + File.separator + FILE_NAME; Path path = new Path(strPath); FSDataInputStream in = null; BufferedReader reader = null; StringBuffer strBuffer = new StringBuffer(); try { in = fSystem.open(path); reader = new BufferedReader(new InputStreamReader(in)); String sTempOneLine; // Write data to a file. while ((sTempOneLine = reader.readLine()) != null) { strBuffer.append(sTempOneLine); } System.out.println("result is : " + strBuffer.toString()); System.out.println("success to read."); } finally { // Resources must be closed. close(reader); close(in); } }
Parent topic: Developing an HDFS Application
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot