当前位置:网站首页>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)
}
}
本文为学习笔记的记录
边栏推荐
- etcd implements large-scale service governance application combat
- IO process thread -> process -> day4
- [OC学习笔记]weak的实现原理
- The crawler video crawl tools you get
- Stop mental exhaustion Daily sharing
- What is NoSQL?Databases for the cloud-scale future
- Biotin - LC - Hydrazide | CAS: 109276-34-8 | Biotin - LC - Hydrazide
- Application and case analysis of CASA model and CENTURY model
- What is the function of the import command of the page directive in JSP?
- 了解下C# 多线程
猜你喜欢

etcd implements large-scale service governance application combat

EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network

【电子电路】长按键拉低电平,适用在有休眠机制的MCU但是没有看门狗,一个按键多个功能场景下使用

如何建立私域流量?私域流量对企业有什么好处?

PostgreSQL learning summary (11) - PostgreSQL commonly used high-availability cluster solutions

Shell becomes canonical and variable

Biotin-C6-amine|N-biotinyl-1,6-hexanediamine|CAS: 65953-56-2

深度学习汇报(4)

文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds

Stop mental exhaustion Daily sharing
随机推荐
WebGPU 导入[1] - 入门常见问题与个人分享
Redis分布式锁
HCIP笔记第十三天
QT web development - Notes - 3
不用Swagger,那我用啥?
Shell becomes canonical and variable
力扣:第 304 场周赛
Biotin hydrazide HCl|CAS:66640-86-6|Biotin-hydrazide hydrochloride
积分商城商品供应商选择的三个要求
Codeforces Round #811 (Div. 3)无DF
大厂外包,值得拥有吗?
抓包工具Charles修改Response步骤
RetinaFace: Single-stage Dense Face Localisation in the Wild
PyCharm使用教程(较详细,图+文)
R语言plotly可视化:plotly可视化回归模型实际值和回归预测值的散点图分析回归模型的预测效能、一个好的模型大部分的散点在对角线附近(predicted vs actual)
二分类和多分类
IO process thread -> process -> day4
How Engineers Treat Open Source --- A veteran engineer's heartfelt words
Wang Xuegang - compiled shipment line file
Database triggers and transactions