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

时间:2024-06-19 14:14:15

代码样例

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

    /**
   * 读文件
   *
   * @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;

      // 写文件
      while ((sTempOneLine = reader.readLine()) != null) {
        strBuffer.append(sTempOneLine);
      }

      System.out.println("result is : " + strBuffer.toString());
      System.out.println("success to read.");

    } finally {
      //务必要关闭资源.
      close(reader);
      close(in);
    }
  }
support.huaweicloud.com/devg-mrs/mrs_06_0128.html