一、说明
电商网站中已经有越来越多的用户来自移动端,相比起传统浏览器的登录方式,手机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() {
}
}
}