当前位置:网站首页>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();
}
}

边栏推荐
- Solve the problem of swagger document interface 404
- 记一次 mysql 主从不同步问题排查
- yapi安装
- flink GROUPING SETS多维度聚合、设置Table state 到期时间
- Live source code, floating window rolling gradient effect
- Hamiltonian graph
- Is the SSL certificate reliable in ensuring the information security of the website?
- Memory mapping image of the grayloc module in the surfacefinder process
- YARN 切换ResourceManager(Failed to connect to server:8032 retries get failed due to exceeded maximum)
- Streaking? Baa!
猜你喜欢

广东市政安全施工资料管理软件2022新表格来啦

Elk - x-pack set user password

Streaking? Baa!

Specflow环境搭建

Elk - hearthbeat implements service monitoring

微信web开发者,如何学习web开发

Hang up the interviewer
![[Chapter II Relationship between genes and chromosomes] summary of biological knowledge - Biology in grade one of senior high school](/img/f0/9f78682798a7874ba176797a6b22ca.png)
[Chapter II Relationship between genes and chromosomes] summary of biological knowledge - Biology in grade one of senior high school
![[JUC supplementary] immutable object, shared meta mode, final principle](/img/c1/c29229108a3f66b83d13b4d90d49f7.jpg)
[JUC supplementary] immutable object, shared meta mode, final principle

SQLServer连接数据库(中文表)部分数据乱码问题解决
随机推荐
Using fast and slow pointer method to solve the problem of array (C language)
ELK - Hearthbeat实现服务监控
saltstack安装与使用
ELK - ElastAlert最大的坑
POJ 3278 catch the cow (width first search, queue implementation)
爱可可AI前沿推介(6.11)
中间人攻击之ettercap嗅探
Solve the problem of swagger document interface 404
. The way to prove the effect of throwing exceptions on performance in. Net core
flink GROUPING SETS多维度聚合、设置Table state 到期时间
Is the SSL certificate reliable in ensuring the information security of the website?
苹果MobileOne: 移动端仅需1ms的高性能骨干
Use of RadioButton in QT
程序员常用的命令符
Software project management 7.1 Basic concept of project schedule
Splunk最佳实践之workload managment
(推荐)splunk 多少数量search head 才合适
Four ways to create threads
Splunk健康检查orphaned searches
log4j-slf4j-impl cannot be present with log4j-to-slf4j