Scala-19:复杂的WordCount案例

Scala-19:复杂的WordCount案例

一、案例分析

简单的案例中传进来的数据全是字符串,如下所示:

List(
      "hello",
      "hello world",
      "hello scala",
      "hello spark from scala",
      "hello flink from scala"
    )

如果传进来的数据并不全是字符串,而是经过一次统计的数据,比如

List(
      ("hello",1),
      ("hello world",2),
      ("hello scala",3),
      ("hello spark from scala",1),
      ("hello flink from scala",2)
    )

那么这个处理过程就会稍微复杂一点,就要考虑每个单词后面次数的问题,通常的处理方式有两种

1:第一种

直接展开为普通版本

("hello world",2) => ("hello world hello world")

变成了普通的版本之后,就和之前简单的WordCount案例的处理流程一样了

2:第二种

基于预统计的结果进行转换

("hello world",2) => ("hello", 2), ("world", 2)

按照二元组的单词分组

("hello" => ("hello", 1), ("hello", 2), ("hello", 3), ("hello", 1), ("hello", 2))

叠加每个单词预统计的数值

("hello", 9)

二、所用方法

所使用的方法和简单案例中的相似,只是不同的处理方式中,所需要的map阶段输出类型不同

1:将列表打散为每个单词+个数 flatMap

List[(String, Int)] => List[(String, Int)]

2:叠加每个单词预统计的数值

Map[String, Lsit[(String, Int)]] => Map[(String, Int)]

三、代码实现

1:直接展开为普通版本

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

    val stringList: List[(String, Int)] = List(
      ("hello",1),
      ("hello world",2),
      ("hello scala",3),
      ("hello spark from scala",1),
      ("hello flink from scala",2)
    )

    //思路1:直接展开为普通版本
    val newStringList: List[String] = stringList.map(
      kv => {
        (kv._1.trim + " ") * kv._2
      }
    )

    //打散
    val groupMap: Map[String, List[String]] = newStringList.flatMap(_.split(" ")).groupBy(word => word)
    val countMap: Map[String, Int] = groupMap.map(kv => (kv._1, kv._2.length))
    //排序
    val sortMap: List[(String, Int)] = countMap.toList.sortWith(_._2 > _._2)
    println(sortMap)
    //取前三
    val preList: List[(String, Int)] = sortMap.take(3)
    println(preList)
  }

}

2:基于预统计的结果进行转换

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

    val stringList: List[(String, Int)] = List(
      ("hello",1),
      ("hello world",2),
      ("hello scala",3),
      ("hello spark from scala",1),
      ("hello flink from scala",2)
    )

    //1.将列表打散为每个单词+个数
    val proCountList: List[(String, Int)] = stringList.flatMap(
      tuple => {
        val strings: Array[String] = tuple._1.split(" ")
        strings.map(word => (word,tuple._2))
      }
    )

    //2.按照二元组单词进行分组
    val preCountMap: Map[String, List[(String, Int)]] = proCountList.groupBy(_._1)
    println(preCountMap)

    //3.叠加每个单词预统计的个数值
    val countMap: Map[String, Int] = preCountMap.map(
      tupleList => (tupleList._1, tupleList._2.map(_._2).sum)

    )

    //4.转换为list,排序,取前三
    val sortList: List[(String, Int)] = countMap.toList.sortWith(_._2 > _._2).take(3)
    println(sortList)

  }

}

四、两种方法的区别

第一种方法就是将单词乘以预统计的结果,得到的就是全字符串的数据,最后的处理方式就是和简单案例的一样,其严格来说最后的统计不能算是完全的聚合

而第二种方法则是借助预统计的结果,在最后根据每个单词的预统计结果进行聚合,是真正意义上的聚合操作,这样的做法是推荐的,形同于MapRedue的阶段。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

牧码文

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值