HDFS中PathFilter类
在单个操作中处理一批文件,这是很常见的需求。比如说处理日志的MapReduce作业可能需要分析一个月内包含在大量目录中的日志文件。在一 个表达式中使用通配符在匹配多个文件时比较方便的,无需列举每个文件和目录来指定输入。hadoop为执行通配提供了两个FIleSystem方法:
1 public FileStatus[] globStatus(Path pathPattern) throw IOException 2 public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throw IOException
globStatus()方法返回与路径想匹配的所有文件的FileStatus对象数组,并按路径排序。hadoop所支持的通配符与Unix bash相同。
第二个方法传了一个PathFilter对象作为参数,PathFilter可以进一步对匹配进行限制。PathFilter是一个接口,里面只有一个方法accept(Path path)。
下面看一个例子演示PathFilter的作用:
RegexExcludePathFilter.java:该类实现了PathFilter接口,重写了accept方法
1 class RegexExcludePathFilter implements PathFilter{ 2 private final String regex; 3 public RegexExcludePathFilter(String regex) { 4 this.regex = regex; 5 } 6 @Override 7 public boolean accept(Path path) { 8 return !path.toString().matches(regex); 9 } 10 11 }
该方法就是打印符合通配的路径:
1 //通配符的使用 2 public static void list() throws IOException{ 3 Configuration conf = new Configuration(); 4 FileSystem fs = FileSystem.get(conf); 5 //PathFilter是过滤布符合置顶表达式的路径,下列就是把以txt结尾的过滤掉 6 FileStatus[] status = fs.globStatus(new Path(“hdfs://master:9000/user/hadoop/test/“),new RegexExcludePathFilter(“.txt”)); 7 //FileStatus[] status = fs.globStatus(new Path(“hdfs://master:9000/user/hadoop/test/*”)); 8 Path[] listedPaths = FileUtil.stat2Paths(status); 9 for (Path p : listedPaths) { 10 System.out.println(p); 11 } 12 }
如果注释第6行,取消第7行的注释,则输出结果如下:
hdfs://master:9000/user/hadoop/test/a.txt
hdfs://master:9000/user/hadoop/test/b.txt
hdfs://master:9000/user/hadoop/test/c.aaa
hdfs://master:9000/user/hadoop/test/c.txt
hdfs://master:9000/user/hadoop/test/cc.aaa
如果注释第7行,取消第6行的注释,则输出结果如下:
hdfs://master:9000/user/hadoop/test/c.aaa
hdfs://master:9000/user/hadoop/test/cc.aaa
由此可见,PathFilter就是在匹配前面条件之后再加以限制,将匹配PathFilter的路径去除掉。其实由accept方法里面的 return !path.toString().matches(regex);可以看出来,就是将匹配的全部去除掉,如果改为return path.toString().matches(regex);就是将匹配regex的Path输出,将不匹配的去除。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
HDFS之FileStatus
任何文件系统的一个重要特性都是提供其目录结构浏览和检索它所存文件和目录相关信息的功能。FileStatus对象封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及权限等信息。 FileStatus对象由FileSystem的getFileStatus()方法获得,调用该方法的时候要把文件的Path传进去。 例子:打印输出某个文件的所有信息 1 package com.hdfs; 2 3 import org.apache.hadoop.conf.Configuration; 4 import org.apache.hadoop.fs.FSDataInputStream; 5 import org.apache.hadoop.fs.FSDataOutputStream; 6 import org.apache.hadoop.fs.FileStatus; 7 import org.apache.hadoop.fs.FileSystem; 8 import org.apache.hadoop.fs.FileUtil; 9 import org.apac...
- 下一篇
HDFS中文件的压缩与解压
文件的压缩有两大好处:1、可以减少存储文件所需要的磁盘空间;2、可以加速数据在网络和磁盘上的传输。尤其是在处理大数据时,这两大好处是相当重要的。 下面是一个使用gzip工具压缩文件的例子。将文件/user/hadoop/aa.txt进行压缩,压缩后为/user/hadoop/text.gz 1 package com.hdfs; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.URI; 7 8 import org.apache.hadoop.conf.Configuration; 9 import org.apache.hadoop.fs.FSDataInputStream; 10 import org.apache.hadoop.fs.FSDataOutputStream; 11 import org.apache.hadoop.fs.FileSystem; 12 import org.apache.hadoo...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Hadoop3单机部署,实现最简伪集群
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Linux系统CentOS6、CentOS7手动修改IP地址
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Windows10,CentOS7,CentOS8安装Nodejs环境
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题