flink bitmap去重并写入RabitMQ

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
简介: flink bitmap去重并写入RabitMQ

1:样例数据集

{"uuid": "7203d5f8a55c11ea9986a64728966615", "title": "暴乱持续发酵,特朗普3小时发16条推文,疫苗又有新进展?惊现巨量抛单,黄金闪跌20美元 | 早报", "url": "http://www.toutiao.com/item/6833955164274033159/","ctime": 1590019056, "utime": 0} 
{"uuid": "43f67f489aef11ea967c8e469741ac9c", "title": "汇添富成长多因子量化策略股票净值下跌1.55% 请保持关注", "url": "http://www.toutiao.com/item/6829065074540282376/","ctime": 1590019056, "utime": 0}                                     
{"uuid": "c44f94129aff11ea967c8e469741ac9c", "title": "上投摩根内需动力混合基金最新净值跌幅达1.66%", "url": "http://www.toutiao.com/item/6829079830701539847/","ctime": 1590019056, "utime": 0 }                                             
{"uuid": "c50c65929aff11ea967c8e469741ac9c", "title": "银华体育文化灵活配置混合基金最新净值跌幅达1.68%", "url": "http://www.toutiao.com/item/6829079830709928455/","ctime": 1590019056, "utime": 0}                                          
{"uuid": "2f18d7cea05d11ea9980a64728966615", "title": "68家上市游戏公司Q1财报梳理:36家净利润下滑,谁吃到了宅经济红利?", "url": "http://www.toutiao.com/item/6831618549249737229/","ctime": 1590019056, "utime": 0}                         
{"uuid": "c78a036a9aff11ea967c8e469741ac9c", "title": "长城中小盘成长混合基金最新净值跌幅达2.45%", "url": "http://www.toutiao.com/item/6829079832752554510/","ctime": 1590019056, "utime": 0}                                               
{"uuid": "602d7c2aa07111ea96878e469741ac9c", "title": "东方量化成长灵活配置混合基金最新净值跌幅达1.75%", "url": "http://www.toutiao.com/item/6831661455230632461/","ctime": 1590019056, "utime": 0}                                          
{"uuid": "c37db9b09aff11ea967c8e469741ac9c", "title": "融通蓝筹成长混合基金最新净值跌幅达1.66%", "url": "http://www.toutiao.com/item/6829079847638139399/","ctime": 1590019056, "utime": 0}                                                 
{"uuid": "5f0d2444a07111ea9980a64728966615", "title": "银华瑞和灵活配置混合基金最新净值跌幅达1.94%", "url": "http://www.toutiao.com/item/6831661456606364167/","ctime": 1590019056, "utime": 0}                                              
{"uuid": "65d4ba4c9b0011ea9972a64728966615", "title": "“宅经济”盛行 游戏上市公司一季度“成绩”逆势上涨", "url": "http://www.toutiao.com/item/6829086094345110029/","ctime": 1590019056, "utime": 0}                                            
{"uuid": "7203d5f8a55c11ea9986a64728966615", "title": "暴乱持续发酵,特朗普3小时发16条推文,疫苗又有新进展?惊现巨量抛单,黄金闪跌20美元 | 早报", "url": "http://www.toutiao.com/item/6833955164274033159/","ctime": 1590019056, "utime": 0} 
{"uuid": "43f67f489aef11ea967c8e469741ac9c", "title": "汇添富成长多因子量化策略股票净值下跌1.55% 请保持关注", "url": "http://www.toutiao.com/item/6829065074540282376/","ctime": 1590019056, "utime": 0}                                     
{"uuid": "c44f94129aff11ea967c8e469741ac9c", "title": "上投摩根内需动力混合基金最新净值跌幅达1.66%", "url": "http://www.toutiao.com/item/6829079830701539847/","ctime": 1590019056, "utime": 0 }                                             
{"uuid": "c50c65929aff11ea967c8e469741ac9c", "title": "银华体育文化灵活配置混合基金最新净值跌幅达1.68%", "url": "http://www.toutiao.com/item/6829079830709928455/","ctime": 1590019056, "utime": 0}                                          
{"uuid": "2f18d7cea05d11ea9980a64728966615", "title": "68家上市游戏公司Q1财报梳理:36家净利润下滑,谁吃到了宅经济红利?", "url": "http://www.toutiao.com/item/6831618549249737229/","ctime": 1590019056, "utime": 0}                         
{"uuid": "c78a036a9aff11ea967c8e469741ac9c", "title": "长城中小盘成长混合基金最新净值跌幅达2.45%", "url": "http://www.toutiao.com/item/6829079832752554510/","ctime": 1590019056, "utime": 0}                                               
{"uuid": "602d7c2aa07111ea96878e469741ac9c", "title": "东方量化成长灵活配置混合基金最新净值跌幅达1.75%", "url": "http://www.toutiao.com/item/6831661455230632461/","ctime": 1590019056, "utime": 0}                                          
{"uuid": "c37db9b09aff11ea967c8e469741ac9c", "title": "融通蓝筹成长混合基金最新净值跌幅达1.66%", "url": "http://www.toutiao.com/item/6829079847638139399/","ctime": 1590019056, "utime": 0}                                                 
{"uuid": "5f0d2444a07111ea9980a64728966615", "title": "银华瑞和灵活配置混合基金最新净值跌幅达1.94%", "url": "http://www.toutiao.com/item/6831661456606364167/","ctime": 1590019056, "utime": 0}                                              
{"uuid": "65d4ba4c9b0011ea9972a64728966615", "title": "“宅经济”盛行 游戏上市公司一季度“成绩”逆势上涨", "url": "http://www.toutiao.com/item/6829086094345110029/","ctime": 1590019056, "utime": 0}                                            
{"uuid": "7203d5f8a55c11ea9986a64728966615", "title": "暴乱持续发酵,特朗普3小时发16条推文,疫苗又有新进展?惊现巨量抛单,黄金闪跌20美元 | 早报", "url": "http://www.toutiao.com/item/6833955164274033159/","ctime": 1590019056, "utime": 0} 
{"uuid": "43f67f489aef11ea967c8e469741ac9c", "title": "汇添富成长多因子量化策略股票净值下跌1.55% 请保持关注", "url": "http://www.toutiao.com/item/6829065074540282376/","ctime": 1590019056, "utime": 0}                                     
{"uuid": "c44f94129aff11ea967c8e469741ac9c", "title": "上投摩根内需动力混合基金最新净值跌幅达1.66%", "url": "http://www.toutiao.com/item/6829079830701539847/","ctime": 1590019056, "utime": 0 }                                             
{"uuid": "c50c65929aff11ea967c8e469741ac9c", "title": "银华体育文化灵活配置混合基金最新净值跌幅达1.68%", "url": "http://www.toutiao.com/item/6829079830709928455/","ctime": 1590019056, "utime": 0}                                          
{"uuid": "2f18d7cea05d11ea9980a64728966615", "title": "68家上市游戏公司Q1财报梳理:36家净利润下滑,谁吃到了宅经济红利?", "url": "http://www.toutiao.com/item/6831618549249737229/","ctime": 1590019056, "utime": 0}                         
{"uuid": "c78a036a9aff11ea967c8e469741ac9c", "title": "长城中小盘成长混合基金最新净值跌幅达2.45%", "url": "http://www.toutiao.com/item/6829079832752554510/","ctime": 1590019056, "utime": 0}                                               
{"uuid": "602d7c2aa07111ea96878e469741ac9c", "title": "东方量化成长灵活配置混合基金最新净值跌幅达1.75%", "url": "http://www.toutiao.com/item/6831661455230632461/","ctime": 1590019056, "utime": 0}                                          
{"uuid": "c37db9b09aff11ea967c8e469741ac9c", "title": "融通蓝筹成长混合基金最新净值跌幅达1.66%", "url": "http://www.toutiao.com/item/6829079847638139399/","ctime": 1590019056, "utime": 0}                                                 
{"uuid": "5f0d2444a07111ea9980a64728966615", "title": "银华瑞和灵活配置混合基金最新净值跌幅达1.94%", "url": "http://www.toutiao.com/item/6831661456606364167/","ctime": 1590019056, "utime": 0}                                              
{"uuid": "65d4ba4c9b0011ea9972a64728966615", "title": "“宅经济”盛行 游戏上市公司一季度“成绩”逆势上涨", "url": "http://www.toutiao.com/item/6829086094345110029/","ctime": 1590019056, "utime": 0}                                            
{"uuid": "7203d5f8a55c11ea9986a64728966615", "title": "暴乱持续发酵,特朗普3小时发16条推文,疫苗又有新进展?惊现巨量抛单,黄金闪跌20美元 | 早报", "url": "http://www.toutiao.com/item/6833955164274033159/","ctime": 1590019056, "utime": 0} 
{"uuid": "43f67f489aef11ea967c8e469741ac9c", "title": "汇添富成长多因子量化策略股票净值下跌1.55% 请保持关注", "url": "http://www.toutiao.com/item/6829065074540282376/","ctime": 1590019056, "utime": 0}                                     
{"uuid": "c44f94129aff11ea967c8e469741ac9c", "title": "上投摩根内需动力混合基金最新净值跌幅达1.66%", "url": "http://www.toutiao.com/item/6829079830701539847/","ctime": 1590019056, "utime": 0 }                                             
{"uuid": "c50c65929aff11ea967c8e469741ac9c", "title": "银华体育文化灵活配置混合基金最新净值跌幅达1.68%", "url": "http://www.toutiao.com/item/6829079830709928455/","ctime": 1590019056, "utime": 0}                                          
{"uuid": "2f18d7cea05d11ea9980a64728966615", "title": "68家上市游戏公司Q1财报梳理:36家净利润下滑,谁吃到了宅经济红利?", "url": "http://www.toutiao.com/item/6831618549249737229/","ctime": 1590019056, "utime": 0}                         
{"uuid": "c78a036a9aff11ea967c8e469741ac9c", "title": "长城中小盘成长混合基金最新净值跌幅达2.45%", "url": "http://www.toutiao.com/item/6829079832752554510/","ctime": 1590019056, "utime": 0}                                               
{"uuid": "602d7c2aa07111ea96878e469741ac9c", "title": "东方量化成长灵活配置混合基金最新净值跌幅达1.75%", "url": "http://www.toutiao.com/item/6831661455230632461/","ctime": 1590019056, "utime": 0}                                          
{"uuid": "c37db9b09aff11ea967c8e469741ac9c", "title": "融通蓝筹成长混合基金最新净值跌幅达1.66%", "url": "http://www.toutiao.com/item/6829079847638139399/","ctime": 1590019056, "utime": 0}                                                 
{"uuid": "5f0d2444a07111ea9980a64728966615", "title": "银华瑞和灵活配置混合基金最新净值跌幅达1.94%", "url": "http://www.toutiao.com/item/6831661456606364167/","ctime": 1590019056, "utime": 0}                                              
{"uuid": "65d4ba4c9b0011ea9972a64728966615", "title": "“宅经济”盛行 游戏上市公司一季度“成绩”逆势上涨", "url": "http://www.toutiao.com/item/6829086094345110029/","ctime": 1590019056, "utime": 0}                                            

    数据说明:此样例数据共有40条,其中只有10条数据uuid不重复。

    2: 依赖导入

     <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-connector-rabbitmq -->
            <dependency>
                <groupId>org.apache.flink</groupId>
                <artifactId>flink-connector-rabbitmq_2.11</artifactId>
                <version>1.12.2</version>
                <scope>provided</scope>
            </dependency>

    3: 核心代码

    3.1 读取样例数据,并解析。通过ValueState结合bitmap 进行去重

    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.apache.flink.api.common.functions.MapFunction;
    import org.apache.flink.api.common.state.ValueState;
    import org.apache.flink.api.common.state.ValueStateDescriptor;
    import org.apache.flink.api.common.typeinfo.TypeHint;
    import org.apache.flink.api.common.typeinfo.TypeInformation;
    import org.apache.flink.api.java.tuple.Tuple;
    import org.apache.flink.api.java.tuple.Tuple10;
    import org.apache.flink.configuration.Configuration;
    import org.apache.flink.shaded.guava18.com.google.common.hash.BloomFilter;
    import org.apache.flink.shaded.guava18.com.google.common.hash.Funnels;
    import org.apache.flink.streaming.api.datastream.KeyedStream;
    import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
    import org.apache.flink.streaming.connectors.rabbitmq.RMQSink;
    import org.apache.flink.streaming.connectors.rabbitmq.common.RMQConnectionConfig;
    import org.apache.flink.streaming.util.serialization.SimpleStringSchema;
    import org.apache.flink.util.Collector;
    public class bitMap2MQ {
        public static void main(String[] args) throws Exception {
            //创建环境,设置参数
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            //读取数据源
            DataStreamSource<String> pulsarDS = env.readTextFile("/Users/mindezhi/Downloads/PublicSentiment/src/main/data/test2.txt");
            ObjectMapper mapper = new ObjectMapper();
            SingleOutputStreamOperator<Tuple5<String, String, String, String, String>> mapDS = pulsarDS.map(new MapFunction<String, Tuple5<String, String, String,String,String>>() {
                @Override
                public Tuple5<String, String, String, String, String> map(String value) throws Exception {
                    JsonNode jsonNode = mapper.readTree(value);
                    String uuid = (jsonNode.get("uuid") != null) ? jsonNode.get("uuid").toString() : "";
                    String ctime = (jsonNode.get("ctime") != null) ? jsonNode.get("ctime").toString() : "";
                    String utime = (jsonNode.get("utime") != null) ? jsonNode.get("utime").toString() : "";
                    String title = (jsonNode.get("title") != null) ? jsonNode.get("title").toString() : "";
                    String url = (jsonNode.get("url") != null) ? jsonNode.get("url").toString() : "";
                    return Tuple5.of(uuid, ctime, utime, title, url);
                }
            });
            KeyedStream<Tuple5<String, String, String, String, String>, Tuple> KeyedStream = mapDS.keyBy(1);
            final SingleOutputStreamOperator<Tuple5<String, String, String, String,String>> distinctProcess = KeyedStream.process(new KeyedProcessFunction<Tuple, Tuple5<String, String, String, String, String>, Tuple5<String, String, String, String, String>>() {
                // 定义一个状态,用来存放BloomFilter
                private transient ValueState<BloomFilter> uidState;
                @Override
                public void open(Configuration parameters) throws Exception {
                    // 定义一个状态描述器 保存BloomFilter
                    ValueStateDescriptor<BloomFilter> stateDescriptor1 = new ValueStateDescriptor(
                            "uid-stated",
                            TypeInformation.of(new TypeHint<BloomFilter>() {
                            })
                    );
                    // 获取历史状态
                    uidState = getRuntimeContext().getState(stateDescriptor1);
                }
                @Override
                public void processElement(Tuple5<String,String, String, String, String> value, Context ctx, Collector<Tuple10<String, String, String, String, String, String, String, String, String, String>> out) throws Exception {
                    String uuid = value.f0;
                    String ctime = value.f1;
                    String utime = value.f2;
                    String title = value.f3;
                    String url = value.f4;
                    // 使用BloomFilter判断去重
                    BloomFilter bloomFilter = uidState.value();
                    if (bloomFilter == null) {
                        bloomFilter = BloomFilter.create(Funnels.unencodedCharsFunnel(), 10000000);
                    }
                    // 如果数据不存在,则将数据添加到bloomFilter中
                    if (!bloomFilter.mightContain(uuid)&&!bloomFilter.mightContain(title)) {
                        bloomFilter.put(uuid);
                        uidState.update(bloomFilter);
                        out.collect(Tuple5.of(uuid, ctime, utime, title, url));
                    }
                }
            });

    4: 定义rabbitMQ sink,由于flink-connector-rabbitmq 发送数据是以string类型发送,在此需要,将数据封装成json形式的字符串。

             // 封装成json形式的String        
            SingleOutputStreamOperator<String> mapDs = distinctProcess.map(new MapFunction<Tuple5<String, String, String, String, String>, String>() {
                @Override
                public String map(Tuple5< String, String, String, String, String> value) throws Exception {
                    String str = "{ " +
                            "\"msg_header\":{" +
                            "\"uuid\":" + value.f0 + "," +
                            "\"ctime\":" + value.f1 + "," +
                            "\"utime\":" + value.f2 + "," +
                            "\"duplicate_filter\":0," +
                            "\"duplicate\":0}," +
                            "\"msg_body\":{" +
                            "\"title\":" + value.f3 + "," +
                            "\"stocks\":[]," +
                            "\"url\":" + value.f4 + "," +
                            "\"tags\":[]" +
                            "}}";
                    return str;
                }
            });
            RMQConnectionConfig connectionConfig = new RMQConnectionConfig
                    .Builder().setHost("localhost").setVirtualHost("/")
                    .setPort(5672).setUserName("rabbitMQ").setPassword("xxx@2020")
                    .build();
            //注意,换一个新的 queue,否则也会报错
            mapDs.addSink(new RMQSink(connectionConfig, "disinct_test",new SimpleStringSchema()));
            env.execute();
        }
    }

    相关实践学习
    基于Hologres轻松玩转一站式实时仓库
    本场景介绍如何利用阿里云MaxCompute、实时计算Flink和交互式分析服务Hologres开发离线、实时数据融合分析的数据大屏应用。
    Linux入门到精通
    本套课程是从入门开始的Linux学习课程,适合初学者阅读。由浅入深案例丰富,通俗易懂。主要涉及基础的系统操作以及工作中常用的各种服务软件的应用、部署和优化。即使是零基础的学员,只要能够坚持把所有章节都学完,也一定会受益匪浅。
    相关文章
    |
    5月前
    |
    Java 数据处理 Apache
    实时计算 Flink版产品使用问题之lookup Join hologres的维表,是否可以指定查bitmap
    实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
    |
    存储 关系型数据库 MySQL
    怎么使用 Flink 向 Apache Doris 表中写 Bitmap 类型的数据
    怎么使用 Flink 向 Apache Doris 表中写 Bitmap 类型的数据
    333 0
    |
    SQL 流计算
    Flink SQL 功能解密系列 —— 数据去重的技巧和思考
    去重逻辑在业务处理中使用广泛,大致可以分两类:DISTINCT去重和FIRST_VALUE主键去重,两者的区别是DISTINCT去重是对整行数据进行去重,比如tt里面数据可能会有重复,我们要去掉重复的数据;FIRST_VALUE是根据主键进行去重,可以看成是一种业务层面的去重,但是真实的业务场景使用也很普遍,比如一个用户有多次点击,业务上只需要取第一条。
    实时计算Flink版 + Hologres,亿级用户实时 UV 精确去重最佳实践
    使用实时计算Flink版 + Hologres 方式,并基于 RoaringBitmap,实时对用户标签去重。
    实时计算Flink版 + Hologres,亿级用户实时 UV 精确去重最佳实践
    |
    消息中间件 SQL 监控
    Flink+Hologres亿级用户实时UV精确去重最佳实践
    Flink+Hologres亿级用户实时UV精确去重最佳实践
    2047 0
    Flink+Hologres亿级用户实时UV精确去重最佳实践
    |
    2月前
    |
    运维 数据处理 数据安全/隐私保护
    阿里云实时计算Flink版测评报告
    该测评报告详细介绍了阿里云实时计算Flink版在用户行为分析与标签画像中的应用实践,展示了其毫秒级的数据处理能力和高效的开发流程。报告还全面评测了该服务在稳定性、性能、开发运维及安全性方面的卓越表现,并对比自建Flink集群的优势。最后,报告评估了其成本效益,强调了其灵活扩展性和高投资回报率,适合各类实时数据处理需求。
    |
    4月前
    |
    存储 监控 大数据
    阿里云实时计算Flink在多行业的应用和实践
    本文整理自 Flink Forward Asia 2023 中闭门会的分享。主要分享实时计算在各行业的应用实践,对回归实时计算的重点场景进行介绍以及企业如何使用实时计算技术,并且提供一些在技术架构上的参考建议。
    836 7
    阿里云实时计算Flink在多行业的应用和实践
    |
    23天前
    |
    存储 分布式计算 流计算
    实时计算 Flash – 兼容 Flink 的新一代向量化流计算引擎
    本文介绍了阿里云开源大数据团队在实时计算领域的最新成果——向量化流计算引擎Flash。文章主要内容包括:Apache Flink 成为业界流计算标准、Flash 核心技术解读、性能测试数据以及在阿里巴巴集团的落地效果。Flash 是一款完全兼容 Apache Flink 的新一代流计算引擎,通过向量化技术和 C++ 实现,大幅提升了性能和成本效益。
    806 17
    实时计算 Flash – 兼容 Flink 的新一代向量化流计算引擎
    |
    3月前
    |
    SQL 消息中间件 Kafka
    实时计算 Flink版产品使用问题之如何在EMR-Flink的Flink SOL中针对source表单独设置并行度
    实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
    |
    20天前
    |
    SQL 运维 数据可视化
    阿里云实时计算Flink版产品体验测评
    阿里云实时计算Flink基于Apache Flink构建,提供一站式实时大数据分析平台,支持端到端亚秒级实时数据分析,适用于实时大屏、实时报表、实时ETL和风控监测等场景,具备高性价比、开发效率、运维管理和企业安全等优势。

    热门文章

    最新文章

    下一篇
    无影云桌面