python spark 通过key来统计不同values个数
2024-10-20 08:03:29
>>> rdd = sc.parallelize([("a", ""), ("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)]
最新文章
- Take into Action!
- Selenium-java-testng插件安装eclipse
- DIB位图(Bitmap)的读取和保存
- iOS离屏渲染简书
- js做计算器
- GetStoredProcCommand和GetSqlStringCommand的区别
- git 放弃本地修改 强制更新
- 清空select内容
- java web 之 web.xml篇
- 高质量代码之HTML、CSS篇
- iOS开发——实用篇&;提高iOS开发效率的方法和工具
- python百科
- JS 新浪API获取IP归属地
- 团队开发冲刺2-----2day
- postgresql 导出函数的方法
- @media 针对不同的屏幕尺寸设置不同的样式
- jquery的attr()方法
- C#Enum用Tuple保存值绑定到前端的CheckBox
- js中事件绑定要注意的事项之如何在方法中自己打印自己的值
- suricata 原文记录
热门文章
- JQuery 动态创建表单,并自动提交
- 《java数据结构与算法》系列之“快速排序";
- OpenCV: OpenCV人脸检测框可信度排序
- VS2013配置编译Caffe-Win10_X64
- ANE打包
- MySQL在Linux下的表名如何不区分大小写
- 死磕itchat源码--config.py
- Python匿名函数/排序函数/过滤函数/映射函数/递归/二分法
- 谨慎调整内核参数:vm.min_free_kbytes
- HTTP 状态码之:301、302 重定向