求uv

uv定义

在同一天内,UV指记录第一次进入网站的具有独立IP的访问者,在同一天内再次访问该网站则不计数.

代码实现:方法一

SparkConf conf = new SparkConf();
conf.setMaster("local").setAppName("uv");
JavaSparkContext jsc = new JavaSparkContext(conf);
JavaRDD<String> lineRDD = jsc.textFile("data/pvuvdata.txt");
lineRDD.flatMap(new FlatMapFunction<String, Tuple2<String, String>>() {
@Override
public Iterator<Tuple2<String, String>> call(String s) throws Exception {
String[] str = s.split(" ");
Tuple2<String, String> tuple2= new Tuple2<String, String>(str[5], str[0]);
List<Tuple2<String, String>> list = new ArrayList<>();
list.add(tuple2);
return list.iterator();
}
}).distinct()
.mapToPair(new PairFunction<Tuple2<String, String>, String, Integer>() {
@Override
public Tuple2<String, Integer> call(Tuple2<String, String> tuple2) throws Exception {
return new Tuple2<String, Integer>(tuple2._1, 1);
}
})
.reduceByKey(new Function2<Integer, Integer, Integer>() {
@Override
public Integer call(Integer integer, Integer integer2) throws Exception {
return integer + integer2;
}
}).foreach(new VoidFunction<Tuple2<String, Integer>>() {
@Override
public void call(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
System.out.println(stringIntegerTuple2);
}
});
jsc.stop();

代码实现:方法二

使用countByKey
posted @   jsqup  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示