python spark 通过key来统计不同values个数
2017-07-12 14:07
302 查看
>>> 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)]
相关文章推荐
- 第110课: Spark Streaming电商广告点击综合案例通过updateStateByKey等实现广告点击流量的在线更新统计
- 第110讲: Spark Streaming电商广告点击综合案例通过updateStateByKey等实现广告点击流量的在线更新统计
- 通过 “由对象V到对象C的转换” 来说明 Spark_Streaming api中 reduceByKey 与 combineByKey 注意事项
- 【Python】利用setdefault函数实现dict的转置(key与value对互换),统计value出现的次数
- 使用Python+jieba和java+庖丁分词在Spark集群上进行中文分词统计
- spark streaming - kafka updateStateByKey 统计用户消费金额
- spark streaming - kafka updateStateByKey 统计用户消费金额
- 关于 android 通过 python 统计 fps
- 《Spark商业案例与性能调优实战100课》第9课:商业案例之通过Spark SQL 下两种不同方式实现口碑最佳和最热门电影比较
- 通过编写Python小程序来统计测试脚本的关键字分析 20160328
- 005_004 Python 统计值 并按照次数排序 可以通过字典的值对key进行排序
- spark累加器介绍-案例通过累加器统计文章中空白行数量
- python读取excel表格统计不同网段IP个数
- 使用Python+jieba和java+庖丁分词在Spark集群上进行中文分词统计
- 通过Python3.5来学习几种不同的IO模型
- Python开发Spark应用之Wordcount词频统计
- Python 统计不同url svn代码变更数
- python3 aproiri算法(涉及字典来统计项集tupl作为key,int转化为tuple)
- Python实现字典的key和values的交换
- spark streaming - kafka updateStateByKey 统计用户消费金额