1.需求 Top10热门品类中每个品类的Top10活跃Session统计(用户)
2.只看点击
3.品类要在top10
package com.rdd.topn
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
object HotCategoryTop10Analysis5 {
def main(args: Array[String]): Unit = {
//TODO Top10热门品类
val conf: SparkConf = new SparkConf().setMaster(“local[*]”).setAppName(“HotCategoryTop10Analysis”)
val sc = new SparkContext(conf)
val actionRDD: RDD[String] = sc.textFile(“in/user_visit_action.txt”)
val top10Ids: Array[String] = top10Category(actionRDD)
//过滤原始数据 保留点击和前10品类
val filderRDD: RDD[String] = actionRDD.filter(
action => {
val datas = action.split("")
if (datas(6) != “-1”) {
top10Ids.contains(datas(6))
} else {
false
}
}
)
//根据品类DS和seddionid进行点击量的铜价
val reduceRDD: RDD[((String, String), Int)] = filderRDD.map(
action => {
val datas = action.split("")
((datas(6), datas(2)), 1)
}
).reduceByKey((_ + ))
//将统计结果进行结构转换
//(品类ID,sessionId),sum)=>(品类ID,(sessionId,sum))
val mapRDD: RDD[(String, (String, Int))] = reduceRDD.map {
case ((cid, sid), sum) => {
(cid, (sid, sum))
}
}
//相同品类进行分组
val groupRDD: RDD[(String, Iterable[(String, Int)])] = mapRDD.groupByKey()
//将分组后的数据进行点击量的排序 取前10名
val result: RDD[(String, List[(String, Int)])] = groupRDD.mapValues(
iter => {
iter.toList.sortBy(.2)(Ordering.Int.reverse).take(10)
}
)
val tuples: Array[(String, List[(String, Int)])] = result.collect()
tuples.foreach(println)
//5先按照点击 再按照下单 再支付
//元组排序 多个无关数据组成元组 先比较第一个 再第二个 一次类推
sc.stop()
}
def top10Category(actionRDD: RDD[String])={
val clickActionRDD: RDD[String] = actionRDD.filter {
data => {
val arr = data.split("")
arr(6) != “-1” //如果第六个字段不是-1 就不是点击事件 直接过滤掉无用数据
}
}
val clickCountRDD: RDD[(String, Int)] = clickActionRDD.map(
action => {
var datas = action.split("")
(datas(6), 1)
}
).reduceByKey( + )
//2.统计品类的下单、数量(品类ID,下单数量) 如果第八个字段肯定不能为Null
val orderActionRDD: RDD[String] = actionRDD.filter {
data => {
val arr = data.split("")
arr(8) != “null” //如果第六个字段不是-1 就不是点击事件 直接过滤掉无用数据
}
}
val orderCountRDD: RDD[(String, Int)] = orderActionRDD.flatMap(
action => {
val datas = action.split("")
val cid = datas(8) //扁平化操作
val cids = cid.split(",")
cids.map(id => (id, 1))
}
).reduceByKey( + )
//3.统计品类的支付数量(品类ID,支付数量)
val payActionRDD: RDD[String] = actionRDD.filter {
data => {
val arr = data.split("")
arr(10) != “null” //如果第六个字段不是-1 就不是点击事件 直接过滤掉无用数据
}
}
val payCountRDD: RDD[(String, Int)] = payActionRDD.flatMap(
action => {
val datas = action.split("")
val cid = datas(8) //扁平化操作
val cids = cid.split(",")
cids.map(id => (id, 1))
}
).reduceByKey( + _)
val rdd1: RDD[(String, (Int, Int, Int))] = clickCountRDD.map {
case (cid, cnt) => {
(cid, (cnt, 0, 0))
}
}
val rdd2: RDD[(String, (Int, Int, Int))] = orderCountRDD.map {
case (cid, cnt) => {
(cid, ( 0, cnt,0))
}
}
val rdd3: RDD[(String, (Int, Int, Int))] = payCountRDD.map {
case (cid, cnt) => {
(cid, (0, 0, cnt))
}
}
//将3个数据源连接在一起统一聚合计算
val sourceRDD: RDD[(String, (Int, Int, Int))] = rdd1.union(rdd2).union(rdd3)
val analysisRDD: RDD[(String, (Int, Int, Int))] = sourceRDD.reduceByKey {
(t1, t2) => {
(t1._1 + t2._1,
t2._2 + t2._2,
t1._3 + t2._3)
}
}
analysisRDD.sortBy(_._2,false).take(10)map(_._1)
}
}