电商最近一小时广告点击量设计方法-尚硅谷大数据培训

最近一小时广告点击量

统计各广告最近 1 小时内的点击量趋势:各广告最近 1 小时内各分钟的点击量

Redis存储数据结构如图所示:

1 思路分析

使用窗口函数进行最近一小时的数据统计,并将结果写入Redis。

2 代码实现

1)LastHourAdsHandler

object LastHourAdsHandler {

    def statLastHourAds(filteredDStream: DStream[AdsInfo]) = {

        val dateFormatter = new SimpleDateFormat(“HH:mm”)

        // 1. 利用窗口来对DStream进行开窗

        val DStreamWithWindow: DStream[AdsInfo] = filteredDStream.window(Minutes(2), Seconds(4))

        val hourMinutesCount = DStreamWithWindow.map(adsInfo => {

            ((adsInfo.adsId, dateFormatter.format(new Date(adsInfo.ts))), 1)

        }).reduceByKey(_ + _).map {

            case ((adsId, hourMinutes), count) => (adsId, (hourMinutes, count))

        }

        // 2. 转成json格式的字符串

        val adsIdHourMintesJson: DStream[(String, String)] = hourMinutesCount.groupByKey.map {

            case (adsId, hourMinutsCountIt) => {

                import org.json4s.JsonDSL._

                (adsId, JsonMethods.compact(JsonMethods.render(hourMinutsCountIt)))

            }

        }

        // 3. 写入redis

        adsIdHourMintesJson.foreachRDD(rdd => {

            val client: Jedis = RedisUtil.getJedisClient

            val result: Array[(String, String)] = rdd.collect

            result.foreach(println)

            import scala.collection.JavaConversions._

            client.hmset(“last:hour:ads:click”, result.toMap)

            client.close()

        })

    }

}

2)RealtimeApp

object RealtimeApp {

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

        // 从kafka中读出我们需要数据

        // 1. 创建 SparkConf 对象

        val conf: SparkConf = new SparkConf()

            .setAppName(“RealTimeApp”)

            .setMaster(“local[*]”)

        // 2. 创建 SparkContext 对象

        val sc = new SparkContext(conf)

        // 3. 创建 StreamingContext

        val ssc = new StreamingContext(sc, Seconds(2))

        // 4. 得到 DStream

        val recordDStream: InputDStream[ConsumerRecord[String, String]] = MyKafkaUtil.getDStream(ssc, “ads_log”)

        // 5. 为了方便后面的计算, 把消费到的字符串封装到对象中

        val adsInfoDStream: DStream[AdsInfo] = recordDStream.map {

            record =>

                val split: Array[String] = record.value.split(“,”)

                AdsInfo(split(0).toLong, split(1), split(2), split(3), split(4))

        }

        // 6: 需求5:

        val filteredDStream: DStream[AdsInfo] = BlackListApp.filterBlackList(adsInfoDStream, sc)

        BlackListApp.checkUserToBlackList(filteredDStream)

        // 7. 需求6:

        val dayAreaAdsCityCount: DStream[(String, Int)] = DayAreaCityAdsApp.statAreaCityAdsPerDay(filteredDStream, sc)

        // 8. 需求 7

        AreaAdsTop3.statAreaAdsTop3(dayAreaAdsCityCount)

        // 9. 需求8

        LastHourAdsApp.statLastHourAds(filteredDStream)

        ssc.start()

        ssc.awaitTermination()

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值