寫(xiě)Spark代碼的時(shí)候經(jīng)常發(fā)現(xiàn)rdd沒(méi)有reduceByKey的方法,這個(gè)發(fā)生在spark1.2及其以前對(duì)版本,因?yàn)閞dd本身不存在reduceByKey的方法,需要隱式轉(zhuǎn)換成PairRDDFunctions才能訪問(wèn),因此需要引入Import org.apache.spark.SparkContext._。
創(chuàng)新互聯(lián)2013年至今,先為寧陵等服務(wù)建站,寧陵等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為寧陵企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
不過(guò)到了spark1.3的版本后,隱式轉(zhuǎn)換的放在rdd的object中,這樣就會(huì)自動(dòng)被引入,不需要顯式引入。
* 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) }
至于什么是隱式轉(zhuǎn)換,簡(jiǎn)單來(lái)講就是scala偷梁換柱換柱,讓隔壁老王來(lái)干你干不了的事情了。