当前位置:网站首页>Flink Broadcast 广播变量
Flink Broadcast 广播变量
2022-08-05 05:14:00 【bigdata1024】
Broadcast 广播变量:一句话解释,可以理解为是一个公共的共享变量,我们可以把一个dataset 数据集广播出去,然后不同的任务在节点上都能够获取到,这个数据在每个节点上只会存在一份。如果不使用broadcast,则在每个节点中的每个任务中都需要拷贝一份dataset数据集,比较浪费内存(也就是一个节点中可能会存在多份dataset数据)。
Broadcast variables允许你创建一个数据集在所有的并行操作节点都能获取到,除了常规的输入操作。针对一些小的依赖数据集,这种方式是非常有用的,这个data set数据集将会作为一个Collection集合被操作访问。
- Broadcast(广播数据) :通过withBroadcastSet(DataSet, String) 进行广播数据,并给这份数据起名字
- Access(获取数据):通过getRuntimeContext().getBroadcastVariable(String)获取广播出去的数据
java代码示例
// 1. 准备等待广播的DataSet数据
DataSet<Integer> toBroadcast = env.fromElements(1, 2, 3);
DataSet<String> data = env.fromElements("a", "b");
data.map(new RichMapFunction<String, String>() {
@Override
public void open(Configuration parameters) throws Exception {
// 3. 获取广播的DataSet数据 作为一个Collection
Collection<Integer> broadcastSet = getRuntimeContext().getBroadcastVariable("broadcastSetName");
}
@Override
public String map(String value) throws Exception {
...
}
}).withBroadcastSet(toBroadcast, "broadcastSetName"); // 2. 广播DataSetscala代码示例
// 1. 准备等待广播的DataSet数据
val toBroadcast = env.fromElements(1, 2, 3)
val data = env.fromElements("a", "b")
data.map(new RichMapFunction[String, String]() {
var broadcastSet: Traversable[String] = null
override def open(config: Configuration): Unit = {
// 3. 获取广播的DataSet数据 作为一个Collection
broadcastSet = getRuntimeContext().getBroadcastVariable[String]("broadcastSetName").asScala
}
def map(in: String): String = {
...
}
}).withBroadcastSet(toBroadcast, "broadcastSetName") // 2. 广播DataSet
确保在广播的时候,和获取数据的时候使用相同的broadcastSetName名称。
注意:
1:广播出去的变量存在于每个节点的内存中,所以这个数据集不能太大。因为广播出去的数据,会常驻内存,除非程序执行结束。
2:广播变量在初始化广播出去以后不支持修改,这样才能保证每个节点的数据都是一致的。
个人建议:如果数据集在几十兆或者百兆的时候,可以选择进行广播,如果数据集的大小上G的话,就不建议进行广播了。
获取更多大数据资料,视频以及技术交流请加群:

边栏推荐
猜你喜欢

Flink accumulator Counter 累加器 和 计数器

Basic properties of binary tree + oj problem analysis
![[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)](/img/86/9c9a2541f2b7089ae47e9832fffdb3.png)
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)

The fourth back propagation back propagation

Lecture 5 Using pytorch to implement linear regression

flink实例开发-batch批处理实例

Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction

vscode+pytorch use experience record (personal record + irregular update)

Flink 状态与容错 ( state 和 Fault Tolerance)

DOM and its applications
随机推荐
redis 缓存清除策略
Database experiment five backup and recovery
DOM及其应用
【Untitled】
[Go through 7] Notes from the first section of the fully connected neural network video
【过一下16】回顾一下七月
day11-函数作业
【Transfer】What is etcd
flink实例开发-batch批处理实例
Lecture 4 Backpropagation Essays
Machine Learning (2) - Machine Learning Fundamentals
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
序列基础练习题
学习总结week2_2
小白一枚各位大牛轻虐虐
Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
[Remember 1] June 29, 2022 Brother and brother double pain
redis cache clearing strategy
redis 持久化
day8字典作业