码迷,mamicode.com
首页 > 数据库 > 详细

spark foldByKey

时间:2015-01-19 00:19:50      阅读:782      评论:0      收藏:0      [点我收藏+]

标签:spark   foldbykey   

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)
  }
}

spark foldByKey

标签:spark   foldbykey   

原文地址:http://blog.csdn.net/hi_1234567/article/details/42847057

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!