首页 > 其他 > 详细

RDDTest.scala

时间:2015-09-09 16:21:40      阅读:246      评论:0      收藏:0      [点我收藏+]
/**
 * Created by root on 9/7/15.
 */
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf

object RDDTest {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("RDDTest").setMaster("local")
    val sc = new SparkContext(conf)
    val lines = sc.textFile("/home/slh/data/rddtest.txt")
    //count the word
    val lineLengths = lines.map(s => s.length)  //rdd
    val totalLength = lineLengths.reduce((a, b) => a + b)
    println("total length: " + totalLength)

    //get the word count
    val word_count = lines.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
    //word_count.saveAsTextFile("/home/slh/data/rddresult0")

    //get the sum
    val sum = lines.flatMap(line => line.split(" ")).map(word => (1, word)).reduceByKey((a, b) => a + b)
    //sum.saveAsTextFile("/home/slh/data/rddresult1")
    //the result is (1,3343566777879717727)
    //println("sum: " + sum)

    //accumulator
    val accum = sc.accumulator(0, "My Accumulator")
    sc.parallelize(Array(1,2,3,4)).foreach(x => accum += x)
    println("Accumulator of Array(1,2,3,4) : " + accum.value)
  }
}

 

RDDTest.scala

原文:http://www.cnblogs.com/sunflower627/p/4794669.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!