当前位置:网站首页>Flink Distributed Cache 分布式缓存
Flink Distributed Cache 分布式缓存
2022-08-05 05:14:00 【bigdata1024】
Flink提供了一个分布式缓存,类似于hadoop,可以使用户在并行函数中很方便的读取本地文件。此功能可用于共享文件,包含静态的外部数据,例如字典或者machine-learned回归模型。
此缓存的工作机制如下:程序注册一个文件或者目录(本地或者远程文件系统,例如hdfs或者s3),通过ExecutionEnvironment注册缓存文件并为它起一个名称。当程序执行,Flink自动将文件或者目录复制到所有worker节点的本地文件系统。用户函数可以查找文件或者目录通过这个指定的名称,然后从worker节点的本地文件系统访问它。
使用分布式缓存 如下示例:
java代码:
在ExecutionEnvironment中注册文件或者目录
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
// 从hdfs注册一个文件
env.registerCachedFile("hdfs:///path/to/your/file", "hdfsFile")
// 注册一个本地可执行的脚本文件
env.registerCachedFile("file:///path/to/exec/file", "localExecFile", true)
// 定义程序代码 并且执行
...
DataSet<String> input = ...
DataSet<Integer> result = input.map(new MyMapper());
...
env.execute();
在用户函数中访问缓存文件或者目录(这里是一个map函数)。这个函数必须继承RichFunction,因为它需要使用RuntimeContext读取数据
// 继承RichFunction 为了获取RuntimeContext
public final class MyMapper extends RichMapFunction<String, Integer> {
@Override
public void open(Configuration config) {
// 通过RuntimeContext 和 DistributedCache访问缓存文件
File myFile = getRuntimeContext().getDistributedCache().getFile("hdfsFile");
// 读取文件(或者本地目录)
...
}
@Override
public Integer map(String value) throws Exception {
// 使用缓存文件的内容做一些处理
...
}
}
scala代码:
在ExecutionEnvironment中注册文件或者目录
val env = ExecutionEnvironment.getExecutionEnvironment
// 从hdfs注册一个文件
env.registerCachedFile("hdfs:///path/to/your/file", "hdfsFile")
// 注册一个本地可执行的脚本文件
env.registerCachedFile("file:///path/to/exec/file", "localExecFile", true)
// 定义程序代码 并且执行
...
val input: DataSet[String] = ...
val result: DataSet[Integer] = input.map(new MyMapper())
...
env.execute()
在用户函数中访问缓存文件或者目录(这里是一个map函数)。这个函数必须继承RichFunction,因为它需要使用RuntimeContext读取数据
// 继承RichFunction 为了获取RuntimeContext
class MyMapper extends RichMapFunction[String, Int] {
override def open(config: Configuration): Unit = {
// 通过RuntimeContext 和 DistributedCache访问缓存文件
val myFile: File = getRuntimeContext.getDistributedCache.getFile("hdfsFile")
// 读取文件(或者本地目录)
...
}
override def map(value: String): Int = {
// 使用缓存文件的内容做一些处理
...
}
}
获取更多大数据资料,视频以及技术交流请加群:
边栏推荐
猜你喜欢
Develop a highly fault-tolerant distributed system
CAP+BASE
Basic properties of binary tree + oj problem analysis
Flutter real machine running and simulator running
CAP+BASE
软件设计 实验四 桥接模式实验
2022 Hangzhou Electric Multi-School 1st Session 01
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
Flink HA安装配置实战
随机推荐
转正菜鸟前进中的经验(废话)之谈 持续更新中... ...
UVA10827
Matplotlib(二)—— 子图
redis 持久化
The underlying mechanism of the class
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
【Untitled】
2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
day7-列表作业(1)
浅谈Servlet生命周期
机器学习(二) —— 机器学习基础
Flink accumulator Counter 累加器 和 计数器
【Over 16】Looking back at July
有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
SQL(一) —— 增删改查
vscode+pytorch使用经验记录(个人记录+不定时更新)
软件设计 实验四 桥接模式实验
ES6 Set、WeakSet
Xiaobai, you big bulls are lightly abused
DOM及其应用