当前位置:网站首页>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() } }
本文为学习笔记记录!
边栏推荐
- TiFlash 存储层概览
- A little bit of knowledge - why do not usually cook with copper pots
- unity pdg 设置隐藏不需要的节点以及实现自动勾选自动加载项
- Axial Turbine Privacy Policy
- 52. [Bool type input any non-0 value is not 1 version reason]
- Jenkins--基础--6.2--Pipeline--语法--声明式
- Flink 系统性学习笔记系列
- tf.where使用
- Biotin - LC - Hydrazide | CAS: 109276-34-8 | Biotin - LC - Hydrazide
- postman下载安装汉化及使用
猜你喜欢
在 QT Creator 上配置 opencv 环境的一些认识和注意点
HCIP笔记十六天
MySQL ODBC驱动简介
PyCharm使用教程(较详细,图+文)
查看变量的数据格式
如何建立私域流量?私域流量对企业有什么好处?
Figure robot software digital twin station oil and gas pipelines, oil and gas transportation control platform
小康股份更名赛力斯,如何走出一条高端产品的“丝绸之路”?
Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )
(Note) AXIS ACASIS DT-3608 Dual-bay Hard Disk Array Box RAID Setting
随机推荐
BGP solves routing black hole through MPLS
A little bit of knowledge - why do not usually cook with copper pots
Gorilla Mux 和 GORM 的使用方法
PyCharm usage tutorial (more detailed, picture + text)
What is the function of the import command of the page directive in JSP?
文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
day_05_pickel 和 json
十大免费cms建站系统介绍推荐
R语言plotly可视化:plotly可视化回归模型实际值和回归预测值的散点图分析回归模型的预测效能、一个好的模型大部分的散点在对角线附近(predicted vs actual)
In a recent build figure SLAM, and locate the progress
构建Flink第一个应用程序
测试时大量TIME_WAIT
UVM之sequence机制
PyCharm使用教程(较详细,图+文)
Biotin-EDA|CAS:111790-37-5| Ethylenediamine biotin
openpyxl 单元格合并
Redisson的看门狗机制
Three types of [OC learning notes] Block
TiFlash 存储层概览
工程师如何对待开源 --- 一个老工程师的肺腑之言