当前位置:网站首页>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的话,就不建议进行广播了。
获取更多大数据资料,视频以及技术交流请加群:

边栏推荐
猜你喜欢

The software design experiment four bridge model experiment

Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!

【过一下14】自习室的一天

NodeJs接收上传文件并自定义保存路径

【NFT网站】教你制作开发NFT预售网站官网Mint作品

Pandas(五)—— 分类数据、读取数据库
![[Go through 8] Fully Connected Neural Network Video Notes](/img/0a/8b2510b5536621f402982feb0a01ef.png)
[Go through 8] Fully Connected Neural Network Video Notes
![[Go through 10] sklearn usage record](/img/70/60783c7d16000c6e9d753d8db9a330.png)
[Go through 10] sklearn usage record

怎么更改el-table-column的边框线

Basic properties of binary tree + oj problem analysis
随机推荐
【过一下 17】pytorch 改写 keras
Difference between for..in and for..of
【过一下7】全连接神经网络视频第一节的笔记
物理层的接口有哪几个方面的特性?各包含些什么内容?
npm搭建本地服务器,直接运行build后的目录
vscode+pytorch使用经验记录(个人记录+不定时更新)
[Go through 8] Fully Connected Neural Network Video Notes
Flink accumulator Counter 累加器 和 计数器
Xiaobai, you big bulls are lightly abused
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
Lecture 4 Backpropagation Essays
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
[Decoding tools] Some online tools for Bitcoin
day11-函数作业
vscode+pytorch use experience record (personal record + irregular update)
【过一下14】自习室的一天
el-table鼠标移入表格改变显示背景色
The role of the range function
【解码工具】Bitcoin的一些在线工具
序列基础练习题