【Spark】WordCount的多种实现方式

【Spark】WordCount的多种实现方式,第1张

【Spark】WordCount的多种实现方式

WordCount的多种实现方式
  • 准备工作
  • 使用groupBy
  • 使用groupByKey
  • 使用reduceByKey
  • 使用aggregateByKey
  • 使用foldByKey
  • 使用combineByKey
  • 使用countByKey
  • 使用countByValue

准备工作

查看数据

创建SparkContext

  val spark = new SparkConf().setMaster("local[6]").setAppName("wordCount")
  val sc = new SparkContext(spark)
  val rdd = sc.textFile("data/wordcount.txt")
使用groupBy
  
  @Test
  def test1():Unit = {
    rdd.flatMap(_.split(" "))
      .groupBy(word => word)
      .mapValues(_.size)
      .collect()
      .foreach(println(_))
  }
使用groupByKey

效率不高

  
  @Test
  def groupByKey():Unit = {
    rdd.flatMap(_.split(" "))
      .map((_,1))
      .groupByKey()
      .mapValues(_.size)
      .collect()
      .foreach(println(_))
  }
使用reduceByKey
  
  @Test
  def reduceByKey():Unit = {
    rdd.flatMap(_.split(" "))
      .map((_,1))
      .reduceByKey(_+_)
      .collect()
      .foreach(println(_))
  }
使用aggregateByKey
  
  @Test
  def aggregateByKey():Unit = {
    rdd.flatMap(_.split(" "))
      .map((_,1))
      .aggregateByKey(0)(_+_,_+_)
      .collect()
      .foreach(println(_))
  }
使用foldByKey
  
  @Test
  def foldByKey():Unit = {
    rdd.flatMap(_.split(" "))
      .map((_,1))
      .foldByKey(0)(_+_)
      .collect()
      .foreach(println(_))
  }
使用combineByKey
  
  @Test
  def combineByKey(): Unit = {
    rdd.flatMap(_.split(" "))
      .map((_, 1))
      .combineByKey(
        v => v,
        (x: Int, y) => x + y,
        (x: Int, y: Int) => x + y
      )
      .collect()
      .foreach(println(_))
  }
使用countByKey
  
  @Test
  def countByKey(): Unit = {
    rdd.flatMap(_.split(" "))
      .map((_, 1))
      .countByKey()
  }
使用countByValue
  
  @Test
  def countByvalue(): Unit = {
    rdd.flatMap(_.split(" "))
      .countByValue()
  }

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5692224.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存