Flink学习16---容错机制(六)深入理解OperatorState,自定义实现ExactlyOnce多并行Source

FlinkKafkaConsumer内部实现了ExactlyOnce,如果使用自定义多并行Source,也可以借助Operator实现ExactlyOnce。

 

import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;
import java.io.RandomAccessFile;
import java.util.Iterator;

// 这里的泛型Tuple2是该source输出的数据类型
public class MyExactlyOnceParFileSource extends RichParallelSourceFunction<Tuple2<String,String>> implements CheckpointedFunction {

    private String path = "C:\\Users\\Dell\\Desktop\\test";

    private Boolean flag = true; // 控制是否cancel

    private long offset = 0;//偏移量默认值

    private transient ListState<Long> offsetState;//状态数据不参与序列化,添加 transient 修饰

    public MyExactlyOnceParFileSource() {
    }

    public MyExactlyOnceParFileSource(String path) {
        this.path = path;
    }

    /**
     * 初始化状态(初始化OperatorState)  相当于subTask new完成之后构造器的生命周期方法,构造器执行完会执行一次
     *
     * 从 StateBackend 中取状态
     * @param context
     * @throws Exception
     */
    @Override
    public void initializeState(FunctionInitializationContext context) throws Exception {
        //定义一个状态描述器(根据状态描述器 1.初始化状态 或者 2.获取历史状态)
        ListStateDescriptor<Long> stateDescriptor = new ListStateDescriptor<Long>(
                "offset-state",//指定状态描述器s称(可以随便定义,但是一个Job任务中不能重复)
                Types.LONG
//                TypeInformation.of(new TypeHint<Long>() {})
//                Long.class
        );
        //获取 operatorState 数据
        offsetState = context.getOperatorStateStore().getListState(stateDescriptor);
    }

    /**
     * run()方法,用于一直运行产生数据
     */
    @Override
    public void run(SourceContext<Tuple2<String, String>> ctx) throws Exception {
        //获取 offsetState 中的历史值(赋值给offset)
        Iterator<Long> iterator = offsetState.get().iterator();
        while (iterator.hasNext()) {
            offset = iterator.next();
        }
        //获取当前 subTask 的 index 值
        int subtaskIndex = getRuntimeContext().getIndexOfThisSubtask();

        //定义用于读取的文件路径
        RandomAccessFile randomAccessFile = new RandomAccessFile(path+"/"+subtaskIndex+".txt", "r");

        //从指定偏移量位置读取
        randomAccessFile.seek(offset);

        //多并行线程不安全问题。需要加锁。
        final Object checkpointLock = ctx.getCheckpointLock();//最好用final修饰

        while (flag) {
            String line = randomAccessFile.readLine();
            if (line != null) {
                line = new String(line.getBytes("ISO-8859-1"), "UTF-8");

                synchronized (checkpointLock){
                    //获取 randomAccessFile 已经读完数据的指针
                    offset = randomAccessFile.getFilePointer();
                    //将数据发送出去
                    ctx.collect(Tuple2.of(subtaskIndex+"",line));
                }
            }else{
                Thread.sleep(1000);
            }
        }
    }

    /**
     * 定期将指定的状态数据,保存到 StateBackEnd 中
     */
    @Override
    public void snapshotState(FunctionSnapshotContext functionSnapshotContext) throws Exception {
        //将历史值清除
        offsetState.clear();
        // 更新最新的状态值
        offsetState.add(offset);

    }

    /**
     * cancel() 方法,用于关闭Source
     */
    @Override
    public void cancel() {
        flag = false;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值