Eclipse实现DFS部分操作复习(2)

本文复习《Hadoop权威指南》第三章,重点在于使用Eclipse进行Hadoop项目的开发。通过FileSystem的listStatus方法读取文件和目录元数据,并用stat2path转换。介绍了如何设置路径参数,以及实现PathFilter接口排除特定路径,举例说明了RegexExcludePathFilter的使用场景。
摘要由CSDN通过智能技术生成

本文用于复习《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[0];
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri), conf);

    Path[] paths = new Path[args.length];
    for (int i = 0; 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);
            }
      }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值