[Hadoop]输入路径过滤,通配符与PathFilter
来源:互联网 发布:企业网络搭建试题 编辑:程序博客网 时间:2024/04/30 21:23
1. 丢失文件路径过滤
应用场景:我们想查询一个月以来度假的订单数据,但是HDFS中可能因为业务故障,导致某一天的订单数据不存在:
FileInputFormat.setInputPaths(job, inputPath);
上述代码在遇到路径不存在的时候会报错。
所以在设置路径之前需要进行一次判断,判断这个路径在HDFS上是否存在,如果存在,使用addInputPath方法添加:
FileSystem fileSystem = FileSystem.get(conf);
String[] params = inputPath.split(",");
for(String path : params){
boolean isExists = fileSystem.exists(new Path(path));
if(isExists){
FileInputFormat.addInputPath(job, new Path(path));
}
}
2. 文件名过滤
在一步操作中处理批量文件,这个要求很常见。举例来说,处理日志的MapReduce作业可能会分析一个月的文件,这些文件被包含在大量目录中。Hadoop有一个通配的操作,可以方便地使用通配符在一个表达式中核对多个文件,不需要列举每个文件和目录来指定输入。Hadoop为执行通配提供了两个FileSystem方法:
public FileStatus[] globStatus(Path pathPattern) throws IOException
public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throws IOException
globStatus()返回了其路径匹配于所供格式的FileStatus对象数组,按路径排序。可选的PathFilter命令可以进一步指定限制匹配。
2.1 通配符过滤
Hadoop支持的一系列通配符与Unix bash相同:
假设有日志文件存储在按日期分层组织的目录结构中。如此一来,便可以假设2007年最后一天的日志文件就会以/2007/12/31的命名存入目录。假设整个文件列表如下:
/2007/12/30
/2007/12/31
/2008/01/01
/2008/01/02
以下是一些文件通配符及其扩展:
通配符扩展/*
/2007/2008
/*/*
/2007/12 /2008/01
/*/12/*
/2007/12/30 /2007/12/31
/200?
/2007 /2008
/200[78]
/2007 /2008
/200[7-8]
/2007 /2008
/200[^01234569]
/2007 /2008
/*/*/{31,01}
/2007/12/31 /2008/01/01
/*/*/3{0,1}
/2007/12/30 /2007/12/31
/*/{12/31,01/01}
/2007/12/31 /2008/01/01
Example:
FileSystem fileSystem = FileSystem.get(conf);
FileStatus[] fileStatusArray = fileSystem.globStatus(new Path("mysql-log/201612/0[1-3]/10/*"));
for(FileStatus fileStatus : fileStatusArray){
Path path = fileStatus.getPath();
System.out.println("----------------------"+path);
FileInputFormat.addInputPath(job, path);
}
输出:
----------------------hdfs://qunarcluster/user/xiaosi/mysql-log/201612/01/10/l-test.cn6
...
----------------------hdfs://qunarcluster/user/xiaosi/mysql-log/201612/02/10/l-test.cn6
...
----------------------hdfs://qunarcluster/user/xiaosi/mysql-log/201612/03/10/l-test.cn6
...
2.2. PathFilter过滤
通配格式不是总能够精确地描述我们想要访问的文件集合。比如,使用通配格式排除一个特定的文件就不太可能。FileSystem中的listStatus()和globStatus()方法提供了可选的PathFilter对象,使我们能够通过编程方式控制匹配:
package org.apache.hadoop.fs;
public interface PathFilter {
boolean accept(Path path);
}
PathFilter与java.io.FileFilter一样,是Path对象而不是File对象。
展示了一个PathFilter,用于排除匹配一个正则表达式的路径:
public class RegexExcludePathFilter implements PathFilter {
private final String regex;
public RegexExcludePathFilter(String regex) {
this.regex = regex;
}
public boolean accept(Path path) {
return !path.toString().matches(regex);
}
}
这个过滤器只留下与正则表达式不同的文件。我们将它与预先剔除一些文件集合的通配配合:过滤器用来优化结果。例如:
fs.globStatus( new Path("/2007/*/*"),
new RegexExcludeFilter("^.*/2007/12/31$")
)
- [Hadoop]输入路径过滤,通配符与PathFilter
- hadoop 之 PathFilter -- 输入文件过滤器
- hadoop 之 PathFilter -- 输入文件过滤器
- hadoop输入路径读取文件的正则通配符
- Hadoop的PathFilter使用
- hadoop的 pathfilter使用
- Hadoop的PathFilter使用
- [Hadoop]MapReduce多路径输入与多个输入
- Hadoop MapReduce多路径输入与多个输入 例子
- hadoop mapreduce 多输入路径
- (五)hadoop路径读取文件的通配符
- hadoop在使用PathFilter时遇到的问题
- 用通配符进行过滤
- MySQL通配符过滤
- SQL通配符过滤
- MySQL通配符过滤数据
- MySQL 用通配符过滤
- Hadoop中获取输入文件路径
- Activity的生命周期及异常情况分析
- 证书算法
- https升级指南
- MySQL 5.6 for Windows 免安装版配置(mysql-5.6.34-winx64.zip)
- Java中的字符串比较相等与大小
- [Hadoop]输入路径过滤,通配符与PathFilter
- Hibernate获取当前Session,Hibernate清空当前Session的缓存
- qt条件编译debug和release版本--qmake之CONFIG(debug, debug|release)
- false positive误检率 准确率 召回率
- Oracle Grid Infrastructure架构
- LeetCode 327 Count of Range Sum
- 升级覆盖安装apk过程中闪退bug的适配(Android)
- 从全球导航到输入法:谈谈动态规划
- UVa401 回文词 刘汝佳 算法竞赛入门经典(第二版)