【问题标题】:How to use PathFilter in Apache Spark?如何在 Apache Spark 中使用 PathFilter?
【发布时间】:2015-04-04 11:57:48
【问题描述】:

我有一个简单的文件过滤器,它基本上从特定日期选择文件。 在 Hadoop 中,我将使用setInputPathFilter 将PathFilter 类设置为InputFormat 参数。如何在 Spark 中执行此操作?

public class FilesFilter extends Configured implements PathFilter {

    @Override
    public boolean accept(Path path) {

        try {
            if (fs.isDirectory(path))
                return true;
        } catch (IOException e1) {
            e1.printStackTrace();
            return false;
        }

        String file_date = "01.30.2015";
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
        Date date = null;

        try {
            date = sdf.parse(file_date);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        long dt = date.getTime()/(1000 * 3600 * 24);

        try {
            FileStatus file = fs.getFileStatus(path);
            long time = file.getModificationTime() / (1000 * 3600 * 24);
            return time == dt;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }
}

【问题讨论】:

    标签: java scala hadoop apache-spark


    【解决方案1】:

    使用这个:

    sc.hadoopConfiguration.setClass("mapreduce.input.pathFilter.class", classOf[TmpFileFilter], classOf[PathFilter])
    

    这是我的TmpFileFilter.scala 代码,它将省略.tmp 文件:

    import org.apache.hadoop.fs.{Path, PathFilter}
    
    class TmpFileFilter  extends PathFilter {
      override def accept(path : Path): Boolean = !path.getName.endsWith(".tmp")
    }
    

    您可以定义自己的PathFilter。

    【讨论】:

    • 如果我想根据运行时正则表达式进行过滤怎么办?
    • 我得到了 java.lang.NoSuchMethodException: TmpFileFilter.<init>() ,我应该在 TmpFileFilter 类中修改什么?谢谢
    猜你喜欢
    • 2015-07-28
    • 1970-01-01
    • 2016-06-19
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多