【发布时间】:2015-11-08 21:02:07
【问题描述】:
我有以下代码部分,将推文集合的 TFIDF 映射到原始单词,然后用于查找每个集群中的热门单词:
#document = sc.textFile("<text file path>").map(lambda line: line.split(" "))
#"tfidf" is an rdd of tweets contained in "document"
#map tfidf to original tweets and cluster similar tweets
clusterIds = clusters.predict(tfidf)
mapped_value = clusterIds.zip(document)
cluster_value = mapped_value.reduceByKey(lambda a,b: a+b).take(cluster_num)
#Fetch the top 5 words from each cluster
topics = []
for i in cluster_value:
word_count = sc.parallelize(i[1])
topics.append(
word_count.map(lambda x: (x,1))
.reduceByKey(lambda x,y: x+y)
.takeOrdered(5, key=lambda x: -x[1]))
有没有更好的方法来做到这一点? 我在 Spark UI 上看到,在具有 20.5 Gb 执行程序内存和 2 Gb 驱动程序内存的 4 个 VM 的集群上执行 reduceByKey() 操作时,我的代码需要大约 70 分钟。推文数量为 500K。文本文件大小为 31 Mb 后处理停用词和垃圾字符。
【问题讨论】:
标签: python apache-spark pyspark apache-spark-mllib