hadoop 之 PathFilter -- 输入文件过滤器
来源:互联网 发布:设置手机淘宝关键词 编辑:程序博客网 时间:2024/04/30 22:29
1.指定多个输入
在单个操作中处理一批文件,这是很常见的需求。比如说处理日志的MapReduce作业可能需要分析一个月内包含在大量目录中的日志文件。在一个表达式中使用通配符在匹配多个文件时比较方便的,无需列举每个文件和目录来指定输入。hadoop为执行通配提供了两个FileSystem方法:
public FileStatus[] globStatus(Path pathPattern) throw IOExceptionpublic FileStatus[] globStatus(Path pathPattern, PathFilter filter) throw IOException
PS:
globStatus()方法返回与路径想匹配的所有文件的FileStatus对象数组,并按路径排序。hadoop所支持的通配符与Unix bash相同。
第二个方法传了一个PathFilter对象作为参数,PathFilter可以进一步对匹配进行限制。PathFilter是一个接口,里面只有一个方法accept(Path path)。
PathFilter实例
RegexExcludePathFilter.java
class RegexExcludePathFilter implements PathFilter{ private final String regex; public RegexExcludePathFilter(String regex) { this.regex = regex; } @Override public boolean accept(Path path) { return !path.toString().matches(regex); }}
PS:该类实现了PathFilter接口,重写了accept方法
使用这个过滤器:
//通配符的使用public static void list() throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); //PathFilter是过滤布符合置顶表达式的路径,下列就是把以txt结尾的过滤掉 FileStatus[] status = fs.globStatus(new Path("hdfs://master:9000/user/hadoop/test/*"),new RegexExcludePathFilter(".*txt")); //FileStatus[] status = fs.globStatus(new Path("hdfs://master:9000/user/hadoop/test/*")); Path[] listedPaths = FileUtil.stat2Paths(status); for (Path p : listedPaths) { System.out.println(p); } }
如果没有过滤器,
FileStatus[] status = fs.globStatus(new Path("hdfs://master:9000/user/hadoop/test/*"));
则输出结果如下:
hdfs://master:9000/user/hadoop/test/a.txthdfs://master:9000/user/hadoop/test/b.txthdfs://master:9000/user/hadoop/test/c.aaahdfs://master:9000/user/hadoop/test/c.txthdfs://master:9000/user/hadoop/test/cc.aaa
如果使用了过滤器
FileStatus[] status = fs.globStatus(new Path("hdfs://master:9000/user/hadoop/test/*"),new RegexExcludePathFilter(".*txt"));
则输出结果如下:
hdfs://master:9000/user/hadoop/test/c.aaahdfs://master:9000/user/hadoop/test/cc.aaa
由此可见,PathFilter就是在匹配前面条件之后再加以限制,将匹配PathFilter的路径去除掉。
其实由accept方法里面的 return !path.toString().matches(regex);
可以看出来,就是将匹配的全部去除掉,如果改为 return path.toString().matches(regex);
就是将匹配regex的Path输出,将不匹配的去除。
PathFilter实例2
public int run(String[] args) throws Exception { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); Job job = Job.getInstance(conf); //通过过滤器过滤掉不要的文件 FileStatus[] status = fs.globStatus(new Path(args[0]),new RegexExcludePathFilter(".*txt")); Path[] listedPaths = FileUtil.stat2Paths(status); job.setJarByClass(this.getClass()); job.setJobName("SumStepByTool"); job.setInputFormatClass(TextInputFormat.class); //这个是默认的输入格式 job.setMapperClass(SumStepByToolMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(InfoBeanMy.class); job.setReducerClass(SumStepByToolReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(InfoBeanMy.class); //job.setNumReduceTasks(3); //对不同的输入文件使用不同的Mapper进行处理// MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class, SumStepByToolMapper.class);// MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, SumStepByToolWithCommaMapper.class); FileInputFormat.setInputPaths(job, listedPaths); FileOutputFormat.setOutputPath(job, new Path(args[1])); return job.waitForCompletion(true) ? 0:-1; }
1 0
- hadoop 之 PathFilter -- 输入文件过滤器
- hadoop 之 PathFilter -- 输入文件过滤器
- [Hadoop]输入路径过滤,通配符与PathFilter
- Hadoop的PathFilter使用
- hadoop的 pathfilter使用
- Hadoop的PathFilter使用
- java学习之路_chapter15_输入/输出之文件过滤器
- hadoop在使用PathFilter时遇到的问题
- 利用PathFilter上传多个本地的文件
- hadoop的输入文件了解了
- Hadoop中获取输入文件路径
- hadoop处理不同输入目录文件
- hadoop自定义文件的输入格式
- 自定义 hadoop MapReduce InputFormat 切分输入文件
- 自定义 hadoop MapReduce InputFormat 切分输入文件
- Hadoop对输入文件划分(InputFormat)
- 自定义 hadoop MapReduce InputFormat 切分输入文件
- 自定义 hadoop MapReduce InputFormat 切分输入文件
- Python 启动 Scrapy爬虫
- listView中Item控件抢占焦点
- 个人微信公众号VI升级心路
- Ubuntu下使用类似QQ一样的截图功能
- 六,游戏主界面
- hadoop 之 PathFilter -- 输入文件过滤器
- 第七讲 项目2-三数最大值
- 如何安全的存储用户的密码
- spark sql DataFrame操作
- [转]MFC制作不规则窗体
- 响应式
- 不同数据库分页技术
- 连续声明的几个int型变量内存地址不连续
- 《HBase权威指南》读书笔记11:第十一章 性能优化