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;
    }
相关推荐
原神启动17 分钟前
云计算大数据——shell教程(三剑客之sed)
大数据
小高学习java1 小时前
Canal、Elasticsearch、RabbitMq构建高可用、高性能的异构数据同步方案(亲测可用!!!!)
大数据·elasticsearch·rabbitmq·java-rabbitmq
_OP_CHEN1 小时前
算法基础篇:(十二)基础算法之倍增思想:从快速幂到大数据运算优化
大数据·c++·算法·acm·算法竞赛·倍增思想
武子康2 小时前
大数据-159 Apache Kylin Cube 实战:Hive 装载与预计算加速(含 Cuboid/实时 OLAP,Kylin 4.x)
大数据·后端·apache kylin
lisw052 小时前
边缘计算与云计算!
大数据·人工智能·机器学习·云计算·边缘计算
森语林溪2 小时前
数据“洪灾”变“水利”——古人“格物致知”的大数据实践
大数据
一只会写代码的猫2 小时前
云计算与边缘计算的融合:推动数字化转型的核心力量
flink
Hello.Reader3 小时前
Flink CDC 用 Db2 CDC 实时同步数据到 Elasticsearch
大数据·elasticsearch·flink
老蒋新思维4 小时前
创客匠人 2025 高峰论谈(11.22-25):AI 智能体重构创始人 IP 打造与知识变现的管理逻辑
大数据·网络·人工智能·网络协议·tcp/ip·重构·知识付费
TDengine (老段)6 小时前
TDengine 字符串函数 TO_BASE64 用户手册
android·大数据·服务器·物联网·时序数据库·tdengine·涛思数据