当前位置: 首页 > 工具软件 > spark-wallet > 使用案例 >

spark foldByKey

融建树
2023-12-01
package com.latrobe.spark

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

/**
 * Created by spark on 15-1-18.
 */
object FoldByKey {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("spark-demo").setMaster("local")
    val sc = new SparkContext(conf)

    import org.apache.spark.SparkContext._
    val a = sc.parallelize(List("dog", "cat", "owl", "gnu", "ant") , 2)
    val b = a.map(x => (x.length , x))

    val c = b.foldByKey("")(_ + _)

    //打印结果:(3,dogcatowlgnuant)
    c.collect().foreach(print)

    val aa = sc.parallelize(List("dog", "tiger", "lion", "cat", "panther", "eagle"), 2)
    val bb = aa.map(x => (x.length , x))
    val cc = bb.foldByKey("")( _ + _)

    //打印结果:(4,lion)(3,dogcat)(7,panther)(5,tigereagle)
    cc.collect().foreach(print)
  }
}
 类似资料:

相关阅读

相关文章

相关问答