SparkAPI】JavaPairRDD——countByKey、countByKeyApprox

      網友投稿 732 2025-04-02

      /** * Count the number of elements for each key, collecting the results to a local Map. * * @note This method should only be used if the resulting map is expected to be small, as * the whole thing is loaded into the driver's memory. * To handle very large results, consider using rdd.mapValues(_ => 1L).reduceByKey(_ + _), which * returns an RDD[T, Long] instead of a map. */

      計算每個鍵的元素數,將結果放到Map中去。

      注意:

      只有當數據量很小時,才應使用此方法,因為整個數據都被載入內存中。

      如果要處理大量數據,請考慮使用rdd.mapValues(_ => 1L).reduceByKey(_ + _),

      返回的結果是 RDD[T, Long] 而不是Map。

      // java public java.util.Map countByKey() // scala def countByKey(): Map[K, Long]

      public class CountByKey { public static void main(String[] args) { System.setProperty("hadoop.home.dir", "E:\hadoop-2.7.1"); SparkConf sparkConf = new SparkConf().setMaster("local").setAppName("Spark_DEMO"); JavaSparkContext sc = new JavaSparkContext(sparkConf); JavaPairRDD javaPairRDD1 = sc.parallelizePairs(Lists.newArrayList( new Tuple2("cat", "11"), new Tuple2("dog", "22"), new Tuple2("cat", "33"), new Tuple2("pig", "44"), new Tuple2("duck", "55"), new Tuple2("cat", "66")), 3); Map key = javaPairRDD1.countByKey(); for (Map.Entry entry : key.entrySet()){ System.out.println(entry.getKey()+":"+entry.getValue()); } } }

      19/03/20 16:36:11 INFO DAGScheduler: ResultStage 1 (countByKey at CountByKey.java:23) finished in 0.093 s 19/03/20 16:36:11 INFO DAGScheduler: Job 0 finished: countByKey at CountByKey.java:23, took 1.229949 s duck:1 cat:3 dog:1 pig:1 19/03/20 16:36:11 INFO SparkContext: Invoking stop() from shutdown hook

      /** * Approximate version of countByKey that can return a partial result if it does * not finish within a timeout. * * The confidence is the probability that the error bounds of the result will * contain the true value. That is, if countApprox were called repeatedly * with confidence 0.9, we would expect 90% of the results to contain the * true count. The confidence must be in the range [0,1] or an exception will * be thrown. * * @param timeout maximum time to wait for the job, in milliseconds * @param confidence the desired statistical confidence in the result * @return a potentially incomplete result, with error bounds */

      【SparkAPI】JavaPairRDD——countByKey、countByKeyApprox

      CountByKey的近似版本,如果沒有在規定時間內完成就返回部分結果。

      @參數超時等待作業的最長時間(毫秒)

      @參數置信度結果中所需的統計置信度

      @返回一個可能不完整的結果,帶有錯誤界限

      // java public PartialResult> countByKeyApprox(long timeout) public PartialResult> countByKeyApprox(long timeout, double confidence) // scala def countByKeyApprox(timeout: Long): PartialResult[Map[K, BoundedDouble]] def countByKeyApprox(timeout: Long, confidence: Double = 0.95): PartialResult[Map[K, BoundedDouble]]

      EI企業智能 Java spark 可信智能計算服務 TICS 智能數據

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:出現value是什么意思(value是啥意思啊)
      下一篇:word空格出現小圓點怎么去除(word敲擊空格有圓點怎么去掉)
      相關文章
      久久亚洲AV成人无码国产| 亚洲精品综合久久| 亚洲情侣偷拍精品| 亚洲av第一网站久章草| 亚洲人成网站999久久久综合| 亚洲AV美女一区二区三区| 亚洲成a人片在线观看老师| 亚洲福利视频网站| 亚洲AV无码不卡无码| 亚洲乱码中文字幕手机在线 | 国产午夜亚洲精品理论片不卡| 亚洲成av人在线视| 亚洲日韩aⅴ在线视频| 亚洲午夜未满十八勿入网站2| 亚洲中文字幕无码爆乳av中文| 亚洲国产一区视频| 亚洲一级片内射网站在线观看| 亚洲AV无码成人精品区大在线| 亚洲成年看片在线观看| 亚洲人成无码网WWW| 亚洲中文久久精品无码| 亚洲精品乱码久久久久久久久久久久 | 久久丫精品国产亚洲av不卡 | 国产一区二区三区亚洲综合| 亚洲成a人一区二区三区| AV在线播放日韩亚洲欧| 亚洲欧洲∨国产一区二区三区| 亚洲国产精品久久久天堂| 亚洲AV人人澡人人爽人人夜夜| 18gay台湾男同亚洲男同| 亚洲日韩中文字幕| 亚洲中文字幕无码中文| 亚洲a无码综合a国产av中文| 亚洲国产成人久久综合一区77| 国产成人精品曰本亚洲79ren| 亚洲欧洲美洲无码精品VA | 大桥未久亚洲无av码在线| 亚洲欧洲自拍拍偷精品 美利坚| 国产gv天堂亚洲国产gv刚刚碰| 亚洲成a人片在线观看无码专区| 无码乱人伦一区二区亚洲|