1、 设置StateBackend模式,此处以保存到hdfs为例;
在flink-conf.yaml中设置 state.backend为filesystem;
设置state.backend.fs.checkpointdir 即checkpoints的保存路径。
也可在程序中指定checkpoints的保存路径。
env.setStateBackend(new FsStateBackend("hdfs://192.168.***.***:9000/flink/checkpoints"));
注意:在程序中指定的路径如果和配置文件不一致,会按程序中指定的路径保存。
2、 打开flinkweb界面http://192.168.***.***:8081,选择上传jar包,出入entryclass等配置;
3、 本程序使用kafka作为数据源,在kafkatopic中输入一些测试数据,程序输出结果如下;
3、 cancel程序,找到checkpointd的保存路径;
在hdfs中打开该路径,发现有9个文件,其中一个位metadata,另外8个里面有四个保存operatorstate,另外四个保存keyedstate
4、 重启程序,输入要恢复的savepoint路径;
5、 再在kafkatopic里面输入部分数据,发现是接着上次的偏移量读取的,并且wordcount也是在cancel之前的基础上计算的;
最后附上代码
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import java.util.Properties;
public class StateBackendAndSavePointReview {
public static void main(String[] args) throws Exception{
System.setProperty("HADOOP_USER_NAME","root"); //以root的身份访问hdfs
// 1.获取flink流计算的运行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// 2. 容错相关配置
// 2.1 开启CheckPointing
env.enableCheckpointing(10000);
// 2.2 设置重启策略
env.setRestartStrategy(RestartStrategies.fixedDelayRestart(10, Time.seconds(2)));
//2.3 设置Checkpoint模式(与Kafka整合,一定要设置Checkpoint模式为Exactly_Once)
env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
//2.4 系统异常退出或人为 Cancel 掉,不删除checkpoint数据
env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
//2.5 StateBackend配置:存储在hdfs中并指定checkpoints保存的具体位置
// 会覆盖flink-conf.yaml中的相关配置
env.setStateBackend(new FsStateBackend("hdfs://192.168.***.***:9000/flink/checkpoints"));
// 3. 创建kafka数据源
// 3.1 Kafka props
Properties properties = new Properties();
//指定Kafka的Broker地址
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.***.***:9092,192.168.***.***:9092,192.168.***.***:9092");
//指定组ID
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "StateBackendTest");
//如果没有记录偏移量,第一次从最开始消费
properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
//Kafka的消费者,不自动提交偏移量 (不建议改成false)
// 默认情况下,该参数为true. 会定期把偏移量保存到kafka特殊的topic里面
// 该topic的作用:监控数据的消费情况;重启时优先从save point中恢复,如果没有指定save point,则直接从该topic中恢复(topic和组id名字没有变)
// properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
FlinkKafkaConsumer<String> kafkaSource = new FlinkKafkaConsumer("StateBackendTest", new SimpleStringSchema(), properties);
DataStreamSource<String> lines = env.addSource(kafkaSource);
// 4 FlinkKafkaStream数据处理
SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndOne = lines.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
@Override
public void flatMap(String line, Collector<Tuple2<String, Integer>> collector) throws Exception {
String[] words = line.split(",");
for (int i = 0; i < words.length; i++) {
collector.collect(Tuple2.of(words[i], 1));
}
}
});
SingleOutputStreamOperator<Tuple2<String, Integer>> summed = wordAndOne.keyBy(0).sum(1);
//5 sink输出
summed.print();
// 6. 执行程序
env.execute("StateBackendAndSavePointReviewTest");
}
}
版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: