大数据-玩转数据-Flink App市场推广统计

一、说明

电商网站中已经有越来越多的用户来自移动端,相比起传统浏览器的登录方式,手机APP成为了更多用户访问电商网站的首选。对于电商企业来说,一般会通过各种不同的渠道对自己的APP进行市场推广,而这些渠道的统计数据(比如,不同网站上广告链接的点击量、APP下载量)就成了市场营销的重要商业指标。

二、思路

统计 不同渠道的不同用户行为

三、数据准备

封装数据的JavaBean类

java 复制代码
package com.lyh.flink06;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MarketingUserBehavior {
    private Long userId;
    private String behavior;
    private String channel;
    private Long timestamp;
}

四、代码

java 复制代码
package com.lyh.flink06;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Random;

public class Project_AppAnalysis_By_Chanel {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        env.addSource(new AppSource())
                .map(new MapFunction<MarketingUserBehavior, Tuple2<String,Long>>() {
                    @Override
                    public Tuple2<String, Long> map(MarketingUserBehavior value) throws Exception {
                        return Tuple2.of(value.getBehavior() + "_" + value.getChannel(),1L);
                    }
                }).keyBy(0)
                .sum(1)
        .print();
         env.execute();
    }
    public static class AppSource implements SourceFunction<MarketingUserBehavior> {

        @Override
        public void run(SourceContext<MarketingUserBehavior> ctx) throws Exception {
            Random random = new Random();
            String[] behaviors = {"update", "install", "uninstall", "update"};
            String[] chanels ={"HW","VIVO","XIAOMI","OPPO"};

            while (true){

                Long userId = (long)(random.nextInt(2000) + 1);
                String behavior = behaviors[random.nextInt(behaviors.length)];
                String chanel = chanels[random.nextInt(chanels.length)];
                Long timstampes = System.currentTimeMillis();


                ctx.collect(new MarketingUserBehavior(
                        userId,
                        behavior,
                        chanel,
                        timstampes

                ));
                Thread.sleep(200);
            }

        }

        @Override
        public void cancel() {
        }
    }

}

五、运行结果

相关推荐
无级程序员2 小时前
大数据平台之ranger与ldap集成,同步用户和组
大数据·hadoop
lifallen3 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
TDengine (老段)4 小时前
TDengine 数据库建模最佳实践
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
张先shen4 小时前
Elasticsearch RESTful API入门:全文搜索实战(Java版)
java·大数据·elasticsearch·搜索引擎·全文检索·restful
Elastic 中国社区官方博客4 小时前
Elasticsearch 字符串包含子字符串:高级查询技巧
大数据·数据库·elasticsearch·搜索引擎·全文检索·lucene
张先shen5 小时前
Elasticsearch RESTful API入门:全文搜索实战
java·大数据·elasticsearch·搜索引擎·全文检索·restful
expect7g5 小时前
Flink-Checkpoint-2.OperatorChain
后端·flink
天翼云开发者社区5 小时前
Doris-HDFS LOAD常见问题汇总(二)
大数据·doris
简婷187019987756 小时前
源网荷储 + 零碳园区:一场关于能源与未来的双向奔赴
大数据·人工智能·能源
2201_756776776 小时前
网络安全初级
大数据·elasticsearch·搜索引擎