当前位置:网站首页>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()
}
}
本文为学习笔记记录!
边栏推荐
- Wang Xuegang - compiled shipment line file
- 自定义卡包效果实现
- Redisson报异常attempt to unlock lock, not locked by current thread by node id解决方案
- C语言基础_共用体
- RestTemlate源码分析及工具类设计
- 积分商城商品供应商选择的三个要求
- Jenkins--基础--6.3--Pipeline--语法--脚本式
- EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
- day_05 time 模块
- LeetCode_2358_分组的最大数量
猜你喜欢
随机推荐
MySQL Workbench 安装及使用
主流监控系统工具选型及落地场景参考
OneNote Tutorial, How to Create More Spaces in OneNote?
What is the function of the import command of the page directive in JSP?
openpyxl 单元格合并
PostgreSQL learning summary (11) - PostgreSQL commonly used high-availability cluster solutions
知识点滴 - 为什么一般不用铜锅做菜
Biotinyl Cystamine | CAS: 128915-82-2 | biotin cysteamine
Technology Cloud Report: To realize the metaverse, NVIDIA starts from building an infrastructure platform
自定义View实现波浪荡漾效果
Seleniu screenshots code and assign name to the picture
Redisson distributed lock source code analysis for high-level use of redis
Hikari连接池源码解读
PyCharm使用教程(详细版 - 图文结合)
普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
Docker内MySQL主从复制学习,以及遇到的一些问题
A young man with strong blood and energy actually became a housekeeper. How did he successfully turn around and change careers?
小康股份更名赛力斯,如何走出一条高端产品的“丝绸之路”?
pnpm:简介
PyCharm usage tutorial (detailed version - graphic and text combination)









