当前位置:网站首页>flink 物理分区( 随机分区、 轮询分区、重缩放分区、 广播、 全局分区、自定义分区 )
flink 物理分区( 随机分区、 轮询分区、重缩放分区、 广播、 全局分区、自定义分区 )
2022-06-11 12:06:00 【但行益事莫问前程】
文章目录
前言
Flink中keyBy是一种按照键的哈希值来进行重新分区的操作,至于分区是否均匀、每个key 的数据具体会分到哪一区无法控制,因此keyBy 是一种逻辑分区(logical partitioning)操作。只有物理分区(physical partitioning),才真正控制分区策略精准地调配数据。
物理分区与 keyBy 另一大区别在于,keyBy 之后得到的是一个 KeyedStream,而物理分区之后结果仍是 DataStream,且流中元素数据类型保持不变。分区算子并不对数据进行转换处理,只是定义了数据的传输方式
1. 随机分区(shuffle)
Partitions elements randomly according to a uniform distribution
通过调用 DataStream 的.shuffle()方法,将数据随机地分配到下游算子的并行任务中去
随机分区服从均匀分布(uniform distribution),所以可以把流中的数据随机打乱,均匀地传递到下游任务分区。因为是完全随机的,所以对于同样的输入数据, 每次执行得到的结果也不会相同。

dataStream.shuffle();
2. 轮询分区(Round-Robin)
按照先后顺序将数据做依次分发。通过调用 DataStream 的.rebalance()方法,就可以实现轮询重分区。rebalance使用的是 Round-Robin 负载均衡算法,可以将输入流数据平均分配到下游的并行任务中去
dataStream.rebalance()
3. 重缩放分区(rescale)
重缩放分区和轮询分区非常相似。当调用 rescale()方法时,其实底层也是使用 Round-Robin算法进行轮询,但是只会将数据轮询发送到下游并行任务的一部分中。 rebalance 的方式是每个发牌人都面向所有人发牌;而 rescale的做法是分成小团体,发牌人只给自己团体内的所有人轮流发牌。
由于 rebalance 是所有分区数据的“重新平衡”,当 TaskManager 数据量较多时,这种跨节点的网络传输必然影响效率;而如果配置的 task slot 数量合适,用 rescale 的方式进行“局部重缩放”,就可以让数据只在当前 TaskManager 的多个 slot 之间重新分配,从而避免了网络传输带来的损耗。
从底层实现上看,rebalance 和 rescale 的根本区别在于任务之间的连接机制不同。rebalance将会针对所有上游任务(发送数据方)和所有下游任务(接收数据方)之间建立通信通道,这是一个笛卡尔积的关系;而 rescale 仅仅针对每一个任务和下游对应的部分任务之间建立通信通道,节省了很多资源。
dataStream.rescale()
4. 广播(broadcast)
经过广播之后,数据会在不同的分区都保留一份,可能进行重复处理。可以通过调用 DataStream 的 broadcast()方法,将输入数据复制并发送到下游算子的所有并行任务中去。
dataStream.broadcast();
5. 全局分区(global)
通过调用.global()方法,会将所有的输入流数据都发送到下游算子的第一个并行子任务中去。这就相当于强行让下游任务并行度变成了 1,所以使用这个操作需要非常谨慎,可能对程序造成很大的压力
6. 自定义分区(Custom)
当 Flink 提 供 的 所 有 分 区 策 略 都 不 能 满 足 用 户 的 需 求 时 , 可 以 通 过 使 用partitionCustom()方法来自定义分区策略。
dataStream.partitionCustom(partitioner, "someKey");
dataStream.partitionCustom(partitioner, 0);
在调用时,方法需要传入两个参数,第一个是自定义分区器(Partitioner)对象,第二个是应用分区器的字段,它的指定方式与 keyBy 指定 key 基本一样:可以通过字段名称指定,也可以通过字段位置索引来指定,还可以实现一个 KeySelector
public class CustomPartitionTest {
//对一组自然数按照奇偶性进行重分区
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env =
StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
// 将自然数按照奇偶分区
env.fromElements(1, 2, 3, 4, 5, 6, 7, 8)
.partitionCustom(new Partitioner<Integer>() {
@Override
public int partition(Integer key, int numPartitions) {
return key % 2;
}
}, new KeySelector<Integer, Integer>() {
@Override
public Integer getKey(Integer value) throws Exception {
return value;
}
})
.print()
.setParallelism(2);
env.execute();
}
}

边栏推荐
- Let you understand selection sorting (C language)
- Intl.numberformat set number format
- How does Sister Feng change to ice?
- 数据如何在 Splunk 中老化?
- 大数相加(C语言)
- 反射真的很耗时吗,反射 10 万次,耗时多久。
- 创建线程的四种方式
- When a pure data service machine calls in, it falls back to 2g/3g
- iframe 传值
- Live app source code, and the status bar and navigation bar are set to transparent status
猜你喜欢

How does Sister Feng change to ice?

flink GROUPING SETS多维度聚合、设置Table state 到期时间

数据如何在 Splunk 中老化?

When a pure data service machine calls in, it falls back to 2g/3g

Splunk Bucket 背后的秘密

中文输入法输入事件composition的使用

刷题笔记(十四)--二叉树:层序遍历和DFS,BFS

. The way to prove the effect of throwing exceptions on performance in. Net core

JS 加法乘法错误解决 number-precision

纯数据业务的机器打电话进来时回落到了2G/3G
随机推荐
安全工程师发现PS主机重大漏洞 用光盘能在系统中执行任意代码
Let you understand bubble sorting (C language)
Generate statement is not synthesized
ELK - ElastAlert最大的坑
调整数组顺序使奇数位于偶数前面(C语言)
异或的妙用(C语言)
SQLServer连接数据库(中文表)部分数据乱码问题解决
iframe 传值
Problems encountered in installing mysql8 under centos7.x couldn't open file /etc/pki/rpm-gpg/rpm-gpg-key-mysql-2022
【LeetCode】494. Objective and (2 wrong questions)
解决Splunk kvstore “starting“ 问题
Zhouhongyi's speech at the China Network Security Annual Conference: 360 secure brain builds a data security system
近期使用nodejs pinyin包时遇到的问题
阶乘后的零(C语言)
Gestion de projets logiciels 7.1. Concept de base du calendrier du projet
Use of Chinese input method input event composition
Solve the problem of swagger document interface 404
Wechat web developers, how to learn web development
Typescript compilation options and configuration files
Splunk Bucket 背後的秘密