【SparkAPI】JavaPairRDD——countByKey、countByKeyApprox

举报
Copy工程师 发表于 2022/01/24 15:42:09 2022/01/24
【摘要】 JavaPairRDD的countByKey方法讲解 官方文档/** * 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 expec...

JavaPairRDD的countByKey方法讲解

官方文档
/**
   * 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<K,Long> 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<String, String> javaPairRDD1 = sc.parallelizePairs(Lists.newArrayList(
                new Tuple2<String, String>("cat", "11"), new Tuple2<String, String>("dog", "22"),
                new Tuple2<String, String>("cat", "33"), new Tuple2<String, String>("pig", "44"),
                new Tuple2<String, String>("duck", "55"), new Tuple2<String, String>("cat", "66")), 3);

        Map<String,Long> key = javaPairRDD1.countByKey();
        for (Map.Entry<String,Long> 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

JavaPairRDD的countByKeyApprox方法讲解

官方文档
/**
   * 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
   */
说明

CountByKey的近似版本,如果没有在规定时间内完成就返回部分结果。

@参数超时等待作业的最长时间(毫秒)
@参数置信度结果中所需的统计置信度
@返回一个可能不完整的结果,带有错误界限

函数原型
// java
public PartialResult<java.util.Map<K,BoundedDouble>> countByKeyApprox(long timeout)
public PartialResult<java.util.Map<K,BoundedDouble>> 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]]
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。