RDD无reduceByKey方法

本文详细介绍了在使用Spark进行编程时如何使用reduceByKey方法。针对Spark不同版本中该方法的调用方式变化进行了说明,特别是在Spark 1.3之后版本中,如何自动引入隐式转换以简化代码编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写Spark代码的时候经常发现rdd没有reduceByKey的方法,这个发生在spark1.2及其以前对版本,因为rdd本身不存在reduceByKey的方法,需要隐式转换成PairRDDFunctions才能访问,因此需要引入Import org.apache.spark.SparkContext._。

不过到了spark1.3的版本后,隐式转换的放在rdd的object中,这样就会自动被引入,不需要显式引入。

 * Defines implicit functions that provide extra functionalities on RDDs of specific types.
 * For example, [[RDD.rddToPairRDDFunctions]] converts an RDD into a [[PairRDDFunctions]] for
 * key-value-pair RDDs, and enabling extra functionalities such as [[PairRDDFunctions.reduceByKey]].
*/
 
object RDD {
  // The following implicit functions were in SparkContext before 1.3 and users had to
  // `import SparkContext._` to enable them. Now we move them here to make the compiler find
  // them automatically. However, we still keep the old functions in SparkContext for backward
  // compatibility and forward to the following functions directly.
  implicit def rddToPairRDDFunctions[K, V](rdd: RDD[(K, V)])
    (implicit kt: ClassTag[K], vt: ClassTag[V], ord: Ordering[K] = null): PairRDDFunctions[K, V] = {
    new PairRDDFunctions(rdd)
  }

 

至于什么是隐式转换,简单来讲就是scala偷梁换柱换柱,让隔壁老王来干你干不了的事情了。

转载于:https://www.cnblogs.com/luckuan/p/4479551.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值