用Flink对keyed state 状态的代码进行设置TTL(过期时间)和hdfs的checkpoints恢复和配置Flink-conf.yaml
依赖dependencygroupIdorg.apache.flink/groupIdartifactIdflink-core/artifactIdversion1.8.1/version/dependencydependencygroupIdorg.apache.flink/groupIdartifactIdflink-clients_2.11/artifactIdversion1.8.1/version/dependencydependencygroupIdorg.apache.flink/groupIdartifactIdflink-scala_2.11/artifactIdversion1.8.1/version/dependencydependencygroupIdorg.apache.flink/groupIdartifactIdflink-streaming-scala_2.11/artifactIdversion1.8.1/version/dependencydependencygroupIdorg.slf4j/groupIdartifactIdslf4j-log4j12/artifactIdversion1.7.26/version/dependency!--添加HDFS支持--dependencygroupIdorg.apache.hadoop/groupIdartifactIdhadoop-hdfs/artifactIdversion2.9.2/version/dependencydependencygroupIdorg.apache.hadoop/groupIdartifactIdhadoop-common/artifactIdversion2.9.2/version/dependency!--添加Kafka支持--dependencygroupIdorg.apache.flink/groupIdartifactIdflink-connector-kafka_2.11/artifactIdversion1.8.1/version/dependencydependencygroupIdorg.apache.flink/groupIdartifactIdflink-json/artifactIdversion1.8.1/version/dependencydependencygroupIdorg.apache.flink/groupIdartifactIdflink-connector-filesystem_2.11/artifactIdversion1.8.1/version/dependency!--写到Redis中--dependencygroupIdorg.apache.bahir/groupIdartifactIdflink-connector-redis_2.11/artifactIdversion1.0/version/dependency/dependenciesbuildplugins!--在执行package时候将scala源码编译进jar--plugingroupIdnet.alchim31.maven/groupIdartifactIdscala-maven-plugin/artifactIdversion4.0.1/versionexecutionsexecutionidscala-compile-first/idphaseprocess-resources/phasegoalsgoaladd-source/goalgoalcompile/goal/goals/execution/executions/plugin/plugins/build代码packagecom.baizhi.demo04importorg.apache.flink.streaming.api.CheckpointingModeimportorg.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanupimportorg.apache.flink.streaming.api.scala.{DataStream,StreamExecutionEnvironment,_}object Flink{defmain(args:Array[String]):Unit{val envStreamExecutionEnvironment.getExecutionEnvironment//开启checkpointenv.enableCheckpointing(7000,CheckpointingMode.EXACTLY_ONCE)//checkpoint必须在2s内完成如果完成不了终止env.getCheckpointConfig.setCheckpointTimeout(4000)//距离上一次的checkpoint完成之后需要等5s 之后再开启下一次的checkpointenv.getCheckpointConfig.setMinPauseBetweenCheckpoints(5000)env.getCheckpointConfig.setMaxConcurrentCheckpoints(1)//只开启一个checkpoint线程//在退出应用时候不删除checkpoint数据env.getCheckpointConfig.enableExternalizedCheckpoints(ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION)//必须保证任务可以从checkpoint恢复恢复不成功任务失败env.getCheckpointConfig.setFailOnCheckpointingErrors(true)val data:DataStream[String]env.socketTextStream(Flink,9999)data.flatMap(lineline.split(\\s)).map((_,1)).keyBy(0).map(newCountMapFunction).print()env.execute()}}packagecom.baizhi.demo04importorg.apache.flink.api.common.functions.RichMapFunctionimportorg.apache.flink.api.common.state.{StateTtlConfig,ValueState,ValueStateDescriptor}importorg.apache.flink.api.common.time.Timeimportorg.apache.flink.configuration.Configurationimportorg.apache.flink.streaming.api.scala._classCountMapFunctionextendsRichMapFunction[(String,Int),(String,Int)]{var state:ValueState[Int]_ override defmap(value:(String,Int)):(String,Int){var historystate.value()if(historynull){history0}state.update(historyvalue._2)(value._1,historyvalue._2)}override defopen(parameters:Configuration):Unit{val decnewValueStateDescriptor[Int](count,createTypeInformation[Int])//1.创建TTLConfigval ttlConfigStateTtlConfig.newBuilder(Time.seconds(80))//这是state存活时间10s.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)//设置过期时间更新方式.setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)//永远不要返回过期的状态//.cleanupInRocksdbCompactFilter(1000)//处理完1000个状态查询时候会启用一次CompactFilter.build//2.开启TTLdec.enableTimeToLive(ttlConfig)val contextgetRuntimeContext statecontext.getState(dec)}}配置Flink的环境配置Flink-conf.yaml## Fault tolerance and checkpointing## The backend that will be used to store operator state checkpoints if# checkpointing is enabled.## Supported backends are jobmanager, filesystem, rocksdb, or the# class-name-of-factory.#state.backend:rocksdb# Directory for checkpoints filesystem, when using any of the default bundled# state backends.#state.checkpoints.dir:hdfs:///flink-checkpoints# Default target directory for savepoints, optional.#state.savepoints.dir:hdfs:///flink-savepoints# Flag to enable/disable incremental checkpoints for backends that# support incremental checkpoints (like the RocksDB state backend).#state.backend.incremental:truestate.backend.rocksdb.ttl.compaction.filter.enabled:true## HistoryServer## The HistoryServer is started and stopped via bin/historyserver.sh (start|stop)# Directory to upload completed jobs to. Add this directory to the list of# monitored directories of the HistoryServer as well (see below).jobmanager.archive.fs.dir:hdfs:///completed-jobs/# The address under which the web-based HistoryServer listens.historyserver.web.address:CentOS# The port under which the web-based HistoryServer listens.historyserver.web.port:8082# Comma separated list of directories to monitor for completed jobs.historyserver.archive.fs.dir:hdfs:///completed-jobs/# Interval in milliseconds for refreshing the monitored directories.historyserver.archive.fs.refresh-interval:10000将hadoop_classpath配置到环境变量中export HADOOP_CLASSPATHhadoop classpath

相关新闻

单细胞测序技术解析:CIMA免疫细胞图谱与应用

单细胞测序技术解析:CIMA免疫细胞图谱与应用

1. 项目背景与意义 CIMA(Cell-level Immune Map Atlas)千万级免疫细胞图谱的发布,标志着单细胞测序技术在免疫学研究领域取得重大突破。这个项目通过对上千万个免疫细胞进行高通量测序和系统性分析,构建了迄今为止最全面的免疫细胞…

2026/10/26 6:28:36 阅读更多 →
Hashcat实战:五种方法破解NTLM哈希与掩码规则速查

Hashcat实战:五种方法破解NTLM哈希与掩码规则速查

1. 项目概述:当NTLM哈希遇上Hashcat 在渗透测试和红队评估的日常工作中,我们经常会遇到从目标系统中提取出的用户凭证哈希。其中,NTLM(NT LAN Manager)哈希是Windows环境中最常见、也最核心的一种凭证存储形式。它不像…

2026/10/21 21:56:20 阅读更多 →
C#中的Nutshell函数式编程

C#中的Nutshell函数式编程

目录 介绍 函数编程定义 函数属性 纯度 头等函数 闭包的概念 成为函数式 函数式实用程序 纯度重要性 头等的重要性 函数编程和面向对象编程 集成函数编程 总结 下载源码 - 7.9 KB 介绍 如今,函数性编程正在流行。我们应该问自己有两个问题&#xff1a…

2026/10/21 15:16:41 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/10/26 12:00:21 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/10/27 4:06:49 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/10/26 4:52:12 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/10/26 12:00:21 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/10/26 5:00:37 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/10/27 4:06:50 阅读更多 →

日新闻

周新闻

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/26 12:00:19 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/25 13:21:54 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/26 8:34:37 阅读更多 →