当前位置:网站首页>spark:热门品类中每个品类活跃的SessionID统计TOP10(案例)
spark:热门品类中每个品类活跃的SessionID统计TOP10(案例)
2022-08-02 08:28:00 【一个人的牛牛】
目录
介绍
session:服务器为了保存用户状态而创建的一个特殊的对象。
浏览器第一次访问服务器时,服务器创建一个session对象,该对象有一个唯一的id,一般称之为sessionId,服务器会将sessionId以cookie的方式发送给浏览器。当浏览器再次访问服务器时,会将sessionId发送过来,服务器依据sessionId就可以找到对应的session对象。
sessionID:用来判断是同一次会话。
服务器端的session只要还在同一个生命期内就还是同一次会话。
数据准备
点击链接下载数据(免费下载)
14万条用户行为数据,搜索、点击、下单、支付-spark文档类资源-CSDN下载
数据说明:
时间_用户ID_sessionID_页面ID_动作时间_搜索_点击(品类ID、产品ID)_下单(品类ID、产品ID)_支付(品类ID、产品ID)_城市ID
代码实现
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}object TopTwo {
def main(args: Array[String]): Unit = {// TODO : 热门品类中每个品类的Session TOP10统计
val sparConf = new SparkConf().setMaster("local[*]").setAppName("TOP")
val sc = new SparkContext(sparConf)val actionRDD = sc.textFile("datas/action.txt")
actionRDD.cache()
val top10Ids: Array[String] = top10Category(actionRDD)//过滤原始数据,保留点击和前10品类ID
val filterActionRDD = actionRDD.filter(
action => {
val datas = action.split("_")
if ( datas(6) != "-1" ) {
top10Ids.contains(datas(6))
} else {
false
}
}
)
//根据品类ID和sessionid进行点击量的统计
val reduceRDD: RDD[((String, String), Int)] = filterActionRDD.map(
action => {
val datas = action.split("_")
((datas(6), datas(2)), 1)
}
).reduceByKey(_ + _)
//将统计的结果进行结构的转换
// (( 品类ID,sessionId ),sum) => ( 品类ID,(sessionId, sum) )
val mapRDD = reduceRDD.map{
case ( (cid, sid), sum ) => {
( cid, (sid, sum) )
}
}
//相同的品类进行分组
val groupRDD: RDD[(String, Iterable[(String, Int)])] = mapRDD.groupByKey()
//将分组后的数据进行点击量的排序,取前10名
val resultRDD = groupRDD.mapValues(
iter => {
iter.toList.sortBy(_._2)(Ordering.Int.reverse).take(10)
}
)resultRDD.collect().foreach(println)
sc.stop()
}
def top10Category(actionRDD:RDD[String]) = {
val flatRDD: RDD[(String, (Int, Int, Int))] = actionRDD.flatMap(
action => {
val datas = action.split("_")
if (datas(6) != "-1") {
// 点击的场合
List((datas(6), (1, 0, 0)))
} else if (datas(8) != "null") {
// 下单的场合
val ids = datas(8).split(",")
ids.map(id => (id, (0, 1, 0)))
} else if (datas(10) != "null") {
// 支付的场合
val ids = datas(10).split(",")
ids.map(id => (id, (0, 0, 1)))
} else {
Nil
}
}
)val analysisRDD = flatRDD.reduceByKey(
(t1, t2) => {
( t1._1+t2._1, t1._2 + t2._2, t1._3 + t2._3 )
}
)analysisRDD.sortBy(_._2, false).take(10).map(_._1)
}
}
本文为学习笔记的记录
边栏推荐
- 科技云报道:实现元宇宙,英伟达从打造基础建设平台开始
- Biotinyl Cystamine | CAS: 128915-82-2 | biotin cysteamine
- (Note) AXIS ACASIS DT-3608 Dual-bay Hard Disk Array Box RAID Setting
- Business Intelligence Platform BI Business Intelligence Analysis Platform How to Choose the Right Business Intelligence Platform BI
- C语言_条件编译
- Write a small game in C (three chess)
- Technology Cloud Report: To realize the metaverse, NVIDIA starts from building an infrastructure platform
- 王学岗-编译出运行的文件
- “蔚来杯“2022牛客暑期多校训练营4
- pycharm的基本使用教程(1)
猜你喜欢
Redis分布式锁入门
PostgreSQL学习总结(11)—— PostgreSQL 常用的高可用集群方案
Mysql Mac版下载安装教程
工程师如何对待开源 --- 一个老工程师的肺腑之言
Pycharm (1) the basic use of tutorial
postman下载安装汉化及使用
Three types of [OC learning notes] Block
查看变量的数据格式
(Note) AXIS ACASIS DT-3608 Dual-bay Hard Disk Array Box RAID Setting
PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image
随机推荐
unity pdg 设置隐藏不需要的节点以及实现自动勾选自动加载项
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
软件测试技术之解析图灵测试离我们还有多远
C语言基础_共用体
High imitation [Huawei consumer business official website] and wonderful animation analysis: practice embedding JS code in low-code platform
主流监控系统工具选型及落地场景参考
Flink 监控指南 被动拉取 Rest API
【C】关于柔性数组.简要的谈谈柔性数组
知识点滴 - 为什么一般不用铜锅做菜
自定义View实现波浪荡漾效果
oracle的sql改成mysql版本
pycharm的基本使用教程(1)
A young man with strong blood and energy actually became a housekeeper. How did he successfully turn around and change careers?
C语言_指针
OneinStack多版本PHP共存
王学岗-编译出运行的文件
Ansible 学习总结(11)—— task 并行执行之 forks 与 serial 参数详解
Redis分布式锁
Ansible learning summary (11) - detailed explanation of forks and serial parameters of task parallel execution
day——05 迭代器,生成器