【问题标题】:Counting distinct texts in a Spark RDD with array objects使用数组对象计算 Spark RDD 中的不同文本
【发布时间】:2016-02-11 18:03:24
【问题描述】:

我有一个由文本数组组成的 spark rdd (words)。例如,

words.take(3)

会返回类似的东西。

[ ["A", "B"], ["B", "C"], ["C", "A", "D"] ]

现在,我想找出文本的总数以及唯一的文本数。如果RDD只有3条以上的记录,

total_words = 7
unique_words = 4 (only A, B,C,D)

现在为了得到总数,我做了类似的事情

text_count_rdd = words.map(lambda x: len(x))
text_count_rdd.sum()

但我不知道如何检索唯一计数。

【问题讨论】:

    标签: python apache-spark pyspark rdd


    【解决方案1】:

    只需flatMap,取distinctcount

    words.flatMap(set).distinct().count()
    

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 2017-08-08
      • 2018-08-17
      相关资源
      最近更新 更多