spark_core_02

package com.atguigu.bigata.spark.core.rdd.builder.operator.transform

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

/**
 * @auther :${user}
 * @date :2022/2/14 14:39
 *
 */
object spark022_RDD_Operator_transform_Test {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(conf)

    val RDD = sc.textFile("datas/agent.log")

    //TODO 统计出每一个省份每个广告被点击数量排行的 Top3

    //1) 数据准备
    //agent.log:时间戳,省份,城市,用户,广告,中间字段使用空格分隔。
    //2) 需求描述
    //统计出每一个省份每个广告被点击数量排行的 Top3


    RDD.map(
      line => {
        val datas = line.split(" ")
        (datas(1), datas(4))
      }
  ).map(
    line => {
      (line, 1)
    }
  ).reduceByKey(_ + _).
    map(
      line => {
        (line._1._1, (line._1._2, line._2))
      }
    ).groupByKey().
    mapValues(
      iter => {
        iter
          .toList
          .sortWith(_._2 > _._2)
          .take(3)
      }
    ).collect
    .foreach(println)

  sc.stop()


  }
}
posted @ 2022-02-15 23:35  ftwftw  阅读(41)  评论(0)    收藏  举报