Spark案例实战之四

本文介绍如何使用Apache Spark进行大规模数据处理,通过具体案例分析微博专栏中粉丝数量最多的用户TopN。采用Spark RDD进行数据加载、清洗、转换及排序等操作,最终输出各专栏下粉丝最多的用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spark案例实战之四

一.微博专栏分析

1.需求:有一个微博网站,下面有很多栏目,每个栏目下面都有几千万用户,每个用户会有很多的粉丝,要求取出各栏目粉丝量最多的用户TopN。【可用TreeMap实现,专栏:feature, 粉丝:fan】
日志每行记录如下:
体育 user01 user04 user05 user08 user09 user10
其中体育是专栏名,user01是用户名,后面的都是user01的粉丝。
2.实现方法:

  • 将专栏名和用户名作为一个key,然后将粉丝数作为value。
  • 然后再排序从高到低输出即可
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object WeiboStatistics {
  def main(args:Array[String]):Unit={
    val conf = new SparkConf().setAppName("ShareVariables").setMaster("local")
    val sc = new SparkContext(conf)

    val text = sc.textFile("C:\\Users\\enmonster\\Desktop\\data1.txt")
    var res1: RDD[Array[String]] = text.map(line => line.split(" "))//split line with space
    var fansNum = 0

    val res2 = res1.map(x => (x(0),x(1),x.length-2) )//get x(0) x(1) x.length-2 form a map
    res2.collect.foreach(println) //print

    val res3: RDD[((String, String), Int)] = res2.map(x => ((x._1,x._2),x._3))
    val res4 = res3.sortBy(x=>x._2,false).groupBy(x=> x._1._1)
    res4.foreach(println)
  }
}

3.微博专栏的信息如下:

体育 user01 user04 user05 user08 user09 user10
体育 user04 user05 user08 user09 user10
体育 user10 user33 user38 user29 user18 user12 user14
明星 user03 user02 user05 user04 user03 user23 user14 user06
明星 user02 user11 user07
明星 user06 user25 user03 user09 user07
文化 user10 user33 user38 user29 user18 user12 user14 user03 user05 user04  user23 user14 user06 user02 user11 user07
文化 user11 user03 user09 user07 user18 user42 user54
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

说文科技

看书人不妨赏个酒钱?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值