码迷,mamicode.com
首页 > 编程语言 > 详细

python spark 通过key来统计不同values个数

时间:2017-07-12 15:07:40      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:operator   import   tor   esc   val   ict   element   des   ast   

>>> rdd = sc.parallelize([("a", "1"), ("b", 1), ("a", 1), ("a", 1)])
>>> rdd.distinct().countByKey().items()
[(a, 2), (b, 1)]

OR:

from operator import add


rdd.distinct().map(lambda x: (x[0], 1)).reduceByKey(add)
rdd.distinct().keys().map(lambda x: (x, 1)).reduceByKey(add)

distinct(numPartitions=None)

Return a new RDD containing the distinct elements in this RDD.

>>> sorted(sc.parallelize([1, 1, 2, 3]).distinct().collect())
[1, 2, 3]

 countByKey()

Count the number of elements for each key, and return the result to the master as a dictionary.

>>> rdd = sc.parallelize([("a", 1), ("b", 1), ("a", 1)])
>>> sorted(rdd.countByKey().items())
[(‘a‘, 2), (‘b‘, 1)]


python spark 通过key来统计不同values个数

标签:operator   import   tor   esc   val   ict   element   des   ast   

原文地址:http://www.cnblogs.com/bonelee/p/7155153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!