当前位置:网站首页>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)
}
}
本文为学习笔记的记录
边栏推荐
- PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image
- pnpm:简介
- [ansible] playbook explains the execution steps in combination with the project
- Biotin-C6-amine|N-biotinyl-1,6-hexanediamine|CAS: 65953-56-2
- day_05 time 模块
- Redisson实现分布式锁
- 第3周学习:ResNet+ResNeXt
- uvm-phase机制
- oracle的sql改成mysql版本
- 科技云报道:实现元宇宙,英伟达从打造基础建设平台开始
猜你喜欢

cas: 139504-50-0 Maytansine DM1|Mertansine|
![52. [Bool type input any non-0 value is not 1 version reason]](/img/73/c4e0048c504e0df073a6d07cfec3ab.png)
52. [Bool type input any non-0 value is not 1 version reason]

C语言_指针

编程与哲学(2)——输出是为了更好的输入

Business Intelligence Platform BI Business Intelligence Analysis Platform How to Choose the Right Business Intelligence Platform BI

三维体尺测量

mysql 中 in 的用法

ORBSLAM代码阅读

etcd实现大规模服务治理应用实战

PostgreSQL学习总结(11)—— PostgreSQL 常用的高可用集群方案
随机推荐
Mysql Mac版下载安装教程
PyQt5(一) PyQt5安装及配置,从文件夹读取图片并显示,模拟生成素描图像
AI目标分割能力,无需绿幕即可实现快速视频抠图
EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
What is the function of page directive contentPage/pageEncoding in JSP page?
【开源项目】X-TRACK源码分析
day_05模块
R语言plotly可视化:plotly可视化回归模型实际值和回归预测值的散点图分析回归模型的预测效能、一个好的模型大部分的散点在对角线附近(predicted vs actual)
为什么都推荐使用wordpress, 而不是 phpcms 这些国内的CMS呢?
Spark 系统性学习笔记系列
Jenkins--部署--3.1--代码提交自动触发jenkins--方式1
Axial Turbine Privacy Policy
Write a small game in C (three chess)
【C】关于柔性数组.简要的谈谈柔性数组
uvm-phase机制
Analysis of software testing technology How far is Turing test from us
postman使用方法
Seleniu截图代码以及给图片赋值名字
QT web development - Notes - 3
PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image