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) } }
时间: 2024-10-19 06:07:01