Hadoop學習筆記———《讀、寫HDFS文件》

讀取HDFS上的文件

  /**
     * Created by MJ on 15/12/06.
     *
     * @use 讀取HDFS上的指定文件的內容並返回
     *
     * @param filePath 待寫入的HDFS文件路徑
     * @return String 文件的內容
     * @exception Exception 異常返回null
     */
    public String readHdfsFile(String filePath) throws Exception {
      FileSystem fileSystem = null;
      BufferedReader bufferedReader = null;
      try {
        String result = "";
        Configuration conf = new Configuration();
        fileSystem = FileSystem.get(conf);
        FSDataInputStream fs = fileSystem.open(new Path(filePath));
        bufferedReader = new BufferedReader(new InputStreamReader(fs));
        String lineString;
        while (null != (lineString = bufferedReader.readLine())) {
          result += lineString;
        }
        return result;
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (null != bufferedReader) {
          bufferedReader.close();
        }
        if (null != fileSystem) {
          fileSystem.close();
        }
      }
      return null;
    }

寫入HDFS文件

/**
     * Created by MJ on 15/12/06.
     *
     * @use 將text內容寫入到HDFS上的指定文件
     *
     * @param filePath 待寫入的HDFS文件路徑
     * @param text 待寫入的內容
     * @exception
     */
    public void writeHdfsFile(String filePath, String text) throws Exception {
      FileSystem fileSystem = null;
      BufferedWriter bufferedWriter = null;
      try {
        Configuration conf = new Configuration();
        fileSystem = FileSystem.get(conf);
        FSDataOutputStream fs = fileSystem.create(new Path(filePath));
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(fs));
        bufferedWriter.write(text + "\n");
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (null != bufferedWriter) {
          bufferedWriter.flush();
          bufferedWriter.close();
        }
        if (null != fileSystem) {
          fileSystem.close();
        }
      }
    }




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章