当前位置:网站首页>spark:商品热门品类TOP10统计(案例)
spark:商品热门品类TOP10统计(案例)
2022-08-02 08:28:00 【一个人的牛牛】
目录
介绍
品类是指产品的分类,大型电商网站品类分多级,一般为三级分类,此次项目中品类只有一级。
不同的公司对热门的定义不一样。此次按照每个品类的 点击---->下单---->支付 的量来统计热门品类。先按照点击数排名,数量决定排名;点击数相同,比较下单数;下单数相同,比较支付数。
数据准备
点击链接下载数据(免费下载)
14万条用户行为数据,搜索、点击、下单、支付-spark文档类资源-CSDN下载
数据说明:

时间_用户ID_sessionID_页面ID_动作时间_搜索_点击(品类ID、产品ID)_下单(品类ID、产品ID)_支付(品类ID、产品ID)_城市ID
代码实现
分别统计每个品类点击的次数,下单的次数和支付的次数:
(品类,点击总数)(品类,下单总数)(品类,支付总数)
import org.apache.spark.{SparkConf, SparkContext}
object TopOne {
def main(args: Array[String]): Unit = {
//TODO 创建环境
val sparkConf = new SparkConf().setMaster("local[*]").setAppName("TOP")
val sc = new SparkContext(sparkConf)
//TODO TOP热门商品
//读取日志文件
val rdd = sc.textFile("datas/action.txt")
rdd.cache()
//统计品类点击数量
//数据清洗
val clickRDD = rdd.filter(
action => {
val datas = action.split("_")
datas(6) != "-1"
}
)
//提取点击品类和数量并统计数量
val clickCountRDD = clickRDD.map(
action => {
val datas = action.split("_")
//(品类,数量)
(datas(6),1)
}
).reduceByKey(_+_)
// println(">>>>>>>>>")
// clickCountRDD.collect().foreach(println)
//统计品类下单数量
//数据清洗
val orderRDD = rdd.filter(
action => {
val datas = action.split("_")
datas(8) != "null"
}
)
//提取下单品类和数量并统计数量
val ordercountRDD = orderRDD.flatMap(
action => {
val datas = action.split("_")
val cid = datas(8)
//(品类,数量)
val cids = cid.split(",")
cids.map(id => (id, 1))
}
).reduceByKey(_ + _)
// println(">>>>>>>>")
// ordercountRDD.collect().foreach(println)
//统计品类支付数量
//清洗数据
val payRDD = rdd.filter(
action => {
val datas = action.split("_")
datas(10) != "null"
}
)
//提取支付品类和数量并统计数量
val paycountRDD = payRDD.flatMap(
action => {
val datas = action.split("_")
val cid = datas(10)
//(品类,数量)
val cids = cid.split(",")
cids.map(id => (id, 1))
}
).reduceByKey(_ + _)
// println(">>>>>>>>>>>>")
// paycountRDD.collect().foreach(println)
//排序————排序顺序:先点击-->再下单-->后支付
val cogroupRDD = clickCountRDD.cogroup(ordercountRDD, paycountRDD)
val cogroupRDD2 = cogroupRDD.mapValues {
case (clickIter, orderIter, payIter) => {
var clickCnt = 0
val iter1 = clickIter.iterator
if (iter1.hasNext) {
clickCnt = iter1.next()
}
var orderCnt = 0
val iter2 = orderIter.iterator
if (iter2.hasNext) {
orderCnt = iter2.next()
}
var payCnt = 0
val iter3 = payIter.iterator
if (iter3.hasNext) {
payCnt = iter3.next()
}
(clickCnt, orderCnt, payCnt)
}
}
val resultRDD = cogroupRDD2.sortBy(_._2, false).take(10)
//打印
resultRDD.foreach(println)
//TODO 关闭环境
sc.stop()
}
}
本文为学习笔记记录!
边栏推荐
- R language plotly visualization: use the plotly visualization model to predict the true positive rate (True positive) TPR and false positive rate (False positive) FPR curve under different thresholds
- 为什么都推荐使用wordpress, 而不是 phpcms 这些国内的CMS呢?
- Redis分布式锁入门
- The custom table form
- 科技云报道:实现元宇宙,英伟达从打造基础建设平台开始
- AI目标分割能力,无需绿幕即可实现快速视频抠图
- tf.where使用
- day——05 迭代器,生成器
- JSP中page指令的import命令具有什么功能呢?
- PyCharm usage tutorial (more detailed, picture + text)
猜你喜欢

redis-desktop-manager下载安装

第3周学习:ResNet+ResNeXt

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

Jenkins--部署--3.1--代码提交自动触发jenkins--方式1

C语言基础_共用体
![[OC学习笔记]ARC与引用计数](/img/56/033cfc15954567d63d987d91ca8d63.png)
[OC学习笔记]ARC与引用计数

etcd implements large-scale service governance application combat

RestTemlate源码分析及工具类设计

LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路

UVM信息服务机制
随机推荐
Scala类型转换
HCIP笔记十六天
文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
What attributes and methods are available for page directives in JSP pages?
Codeforces Round #811 (Div. 3)无DF
Three types of [OC learning notes] Block
What is the function of the import command of the page directive in JSP?
UVM信息服务机制
大厂外包,值得拥有吗?
[OC学习笔记]ARC与引用计数
Jenkins--部署--3.1--代码提交自动触发jenkins--方式1
unity pdg 设置隐藏不需要的节点以及实现自动勾选自动加载项
普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
R language plotly visualization: use the plotly visualization model to predict the true positive rate (True positive) TPR and false positive rate (False positive) FPR curve under different thresholds
OneNote 教程,如何在 OneNote 中创建更多空间?
RetinaFace: Single-stage Dense Face Localisation in the Wild
为什么都推荐使用wordpress, 而不是 phpcms 这些国内的CMS呢?
openpyxl 单元格合并
R language plotly visualization: plotly visualizes the scatter plot of the actual value of the regression model and the predicted value of the regression, analyzes the prediction performance of the re
day_05模块