天天看點

Eclipse實作DFS部分操作複習(2)本文用于複習《Hadoop權威指南》第三章後半部分内容

本文用于複習《Hadoop權威指南》第三章後半部分内容

代碼來自于書中,僅有少部分修改,主要是為了回憶起來友善

在文章eclipse實作word count中就有關于如何在eclipse中開發hadoop項目,連結如下Eclipse實作Hadoop WordCount

  • 利用FileSystem的listStatus方法來讀取檔案和目錄的中繼資料,再用stat2path方法講status數組轉為path數組。

    這裡用一種與之前不同的路徑設定方法(雖然其實是與書上一樣的),在run configuration中添加參數

    hdfs://localhost:9000/ 和hdfs://localhost:9000/user/wyh/

public class ListStatus {

  public static void main(String[] args) throws Exception {
    String uri = args[];
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri), conf);

    Path[] paths = new Path[args.length];
    for (int i = ; i < paths.length; i++) {
      paths[i] = new Path(args[i]);
    }

    FileStatus[] status = fs.listStatus(paths);
    Path[] listedPaths = FileUtil.stat2Paths(status);
    for (Path p : listedPaths) {
      System.out.println(p);
    }
  }
}
           
  • 書上給出了一個RegexExcludePathFilter類實作了PathFilter接口,用于排除一個正規表達式路徑,也覺得挺有趣的,但是沒有給具體實作排除的代碼,在這裡寫了一個TestFilter類來實作一下。

    首先把2016.12.01.txt和2016.12.02.txt放到分布式系統中,可以看到在去掉RegexExcludePathFilter的時候會兩個檔案都顯示,直接運作則會顯示路徑+2016.12.02.txt。

public class TestFilter {
      public static void main(String[] args) throws Exception {
            String uri = "hdfs://localhost:9000/";
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(uri), conf);

            FileStatus[] status =fs.globStatus(new Path("hdfs://localhost:9000/user/wyh/2016.*.*")
            ,new RegexExcludePathFilter("hdfs://localhost:9000/user/wyh/2016.12.01.*"));
            // ,new RegexExcludePathFilter("hdfs://localhost:9000/user/wyh/2016.12.01.*")
            Path[] listedPaths = FileUtil.stat2Paths(status);
            for (Path p : listedPaths) {
              System.out.println(p);
            }
      }
}
           

繼續閱讀