flink keyby使用与总结 基础片段梳理

文章目录


提示:以下是本篇文章正文内容,下面案例可供参考

一、KeyBy的源码分析

总结:

保证key相同的一定进入到一个分区内,但是一个分区内可以有多key的数据;

是对数据进行实时的分区,不是上游发送给下游,而是将数据写入到对应的channel的缓存中,下游到上游实时拉取;

keyBy底层是new KeyedStream,然后将父DataStream包起来,并且传入keyBy的条件(keySelector);

最终会调用KeyGroupStreamPartitioner的selectChannel方法,将keyBy的条件的返回值传入到该方法中;

流程:

1.先计算key的HashCode值(有可能会是负的)

2将key的HashCode值进行特殊的hash处理,MathUtils.murmurHash(keyHash),一定返回正数,避免返回的数字为负

3.将返回特特殊的hash值模除以默认最大并行的,默认是128,得到keyGroupId

4.keyGroupId * parallelism(程序的并行度) / maxParallelism(默认最大并行),返回分区编号

注意:1.如果将自定义POJO当成key,必须重新hashcode方法,2.不能将数组当成keyBy的key

java 复制代码
public <K> KeyedStream<T, K> keyBy(KeySelector<T, K> key) {
        Preconditions.checkNotNull(key);
        return new KeyedStream<>(this, clean(key));
    }


public KeyedStream(
            DataStream<T> dataStream,
            KeySelector<T, KEY> keySelector,
            TypeInformation<KEY> keyType) {
        this(
                dataStream,
                new PartitionTransformation<>(
                        dataStream.getTransformation(),
                        new KeyGrouppublic KeyGroupStreamPartitioner(KeySelector<T, K> keySelector, int maxParallelism) {
        Preconditions.checkArgument(maxParallelism > 0, "Number of key-groups must be > 0!");
        this.keySelector = Preconditions.checkNotNull(keySelector);
        this.maxParallelism = maxParallelism;
    }

    public int getMaxParallelism() {
        return maxParallelism;
    }

    @Override
    public int selectChannel(SerializationDelegate<StreamRecord<T>> record) {
        K key;
        try {
            key = keySelector.getKey(record.getInstance().getValue());
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not extract key from " + record.getInstance().getValue(), e);
        }
        return KeyGroupRangeAssignment.assignKeyToParallelOperator(
                key, maxParallelism, numberOfChannels);
    }StreamPartitioner<>(
                                keySelector,
                                StreamGraphGenerator.DEFAULT_LOWER_BOUND_MAX_PARALLELISM)),
                                keySelector,
                                keyType);
    }

public static int assignKeyToParallelOperator(Object key, int maxParallelism, int parallelism) {
        Preconditions.checkNotNull(key, "Assigned key must not be null!");
        return computeOperatorIndexForKeyGroup(maxParallelism, parallelism, assignToKeyGroup(key, maxParallelism));
    }

    public static int assignToKeyGroup(Object key, int maxParallelism) {
        Preconditions.checkNotNull(key, "Assigned key must not be null!");
        return computeKeyGroupForKeyHash(key.hashCode(), maxParallelism);
    }
    
// 将key的HashCode值进行特殊的hash处理,MathUtils.murmurHash(keyHash),一定返回正数,避免返回的数字为负
public static int computeKeyGroupForKeyHash(int keyHash, int maxParallelism) {
        return MathUtils.murmurHash(keyHash) % maxParallelism;
    }
// keyGroupId * parallelism(程序的并行度) / maxParallelism(默认最大并行),返回分区编号
public static int computeKeyGroupForKeyHash(int keyHash, int maxParallelism) {
        return MathUtils.murmurHash(keyHash) % maxParallelism;
    }
相关推荐
boppu3 小时前
布草特殊污渍去渍剂的种类及作用
大数据·人工智能
Achou.Wang4 小时前
深入理解go语言-第5章 并发编程——Go的灵魂
大数据·算法·golang
事变天下7 小时前
迈瑞的足球之夏:从一次救援到全球守护
大数据·科技
中微极客8 小时前
多智能体编排实战:CrewAI vs AutoGen(2026版)
大数据·网络·人工智能
西邮彭于晏8 小时前
图文详解:Git分支创建、合并与冲突解决|新手零门槛完整教程
大数据·git·elasticsearch
IanSkunk10 小时前
企业AI办公落地技术拆解:从WorkBuddy任务引擎到JOTO实施路径
大数据·人工智能
上海心泾国际物流有限公司10 小时前
2024-2026年进出口报关政策五大变化
大数据·经验分享·交通物流
Xvisio诠视科技10 小时前
从“感知”到“理解”:空间计算如何成为具身智能世界模型的“数据底座”?
大数据·空间计算
jerryinwuhan11 小时前
数据预处理技术 2026-2027-1 开篇-课程介绍
大数据·python
Elastic 中国社区官方博客11 小时前
Elastic 和 OpenAI 合作,将前沿智能引入非结构化企业数据
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai