栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 前沿技术 > 大数据 > 大数据系统

flink readFile方法按时间范围递归读取文件工具

大数据系统 更新时间:发布时间: 百科书网 趣学号

import org.apache.flink.api.common.io.FilePathFilter;
import org.apache.flink.api.java.io.TextInputFormat;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.fs.Path;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class InputUtil {
public static TextInputFormat myInputFormat(String dir, String startDate, String endDate){
Configuration configuration = new Configuration();
configuration.setBoolean(“recursive.file.enumeration”, true);
Path path = new Path(dir);
TextInputFormat textInputFormat = new TextInputFormat(path);
textInputFormat.configure(configuration);
textInputFormat.supportsMultiPaths();

    textInputFormat.setFilesFilter(new FilePathFilter() {
        @Override
        public boolean filterPath(Path filePath) {

            String filepath = filePath.toString();
            // 截取时间字符串用于转换为timestamp类型
            String[] splits = filepath.split("dt=");
            int length = splits.length;
            String date = splits[length - 1];
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");



            Date parseDate = null;
            Date parseStartDate = null;
            Date parseEndDate = null;
            try {
                parseDate = simpleDateFormat.parse(date);
                parseStartDate = simpleDateFormat.parse(startDate);
                parseEndDate = simpleDateFormat.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            long dateTime = parseDate.getTime();
            long startDateTime = parseStartDate.getTime();
            long endDateTime = parseEndDate.getTime();

            boolean s = dateTime >= startDateTime;
            boolean e = dateTime <= endDateTime;
            System.out.println(filePath.toString());

            return !(s && e);

        }
    });




    return textInputFormat;
}

}

最后的文件夹必须是这种格式

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/280892.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号