美文网首页
reduceByKey 根据key化简

reduceByKey 根据key化简

作者: yayooo | 来源:发表于2019-07-30 23:30 被阅读0次

源码:

  def reduceByKey(func: (V, V) => V): RDD[(K, V)] = self.withScope {
    reduceByKey(defaultPartitioner(self), func)
  }
reduceByKey
package com.atguigu

import org.apache.spark.rdd.RDD
import org.apache.spark.{HashPartitioner, Partitioner, SparkConf, SparkContext}

object Trans {
  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("Spark01_Partition")
    //构建spark上下文对象
    val sc = new SparkContext(conf)

    val rdd: RDD[(String, Int)] = sc.makeRDD(List(("a",1),("b",2),("a",3),("b",4)))
    val reduceByKeyRDD: RDD[(String, Int)] = rdd.reduceByKey((x,y) => (x+y))
    reduceByKeyRDD.collect().foreach(println)
    sc.stop()
  }
}


(a,4)
(b,6)

相关文章

网友评论

      本文标题:reduceByKey 根据key化简

      本文链接:https://www.haomeiwen.com/subject/ansnrctx.html