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

边栏推荐
- yapi安装
- Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr
- JS addition and multiplication error resolution number precision
- Guangdong municipal safety construction data management software 2022 new forms are coming
- 解决swagger文档接口404的问题
- Yapi installation
- 【LeetCode】494. Objective and (2 wrong questions)
- Gestion de projets logiciels 7.1. Concept de base du calendrier du projet
- 中国联通 22春招 群面
- 爱可可AI前沿推介(6.11)
猜你喜欢

Let you understand selection sorting (C language)

ELK - Hearthbeat实现服务监控

centos安装mysql5.7
Use cache to reduce network requests

Zero after factorial (C language)

mysql 导入宝塔中数据库data为0000-00-00,enum为null出错

Splunk最佳实践之workload managment

C# 在PDF文档中应用多种不同字体

Jest unit test description config json

Uncaught TypeError: Cannot set property ‘next‘ of undefined 报错解决
随机推荐
Notes on brushing questions (13) -- binary tree: traversal of the first, middle and last order (review)
爱可可AI前沿推介(6.11)
广东市政安全施工资料管理软件2022新表格来啦
Elk - x-pack set user password
Iframe value transfer
splunk 证书过期 使KV-store不能启动
JS 加法乘法错误解决 number-precision
When the security engineer finds a major vulnerability in the PS host, the CD can execute arbitrary code in the system
Use compiler option '--downleveliteration' to allow iteration of iterations
进度条加载
InputStream读取文件OutputStream创建文件
2019 book list
yapi安装
Splunk最佳实践之workload managment
Merge two ordered arrays (C language)
saltstack安装与使用
Queuing theory model
Read geo expression matrix
Pan domain SSL certificate, sectigo cheap wildcard certificate popularization plan
《公司理财师专业能力》笔记