源码解析flink文件连接源TextInputFormat

背景:

kafka的文件系统数据源可以支持精准一次的一致性,本文就从源码看下如何TextInputFormat如何支持状态的精准一致性

TextInputFormat源码解析

首先flink会把输入的文件进行切分,分成多个数据块的形式,每个数据源算子任务会被分配以读取其中的数据块,但是不是所有的文件都能进行分块,判断文件是否可以进行分块的代码如下:

java 复制代码
protected boolean testForUnsplittable(FileStatus pathFile) {
    if (getInflaterInputStreamFactory(pathFile.getPath()) != null) {
        unsplittable = true;
        return true;
    }
    return false;
}

private InflaterInputStreamFactory<?> getInflaterInputStreamFactory(Path path) {
    String fileExtension = extractFileExtension(path.getName());
    if (fileExtension != null) {
        return getInflaterInputStreamFactory(fileExtension);
    } else {
        return null;
    }
}

后缀名称是.gz,.bzip2等的文件都没法切分,如果可以切分,切分的具体代码如下所示:

java 复制代码
while (samplesTaken < numSamples && fileNum < allFiles.size()) {
    // make a split for the sample and use it to read a record
    FileStatus file = allFiles.get(fileNum);
// 根据偏移量进行切分
    FileInputSplit split = new FileInputSplit(0, file.getPath(), offset, file.getLen() - offset, null);
    // we open the split, read one line, and take its length
    try {
        open(split);
        if (readLine()) {
            totalNumBytes += this.currLen + this.delimiter.length;
            samplesTaken++;
        }
    } finally {
        // close the file stream, do not release the buffers
        super.close();
    }
// 偏移量迁移
    offset += stepSize;

    // skip to the next file, if necessary
    while (fileNum < allFiles.size()
            && offset >= (file = allFiles.get(fileNum)).getLen()) {
        offset -= file.getLen();
        fileNum++;
    }
}

再来看一下TextInputFormat如何支持checkpoint操作,保存文件的偏移量的代码:

java 复制代码
@Override
public void snapshotState(StateSnapshotContext context) throws Exception {
    super.snapshotState(context);

    checkState(
            checkpointedState != null, "The operator state has not been properly initialized.");

    int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
    // 算子列表状态
    checkpointedState.clear();
    // 获取文件的当前读取的偏移
    List<T> readerState = getReaderState();

    try {
        for (T split : readerState) {
           //保存到检查点路径中
            checkpointedState.add(split);
        }
    } catch (Exception e) {
        checkpointedState.clear();

        throw new Exception(
                "Could not add timestamped file input splits to to operator "
                        + "state backend of operator "
                        + getOperatorName()
                        + '.',
                e);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug(
                "{} (taskIdx={}) checkpointed {} splits: {}.",
                getClass().getSimpleName(),
                subtaskIdx,
                readerState.size(),
                readerState);
    }
}

从检查点中恢复状态的代码如下:

java 复制代码
public void initializeState(StateInitializationContext context) throws Exception {
    super.initializeState(context);

    checkState(checkpointedState == null, "The reader state has already been initialized.");

    // 初始化算子操作状态
    checkpointedState =
            context.getOperatorStateStore()
                    .getListState(new ListStateDescriptor<>("splits", new JavaSerializer<>()));

    int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
    
    LOG.info(
            "Restoring state for the {} (taskIdx={}).", getClass().getSimpleName(), subtaskIdx);

    splits = splits == null ? new PriorityQueue<>() : splits;
    for (T split : checkpointedState.get()) {//从检查点状态中恢复各个切分的分块
        splits.add(split);
    }
}
相关推荐
FII工业富联科技服务1 小时前
Omniverse + Isaac Teleop + 合成数据:工业富联机器人大脑训练+执行落地闭环拆解
大数据·人工智能·深度学习·机器学习·机器人·制造·具身智能
airank3 小时前
2026年GEO服务商选型指南:系统梳理服务商分类框架、主流服务商能力横评、企业级选型六大维度及避坑要点,帮助企业在AI搜索时代做出科学决策。
大数据·人工智能·数据分析·aigc
Delite8023 小时前
摆脱实验室束缚:便携式卡尔费休微量水分检测技术与现场应用解析
大数据·网络·人工智能
袋鼠云数栈4 小时前
实时湖仓如何真正做到“数据够新”?
大数据·数据库·人工智能·数据治理
ACP广源盛139246256734 小时前
M6/M5 Pro Mac mini 端侧 AI 落地@ACP#YLB3116 中端多盘存储扩展在 AI 服务中的机会与应用场景
大数据·网络·数据库·人工智能·嵌入式硬件·macos
大大大大晴天4 小时前
大数据数据治理到底治理什么:从“数据可用”到“数据可信”
大数据
ACP广源盛139246256734 小时前
M6/M5 Pro Mac mini 端侧 AI 新形态@ACP#GSV5800 Serdes 长距离视频传输在 AI 服务中的机会与落地场景
大数据·网络·数据库·人工智能·嵌入式硬件·macos·音视频
策案案案6 小时前
YOLO: 将AI Agents嵌入到IntelliJ IDEA
java·大数据·人工智能·笔记·yolo·microsoft·intellij-idea
一支标6 小时前
【手机+电脑】通达信《K策王》策略战法K策王技术副图指标使用说明
大数据·智能手机·电脑
坐吃山猪7 小时前
【多线程】Lock与Condition
大数据·数据库