>>> 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)]


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-17
  • 2021-05-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
猜你喜欢
  • 2021-12-25
  • 2021-08-14
  • 2021-11-10
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-12-11
相关资源
相似解决方案