MapReduce服务 MRS-读文件:代码样例

时间:2023-11-01 16:19:45

代码样例

如下是读文件的代码片段,详细代码请参考com.huawei.bigdata.hdfs.examples中的HdfsExample类。

/** * 读文件 * * @throws java.io.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 file        while ((sTempOneLine = reader.readLine()) != null) {            strBuffer.append(sTempOneLine);        }        LOG.info("result is : " + strBuffer.toString());        LOG.info("success to read.");    } finally {        // make sure the streams are closed finally.        IOUtils.closeStream(reader);        IOUtils.closeStream(in);    }}
support.huaweicloud.com/devg3-mrs/mrs_07_300016.html