Flink中序列化RoaringBitmap不同方式的对比

背景

在flink中,我们有时候会使用到RoaringBitmap进行统计计数等操作,而当使用RoaringBitmap时,这就涉及到了最重要的问题,如何序列化?序列化的目的是为了进行网络通信或者状态序列化的目的,本文的重点是比较kryo使用默认的序列化器序列化RoaringBitmap和自定义序列化器序列化RoaringBitmap的性能对比

性能对比

当在flink中使用RoaringBitmap时,flink自身携带的序列化器是没法处理这个类的序列化的,只能交给kryo进行序列化,而kryo都是使用FieldSerializer来对对象进行序列化,当kryo对RoaringBitmap类进行序列化时,他会对里面的

的每个字段分别调用对应的序列化器进行序列化/反序列化,但是其实这样的性能不高,因为其实不是这里面数组里面的每个元素都需要序列化,而是可以根据情况来决定的,所以RoaringBitmap本身提供了serializer/deserializer方法,这相比于直接序列化每个字段有极大的性能提升,所以我们这里需要实现自己的kryo序列化器来直接使用RoaringBitmap提供的serializer/deserializer方法,以下是得到的性能测试结果:

附测试代码:

java 复制代码
    @Benchmark @OperationsPerInvocation(value = SerializationFrameworkMiniBenchmarks.RECORDS_PER_INVOCATION_SER)
    public void serializerKryoBitmap(FlinkEnvironmentContext context) throws Exception {
        StreamExecutionEnvironment env = context.env;
        env.setParallelism(4);
        ExecutionConfig executionConfig = env.getConfig();
        executionConfig.enableForceKryo();

        env.addSource(new BitMapWrapperSource(RECORDS_PER_INVOCATION_SER, 10)).rebalance()
                .addSink(new SelfBitMapSink<BitMapWrapper>());
        env.execute();
    }

    @Benchmark @OperationsPerInvocation(value = SerializationFrameworkMiniBenchmarks.RECORDS_PER_INVOCATION_SER)
    public void serializerKryoBitmapSerializer(FlinkEnvironmentContext context) throws Exception {
        StreamExecutionEnvironment env = context.env;
        env.setParallelism(4);
        ExecutionConfig executionConfig = env.getConfig();
        executionConfig.enableForceKryo();
        executionConfig.registerTypeWithKryoSerializer(RoaringBitmap.class, BitMapSerializer.class);

        env.addSource(new BitMapWrapperSource(RECORDS_PER_INVOCATION_SER, 10)).rebalance()
                .addSink(new SelfBitMapSink<BitMapWrapper>());
        env.execute();
    }
  /**
     * RoaringBitmapSource
     */
    public static class BitMapWrapperSource extends BaseSourceWithKeyRange<BitMapWrapper> {
        private static final long serialVersionUID = 2941333602938145599L;

        private transient BitMapWrapper template;

        public BitMapWrapperSource(int numEvents, int numKeys) {
            super(numEvents, numKeys);
        }

        @Override protected void init() {
            super.init();
            template = initNewBitMapWrapper(0);
        }


        @Override protected BitMapWrapper getElement(int keyId) {
            template.setId(keyId);
            return template;
        }

        private BitMapWrapper initNewBitMapWrapper(int keyId) {
            BitMapWrapper template = new BitMapWrapper();
            RoaringBitmap r32 = new RoaringBitmap();
            for (int i = 0; i < BITMAP_INIT_NUM / 2; i++) {
                r32.add(1000000 + i);
            }
            for (int i = BITMAP_INIT_NUM / 2; i < BITMAP_INIT_NUM; i++) {
                r32.add(9000000 + i);
            }
            r32.runOptimize();
            template.setR1(r32);
            template.setId(keyId);
            return template;
        }
    }
    public class SelfBitMapSink<BitMapWrapper> implements SinkFunction<org.apache.flink.benchmark.selfbitmap.BitMapWrapper>, SupportsConcurrentExecutionAttempts {

    private static final long serialVersionUID = 1L;

    public SelfBitMapSink() {
    }

    public void invoke(org.apache.flink.benchmark.selfbitmap.BitMapWrapper value) throws Exception{
        if (value.getId() > 10 || value.getId() < 0) {
            throw new Exception("id is illegal" + value.getId());
        }
        if(value.getR1().getCardinality() != SerializationFrameworkMiniBenchmarks.BITMAP_INIT_NUM){
            throw new Exception("bitmap error" + value.getR1().getCardinality());
        }
    }
}
相关推荐
万米商云1 小时前
企业物资集采平台解决方案:跨地域、多仓库、百部门——大型企业如何用一套系统管好百万级物资?
大数据·运维·人工智能
BigData共享1 小时前
极致性能背后的黑科技?这个世上没有“银弹”!(三)
大数据
阿里云大数据AI技术1 小时前
Flink Forward Asia 2025 主旨演讲精彩回顾
大数据·人工智能·flink
y_y_liang1 小时前
图生生AI商品换背景,高效商拍!
大数据·人工智能·ai·ai作画
王小王-1232 小时前
基于Hadoop的用户购物行为可视化分析系统设计与实现
大数据·hadoop·分布式·用户购物行为·电商日志分析
沐尘而生2 小时前
【AI智能体】智能音视频-硬件设备基于 WebSocket 实现语音交互
大数据·人工智能·websocket·机器学习·ai作画·音视频·娱乐
贝格前端工场2 小时前
小程序订阅消息设计:用户触达与隐私保护的平衡法则
大数据·小程序
成都极云科技3 小时前
成都算力租赁新趋势:H20 八卡服务器如何重塑 AI 产业格局?
大数据·服务器·人工智能·云计算·gpu算力
典学长编程4 小时前
高效学习之一篇搞定分布式管理系统Git !
大数据·git·搜索引擎
YFJ_mily6 小时前
2025第二届机电一体化、机器人与控制系统国际会议(MRCS2025)即将来袭
大数据·人工智能·机器人·机电一体化