一、数据说明

图片1.png
上面的数据图是从数据文件中截取的一部分内容,表示为电商网站的用户行为数据,主要包含用户的4 种行为:搜索,点击,下单,支付。数据规则如下:

  • 数据文件中每行数据采用下划线分隔数据
  • 每一行数据表示用户的一次行为,这个行为只能是 4 种行为的一种
  • 如果搜索关键字为 null,表示数据不是搜索数据
  • 如果点击的品类 ID 和产品 ID 为-1,表示数据不是点击数据
  • 针对于下单行为,一次可以下单多个商品,所以品类 ID 和产品 ID 可以是多个,id 之间采用逗号分隔,如果本次不是下单行为,则数据采用 null 表示

支付行为和下单行为类似
Ø 支付行为和下单行为类似详细字段说明:

编号 字段名称 字段类型 字段含义
1 date String 用户点击行为的日期
2 user_id Long 用户的ID
3 session_id String Session 的ID
4 page_id Long 某个页面的ID
5 action_time String 动作的时间点
6 search_keyword String 用户搜索的关键词
7 click_category_id Long 某一个商品品类的ID
8 click_product_id Long 某一个商品的ID
9 order_category_ids String 一次订单中所有品类的ID 集合
10 order_product_ids String 一次订单中所有商品的ID 集合
11 pay_category_ids String 一次支付中所有品类的ID 集合
12 pay_product_ids String 一次支付中所有商品的ID 集合
13 city_id Long 城市id

二、Top10 热门品类

本项目需求为:先按照点击数排名,靠前的就排名高;如果点击数相同,再比较下单数;下单数再相同,就比较支付数。

1.1、需求1-实现方案一

分别统计每个品类点击的次数,下单的次数和支付的次数:(品类,点击总数)(品类,下单总数)(品类,支付总数)

  1. package com.atguigu.bigdata.spark.core.req
  2. import org.apache.spark.rdd.RDD
  3. import org.apache.spark.{SparkConf, SparkContext}
  4. object Spark01_Req1_HotCategoryTop10Analysis {
  5. def main(args: Array[String]): Unit = {
  6. // TODO : Top10热门品类
  7. val sparConf = new SparkConf().setMaster("local[*]").setAppName("HotCategoryTop10Analysis")
  8. val sc = new SparkContext(sparConf)
  9. // 1. 读取原始日志数据
  10. val actionRDD = sc.textFile("datas/user_visit_action.txt")
  11. // 2. 统计品类的点击数量:(品类ID,点击数量)
  12. val clickActionRDD = actionRDD.filter(
  13. action => {
  14. val datas = action.split("_")
  15. datas(6) != "-1"
  16. }
  17. )
  18. val clickCountRDD: RDD[(String, Int)] = clickActionRDD.map(
  19. action => {
  20. val datas = action.split("_")
  21. (datas(6), 1)
  22. }
  23. ).reduceByKey(_ + _)
  24. // 3. 统计品类的下单数量:(品类ID,下单数量)
  25. val orderActionRDD = actionRDD.filter(
  26. action => {
  27. val datas = action.split("_")
  28. datas(8) != "null"
  29. }
  30. )
  31. // orderid => 1,2,3
  32. // 【(1,1),(2,1),(3,1)】
  33. val orderCountRDD = orderActionRDD.flatMap(
  34. action => {
  35. val datas = action.split("_")
  36. val cid = datas(8)
  37. val cids = cid.split(",")
  38. cids.map(id=>(id, 1))
  39. }
  40. ).reduceByKey(_+_)
  41. // 4. 统计品类的支付数量:(品类ID,支付数量)
  42. val payActionRDD = actionRDD.filter(
  43. action => {
  44. val datas = action.split("_")
  45. datas(10) != "null"
  46. }
  47. )
  48. // orderid => 1,2,3
  49. // 【(1,1),(2,1),(3,1)】
  50. val payCountRDD = payActionRDD.flatMap(
  51. action => {
  52. val datas = action.split("_")
  53. val cid = datas(10)
  54. val cids = cid.split(",")
  55. cids.map(id=>(id, 1))
  56. }
  57. ).reduceByKey(_+_)
  58. // 5. 将品类进行排序,并且取前10名
  59. // 点击数量排序,下单数量排序,支付数量排序
  60. // 元组排序:先比较第一个,再比较第二个,再比较第三个,依此类推
  61. // ( 品类ID, ( 点击数量, 下单数量, 支付数量 ) )
  62. //
  63. // cogroup = connect + group
  64. val cogroupRDD: RDD[(String, (Iterable[Int], Iterable[Int], Iterable[Int]))] = clickCountRDD.cogroup(orderCountRDD, payCountRDD)
  65. val analysisRDD = cogroupRDD.mapValues{
  66. case ( clickIter, orderIter, payIter ) => {
  67. var clickCnt = 0
  68. val iter1 = clickIter.iterator
  69. if ( iter1.hasNext ) {
  70. clickCnt = iter1.next()
  71. }
  72. var orderCnt = 0
  73. val iter2 = orderIter.iterator
  74. if ( iter2.hasNext ) {
  75. orderCnt = iter2.next()
  76. }
  77. var payCnt = 0
  78. val iter3 = payIter.iterator
  79. if ( iter3.hasNext ) {
  80. payCnt = iter3.next()
  81. }
  82. ( clickCnt, orderCnt, payCnt )
  83. }
  84. }
  85. val resultRDD = analysisRDD.sortBy(_._2, false).take(10)
  86. // 6. 将结果采集到控制台打印出来
  87. resultRDD.foreach(println)
  88. sc.stop()
  89. }
  90. }

减少cogroup出现shuffle的影响

  1. package com.atguigu.bigdata.spark.core.req
  2. import org.apache.spark.rdd.RDD
  3. import org.apache.spark.{SparkConf, SparkContext}
  4. object Spark02_Req1_HotCategoryTop10Analysis1 {
  5. def main(args: Array[String]): Unit = {
  6. // TODO : Top10热门品类
  7. val sparConf = new SparkConf().setMaster("local[*]").setAppName("HotCategoryTop10Analysis")
  8. val sc = new SparkContext(sparConf)
  9. // Q : actionRDD重复使用
  10. // Q : cogroup性能可能较低
  11. // 1. 读取原始日志数据
  12. val actionRDD = sc.textFile("datas/user_visit_action.txt")
  13. actionRDD.cache()
  14. // 2. 统计品类的点击数量:(品类ID,点击数量)
  15. val clickActionRDD = actionRDD.filter(
  16. action => {
  17. val datas = action.split("_")
  18. datas(6) != "-1"
  19. }
  20. )
  21. val clickCountRDD: RDD[(String, Int)] = clickActionRDD.map(
  22. action => {
  23. val datas = action.split("_")
  24. (datas(6), 1)
  25. }
  26. ).reduceByKey(_ + _)
  27. // 3. 统计品类的下单数量:(品类ID,下单数量)
  28. val orderActionRDD = actionRDD.filter(
  29. action => {
  30. val datas = action.split("_")
  31. datas(8) != "null"
  32. }
  33. )
  34. // orderid => 1,2,3
  35. // 【(1,1),(2,1),(3,1)】
  36. val orderCountRDD = orderActionRDD.flatMap(
  37. action => {
  38. val datas = action.split("_")
  39. val cid = datas(8)
  40. val cids = cid.split(",")
  41. cids.map(id=>(id, 1))
  42. }
  43. ).reduceByKey(_+_)
  44. // 4. 统计品类的支付数量:(品类ID,支付数量)
  45. val payActionRDD = actionRDD.filter(
  46. action => {
  47. val datas = action.split("_")
  48. datas(10) != "null"
  49. }
  50. )
  51. // orderid => 1,2,3
  52. // 【(1,1),(2,1),(3,1)】
  53. val payCountRDD = payActionRDD.flatMap(
  54. action => {
  55. val datas = action.split("_")
  56. val cid = datas(10)
  57. val cids = cid.split(",")
  58. cids.map(id=>(id, 1))
  59. }
  60. ).reduceByKey(_+_)
  61. // (品类ID, 点击数量) => (品类ID, (点击数量, 0, 0))
  62. // (品类ID, 下单数量) => (品类ID, (0, 下单数量, 0))
  63. // => (品类ID, (点击数量, 下单数量, 0))
  64. // (品类ID, 支付数量) => (品类ID, (0, 0, 支付数量))
  65. // => (品类ID, (点击数量, 下单数量, 支付数量))
  66. // ( 品类ID, ( 点击数量, 下单数量, 支付数量 ) )
  67. // 5. 将品类进行排序,并且取前10名
  68. // 点击数量排序,下单数量排序,支付数量排序
  69. // 元组排序:先比较第一个,再比较第二个,再比较第三个,依此类推
  70. // ( 品类ID, ( 点击数量, 下单数量, 支付数量 ) )
  71. //
  72. val rdd1 = clickCountRDD.map{
  73. case ( cid, cnt ) => {
  74. (cid, (cnt, 0, 0))
  75. }
  76. }
  77. val rdd2 = orderCountRDD.map{
  78. case ( cid, cnt ) => {
  79. (cid, (0, cnt, 0))
  80. }
  81. }
  82. val rdd3 = payCountRDD.map{
  83. case ( cid, cnt ) => {
  84. (cid, (0, 0, cnt))
  85. }
  86. }
  87. // 将三个数据源合并在一起,统一进行聚合计算
  88. val soruceRDD: RDD[(String, (Int, Int, Int))] = rdd1.union(rdd2).union(rdd3)
  89. val analysisRDD = soruceRDD.reduceByKey(
  90. ( t1, t2 ) => {
  91. ( t1._1+t2._1, t1._2 + t2._2, t1._3 + t2._3 )
  92. }
  93. )
  94. val resultRDD = analysisRDD.sortBy(_._2, false).take(10)
  95. // 6. 将结果采集到控制台打印出来
  96. resultRDD.foreach(println)
  97. sc.stop()
  98. }
  99. }

1.2、实现方案二

一次性统计每个品类点击的次数,下单的次数和支付的次数:(品类,(点击总数,下单总数,支付总数))

  1. package com.atguigu.bigdata.spark.core.req
  2. import org.apache.spark.rdd.RDD
  3. import org.apache.spark.{SparkConf, SparkContext}
  4. object Spark03_Req1_HotCategoryTop10Analysis2 {
  5. def main(args: Array[String]): Unit = {
  6. // TODO : Top10热门品类
  7. val sparConf = new SparkConf().setMaster("local[*]").setAppName("HotCategoryTop10Analysis")
  8. val sc = new SparkContext(sparConf)
  9. // Q : 存在大量的shuffle操作(reduceByKey)
  10. // reduceByKey 聚合算子,spark会提供优化,缓存
  11. // 1. 读取原始日志数据
  12. val actionRDD = sc.textFile("datas/user_visit_action.txt")
  13. // 2. 将数据转换结构
  14. // 点击的场合 : ( 品类ID,( 1, 0, 0 ) )
  15. // 下单的场合 : ( 品类ID,( 0, 1, 0 ) )
  16. // 支付的场合 : ( 品类ID,( 0, 0, 1 ) )
  17. val flatRDD: RDD[(String, (Int, Int, Int))] = actionRDD.flatMap(
  18. action => {
  19. val datas = action.split("_")
  20. if (datas(6) != "-1") {
  21. // 点击的场合
  22. List((datas(6), (1, 0, 0)))
  23. } else if (datas(8) != "null") {
  24. // 下单的场合
  25. val ids = datas(8).split(",")
  26. ids.map(id => (id, (0, 1, 0)))
  27. } else if (datas(10) != "null") {
  28. // 支付的场合
  29. val ids = datas(10).split(",")
  30. ids.map(id => (id, (0, 0, 1)))
  31. } else {
  32. Nil
  33. }
  34. }
  35. )
  36. // 3. 将相同的品类ID的数据进行分组聚合
  37. // ( 品类ID,( 点击数量, 下单数量, 支付数量 ) )
  38. val analysisRDD = flatRDD.reduceByKey(
  39. (t1, t2) => {
  40. ( t1._1+t2._1, t1._2 + t2._2, t1._3 + t2._3 )
  41. }
  42. )
  43. // 4. 将统计结果根据数量进行降序处理,取前10名
  44. val resultRDD = analysisRDD.sortBy(_._2, false).take(10)
  45. // 5. 将结果采集到控制台打印出来
  46. resultRDD.foreach(println)
  47. sc.stop()
  48. }
  49. }

1.3、实现方案三

使用累加器的方式聚合数据

  1. package com.atguigu.bigdata.spark.core.req
  2. import org.apache.spark.rdd.RDD
  3. import org.apache.spark.util.AccumulatorV2
  4. import org.apache.spark.{SparkConf, SparkContext}
  5. import scala.collection.mutable
  6. object Spark04_Req1_HotCategoryTop10Analysis3 {
  7. def main(args: Array[String]): Unit = {
  8. // TODO : Top10热门品类
  9. val sparConf = new SparkConf().setMaster("local[*]").setAppName("HotCategoryTop10Analysis")
  10. val sc = new SparkContext(sparConf)
  11. // 1. 读取原始日志数据
  12. val actionRDD = sc.textFile("datas/user_visit_action.txt")
  13. val acc = new HotCategoryAccumulator
  14. sc.register(acc, "hotCategory")
  15. // 2. 将数据转换结构
  16. actionRDD.foreach(
  17. action => {
  18. val datas = action.split("_")
  19. if (datas(6) != "-1") {
  20. // 点击的场合
  21. acc.add((datas(6), "click"))
  22. } else if (datas(8) != "null") {
  23. // 下单的场合
  24. val ids = datas(8).split(",")
  25. ids.foreach(
  26. id => {
  27. acc.add( (id, "order") )
  28. }
  29. )
  30. } else if (datas(10) != "null") {
  31. // 支付的场合
  32. val ids = datas(10).split(",")
  33. ids.foreach(
  34. id => {
  35. acc.add( (id, "pay") )
  36. }
  37. )
  38. }
  39. }
  40. )
  41. val accVal: mutable.Map[String, HotCategory] = acc.value
  42. val categories: mutable.Iterable[HotCategory] = accVal.map(_._2)
  43. val sort = categories.toList.sortWith(
  44. (left, right) => {
  45. if ( left.clickCnt > right.clickCnt ) {
  46. true
  47. } else if (left.clickCnt == right.clickCnt) {
  48. if ( left.orderCnt > right.orderCnt ) {
  49. true
  50. } else if (left.orderCnt == right.orderCnt) {
  51. left.payCnt > right.payCnt
  52. } else {
  53. false
  54. }
  55. } else {
  56. false
  57. }
  58. }
  59. )
  60. // 5. 将结果采集到控制台打印出来
  61. sort.take(10).foreach(println)
  62. sc.stop()
  63. }
  64. case class HotCategory( cid:String, var clickCnt : Int, var orderCnt : Int, var payCnt : Int )
  65. /**
  66. * 自定义累加器
  67. * 1. 继承AccumulatorV2,定义泛型
  68. * IN : ( 品类ID, 行为类型 )
  69. * OUT : mutable.Map[String, HotCategory]
  70. * 2. 重写方法(6)
  71. */
  72. class HotCategoryAccumulator extends AccumulatorV2[(String, String), mutable.Map[String, HotCategory]]{
  73. private val hcMap = mutable.Map[String, HotCategory]()
  74. override def isZero: Boolean = {
  75. hcMap.isEmpty
  76. }
  77. override def copy(): AccumulatorV2[(String, String), mutable.Map[String, HotCategory]] = {
  78. new HotCategoryAccumulator()
  79. }
  80. override def reset(): Unit = {
  81. hcMap.clear()
  82. }
  83. override def add(v: (String, String)): Unit = {
  84. val cid = v._1
  85. val actionType = v._2
  86. val category: HotCategory = hcMap.getOrElse(cid, HotCategory(cid, 0,0,0))
  87. if ( actionType == "click" ) {
  88. category.clickCnt += 1
  89. } else if (actionType == "order") {
  90. category.orderCnt += 1
  91. } else if (actionType == "pay") {
  92. category.payCnt += 1
  93. }
  94. hcMap.update(cid, category)
  95. }
  96. override def merge(other: AccumulatorV2[(String, String), mutable.Map[String, HotCategory]]): Unit = {
  97. val map1 = this.hcMap
  98. val map2 = other.value
  99. map2.foreach{
  100. case ( cid, hc ) => {
  101. val category: HotCategory = map1.getOrElse(cid, HotCategory(cid, 0,0,0))
  102. category.clickCnt += hc.clickCnt
  103. category.orderCnt += hc.orderCnt
  104. category.payCnt += hc.payCnt
  105. map1.update(cid, category)
  106. }
  107. }
  108. }
  109. override def value: mutable.Map[String, HotCategory] = hcMap
  110. }
  111. }

三、需求2-Top10 热门品类中每个品类的Top10 活跃Session 统计

在需求一的基础上,增加每个品类用户session 的点击统计

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Spark05_Req2_HotCategoryTop10SessionAnalysis {

    def main(args: Array[String]): Unit = {

        // TODO : Top10热门品类
        val sparConf = new SparkConf().setMaster("local[*]").setAppName("HotCategoryTop10Analysis")
        val sc = new SparkContext(sparConf)

        val actionRDD = sc.textFile("datas/user_visit_action.txt")
        actionRDD.cache()
        val top10Ids: Array[String] = top10Category(actionRDD)

        // 1. 过滤原始数据,保留点击和前10品类ID
        val filterActionRDD = actionRDD.filter(
            action => {
                val datas = action.split("_")
                if ( datas(6) != "-1" ) {
                    top10Ids.contains(datas(6))
                } else {
                    false
                }
            }
        )

        // 2. 根据品类ID和sessionid进行点击量的统计
        val reduceRDD: RDD[((String, String), Int)] = filterActionRDD.map(
            action => {
                val datas = action.split("_")
                ((datas(6), datas(2)), 1)
            }
        ).reduceByKey(_ + _)

        // 3. 将统计的结果进行结构的转换
        //  (( 品类ID,sessionId ),sum) => ( 品类ID,(sessionId, sum) )
        val mapRDD = reduceRDD.map{
            case ( (cid, sid), sum ) => {
                ( cid, (sid, sum) )
            }
        }

        // 4. 相同的品类进行分组
        val groupRDD: RDD[(String, Iterable[(String, Int)])] = mapRDD.groupByKey()

        // 5. 将分组后的数据进行点击量的排序,取前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)
    }
}

四、需求3-页面单跳转换率统计

4.1、页面单跳转化率

计算页面单跳转化率,什么是页面单跳转换率,比如一个用户在一次Session 过程中访问的页面路径3,5,7,9,10,21,那么页面3 跳到页面5 叫一次单跳,7-9 也叫一次单跳, 那么单跳转化率就是要统计页面点击的概率。
比如:计算3-5 的单跳转化率,先获取符合条件的Session 对于页面3 的访问次数(PV)为 A,然后获取符合条件的 Session 中访问了页面 3 又紧接着访问了页面 5 的次数为 B,那么B/A 就是3-5 的页面单跳转化率。

4.2、统计页面单跳转化率意义

产品经理和运营总监,可以根据这个指标,去尝试分析,整个网站,产品,各个页面的表现怎么样,是不是需要去优化产品的布局;吸引用户最终可以进入最后的支付页面。
数据分析师,可以此数据做更深一步的计算和分析。
企业管理层,可以看到整个公司的网站,各个页面的之间的跳转的表现如何,可以适当调整公司的经营战略或策略。
图片1.png

4.3、代码实现

package com.atguigu.bigdata.spark.core.req

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Spark06_Req3_PageflowAnalysis {

    def main(args: Array[String]): Unit = {

        // TODO : Top10热门品类
        val sparConf = new SparkConf().setMaster("local[*]").setAppName("HotCategoryTop10Analysis")
        val sc = new SparkContext(sparConf)

        val actionRDD = sc.textFile("datas/user_visit_action.txt")

        val actionDataRDD = actionRDD.map(
            action => {
                val datas = action.split("_")
                UserVisitAction(
                    datas(0),
                    datas(1).toLong,
                    datas(2),
                    datas(3).toLong,
                    datas(4),
                    datas(5),
                    datas(6).toLong,
                    datas(7).toLong,
                    datas(8),
                    datas(9),
                    datas(10),
                    datas(11),
                    datas(12).toLong
                )
            }
        )
        actionDataRDD.cache()

        // TODO 对指定的页面连续跳转进行统计
        // 1-2,2-3,3-4,4-5,5-6,6-7
        val ids = List[Long](1,2,3,4,5,6,7)
        val okflowIds: List[(Long, Long)] = ids.zip(ids.tail)

        // TODO 计算分母
        val pageidToCountMap: Map[Long, Long] = actionDataRDD.filter(
            action => {
                ids.init.contains(action.page_id)
            }
        ).map(
            action => {
                (action.page_id, 1L)
            }
        ).reduceByKey(_ + _).collect().toMap

        // TODO 计算分子

        // 根据session进行分组
        val sessionRDD: RDD[(String, Iterable[UserVisitAction])] = actionDataRDD.groupBy(_.session_id)

        // 分组后,根据访问时间进行排序(升序)
        val mvRDD: RDD[(String, List[((Long, Long), Int)])] = sessionRDD.mapValues(
            iter => {
                val sortList: List[UserVisitAction] = iter.toList.sortBy(_.action_time)

                // 【1,2,3,4】
                // 【1,2】,【2,3】,【3,4】
                // 【1-2,2-3,3-4】
                // Sliding : 滑窗
                // 【1,2,3,4】
                // 【2,3,4】
                // zip : 拉链
                val flowIds: List[Long] = sortList.map(_.page_id)
                val pageflowIds: List[(Long, Long)] = flowIds.zip(flowIds.tail)

                // 将不合法的页面跳转进行过滤
                pageflowIds.filter(
                    t => {
                        okflowIds.contains(t)
                    }
                ).map(
                    t => {
                        (t, 1)
                    }
                )
            }
        )
        // ((1,2),1)
        val flatRDD: RDD[((Long, Long), Int)] = mvRDD.map(_._2).flatMap(list=>list)
        // ((1,2),1) => ((1,2),sum)
        val dataRDD = flatRDD.reduceByKey(_+_)

        // TODO 计算单跳转换率
        // 分子除以分母
        dataRDD.foreach{
            case ( (pageid1, pageid2), sum ) => {
                val lon: Long = pageidToCountMap.getOrElse(pageid1, 0L)

                println(s"页面${pageid1}跳转到页面${pageid2}单跳转换率为:" + ( sum.toDouble/lon ))
            }
        }
        sc.stop()
    }

    //用户访问动作表
    case class UserVisitAction(
              date: String,//用户点击行为的日期
              user_id: Long,//用户的ID
              session_id: String,//Session的ID
              page_id: Long,//某个页面的ID
              action_time: String,//动作的时间点
              search_keyword: String,//用户搜索的关键词
              click_category_id: Long,//某一个商品品类的ID
              click_product_id: Long,//某一个商品的ID
              order_category_ids: String,//一次订单中所有品类的ID集合
              order_product_ids: String,//一次订单中所有商品的ID集合
              pay_category_ids: String,//一次支付中所有品类的ID集合
              pay_product_ids: String,//一次支付中所有商品的ID集合
              city_id: Long
      )//城市 id
}