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

边栏推荐
- Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
- day9-字符串作业
- 【NFT网站】教你制作开发NFT预售网站官网Mint作品
- Error creating bean with name 'configDataContextRefresher' defined in class path resource
- Requests the library deployment and common function
- [Decoding tools] Some online tools for Bitcoin
- [Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
- day6-列表作业
- day8字典作业
- 学习总结week2_1
猜你喜欢

day10-字符串作业

Difference between for..in and for..of

Flex layout frog game clearance strategy

OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems

CAP+BASE
![[Go through 7] Notes from the first section of the fully connected neural network video](/img/e2/1107171b52fe9dcbf454f7edcdff77.png)
[Go through 7] Notes from the first section of the fully connected neural network video

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

Detailed Explanation of Redis Sentinel Mode Configuration File

【过一下10】sklearn使用记录

Flink 状态与容错 ( state 和 Fault Tolerance)
随机推荐
RL reinforcement learning summary (1)
redis persistence
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
Basic properties of binary tree + oj problem analysis
redis 缓存清除策略
Analysis of Mvi Architecture
【过一下12】整整一星期没记录
Develop a highly fault-tolerant distributed system
Flink HA安装配置实战
UVA10827
怎么更改el-table-column的边框线
【过一下16】回顾一下七月
2022杭电多校第一场01
软件设计 实验四 桥接模式实验
分布式和集群
OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
【过一下4】09-10_经典网络解析
range函数作用
序列基础练习题