【发布时间】:2017-11-27 02:28:49
【问题描述】:
我在 Kafka 中有一些 twitter 数据,现在我尝试使用 pyspark 流来分析每个状态下的 top-k 词频,数据如下所示:
{"state": "AK", "tweet": "hello world"}
{"state": "MN", "tweet": "hello cruel world"}
{"state": "AK", "tweet": "hello cool world"}
我要生成的输出是:
"AK", "hello", 2
"AK", "world", 2
"AK", "cool", 1
"MN", "hello", 1
"MN", "world", 1
"MN", "cruel", 1
我的代码如下所示:
def get_word_count(line):
tokens = get_tokens(line['tweet'])
state = line['state']
return [state, tokens]
dstream_tweets.flatMap(lambda line: get_word_count(line)) \
.map(lambda line:((line[0], line[1]), 1)) \
.reduceByKey(lambda x,y : x+y)
dstream_tweets 的类是pyspark.streaming.dstream.TransformedDStream
这段代码无法计算流数据中每个状态的top-k twitter词频,有什么方法可以做到吗?
【问题讨论】:
标签: apache-spark mapreduce pyspark spark-streaming