【问题标题】:python spark streaming word count in each group每组中的python spark流字数
【发布时间】: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


    【解决方案1】:

    从 Apache Spark 源代码的this example 开始,你只需要更改两部分:

    1. 更改输入以使用 twitter。
    2. 使用 updateStateByKey 函数使计数变为有状态,但键是单词,您必须使用 (state, word) 组合键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 2014-11-26
      • 2019-05-19
      相关资源
      最近更新 更多