Updated on 2024-08-16 GMT+08:00

Java Sample Code of Flink Asynchronous Checkpoint

Sample Code

Assume that you want to collect data volume in a 4-second time window every other second and the status of operators must be strictly consistent.

  • Snapshot data

    The snapshot data is used to store number of data pieces recorded by operators during creation of snapshots.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    import java.io.Seriablizale;
    // As a part of the snapshot, this class saves the user-defined status.
    public class UDFState implements Serializable {
        private long count;
        // Initialize the user-defined status.
        public UDFState() {
            count = 0L;
        }
        // Set the user-defined status.
        public void setState(long count) {
           this.count = count;
        }
        // Obtain the user-defined status.
        public long geState() {
            return this.count;
        }
    }
    
  • Data source with checkpoints

    The code snippet of a source operator pauses 1 second every time after sending 10,000 pieces of data. When a snapshot is created, the code saves the total number of sent data pieces in UDFState. When the snapshot is used for restoration, the number of sent data pieces saved in UDFState is read and assigned to the count variable.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    import org.apache.flink.api.java.tuple.Tuple4;
    import org.apache.flink.streaming.api.checkpoint.ListCheckpointed;
    import org.apache.flink.streaming.api.functions.source.SourceFunction;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class SimpleSourceWithCheckPoint implements SourceFunction<Tuple4<Long, String, String, Integer>>, ListCheckpointed<UDFState> {
    
        private long count = 0;
        private boolean isRunning = true;
        private String alphabet = "justtest";
    
        @Override
        public List<UDFState> snapshotState(long l, long l1) throws Exception
        {
            UDFState udfState = new UDFState();
            List<UDFState> udfStateList = new ArrayList<UDFState>();
            udfState.setCount(count);
            udfStateList.add(udfState);
            return udfStateList;
        }
    
        @Override
        public void restoreState(List<UDFState> list) throws Exception
        {
            UDFState udfState = list.get(0);
            count = udfState.getCount();
        }
    
        @Override
        public void run(SourceContext<Tuple4<Long, String, String, Integer>> sourceContext) throws Exception
        {
            Random random = new Random();
            while (isRunning) {
                for (int i = 0; i < 10000; i++) {
                    sourceContext.collect(Tuple4.of(random.nextLong(), "hello" + count, alphabet, 1));
                    count ++;
                }
                Thread.sleep(1000);
            }
        }
    
        @Override
        public void cancel()
        {
            isRunning = false;
        }
    }
    
  • Definition of a window with a checkpoint

    This code snippet is about a window operator and is used to calculate the number or tuples in a window.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    import org.apache.flink.api.java.tuple.Tuple;
    import org.apache.flink.api.java.tuple.Tuple4;
    import org.apache.flink.streaming.api.checkpoint.ListCheckpointed;
    import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
    import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
    import org.apache.flink.util.Collector;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class WindowStatisticWithChk implements WindowFunction<Tuple4<Long, String, String, Integer>, Long, Tuple, TimeWindow>, ListCheckpointed<UDFState> {
    
        private long total = 0;
    
        @Override
        public List<UDFState> snapshotState(long l, long l1) throws Exception
        {
            UDFState udfState = new UDFState();
            List<UDFState> list = new ArrayList<UDFState>();
            udfState.setCount(total);
            list.add(udfState);
            return list;
        }
    
        @Override
        public void restoreState(List<UDFState> list) throws Exception
        {
            UDFState udfState = list.get(0);
            total = udfState.getCount();
        }
    
        @Override
        public void apply(Tuple tuple, TimeWindow timeWindow, Iterable<Tuple4<Long, String, String, Integer>> iterable, Collector<Long> collector) throws Exception
        {
            long count = 0L;
            for (Tuple4<Long, String, String, Integer> tuple4 : iterable) {
                count ++;
            }
            total += count;
            collector.collect(total);
        }
    }
    
  • Application code

    This code snippet is about the definition of StreamGraph and detailed service implementation process. The processing time is used as time to trigger the window.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    import org.apache.flink.api.java.utils.ParameterTool;
    import org.apache.flink.runtime.state.StateBackend;
    import org.apache.flink.runtime.state.filesystem.FsStateBackend;
    import org.apache.flink.streaming.api.CheckpointingMode;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
    import org.apache.flink.streaming.api.windowing.time.Time;
    public class FlinkProcessingTimeAPIChkMain {
    
        public static void main(String[] args) throws Exception
        {
            String chkPath = ParameterTool.fromArgs(args).get("chkPath", "hdfs://hacluster/flink/checkpoints/");
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            
            env.setStateBackend((StateBackend) new FsStateBackend((chkPath)));
            env.enableCheckpointing(6000, CheckpointingMode.EXACTLY_ONCE);
            env.addSource(new SimpleSourceWithCheckPoint())
                    .keyBy(0)
                    .window(SlidingProcessingTimeWindows.of(Time.seconds(4), Time.seconds(1)))
                    .apply(new WindowStatisticWithChk())
                    .print();
    
            env.execute();
        }
    }