我有一个由文本数组组成的 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()
但我一直纠结于如何检索唯一计数。
最佳答案
只是flatMap
,取distinct
和count
:
words.flatMap(set).distinct().count()
关于python - 使用数组对象计算 Spark RDD 中的不同文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35346780/