如何在streamparse-pout中使用tweepy接受tweepy流并将tweets传递给bolt?

2024-06-16 14:38:32 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我开始研究storm,对python更加熟悉,我决定使用streamparse来处理storm。我计划在spoute中接受twitter流,并在bolt中执行一些计算。但我不知道我该如何在spoute中编码。我看过各种streamparse教程,但它们都显示了静态列表中的喷口发射元组,并且没有类似twitter流式api提供的流。 这是我的暴风密码:

class WordSpout(Spout):

def initialize(self, stormconf, context):
    self.words = itertools.cycle(['dog', 'cat','zebra', 'elephant'])
def next_tuple(self):
    word = next(self.words)
    self.emit([word])

这是我的tweepy代码:

^{pr2}$

我应该如何集成这两个代码?在


Tags: 代码self编码def静态教程twitter计划
1条回答
网友
1楼 · 发布于 2024-06-16 14:38:32

为此,我设置了一个kafka队列,tweepy侦听器通过这个队列编写状态.text使用pykafka进入队列。然后喷口不断地从队列中读取数据来执行分析。我的代码看起来有点像这样:

在listener.py公司名称:

class MyStreamListener(tweepy.StreamListener):

  def on_status(self, status):
    # print(status.text)
    client = KafkaClient(hosts='127.0.0.1:9092')

    topic = client.topics[str('tweets')]
    with topic.get_producer(delivery_reports=False) as producer:

        # print status.text
        sentence = status.text
        for word in sentence.split(" "):
            if word is None:
                continue
            try:
                word = str(word)
                producer.produce(word)
            except:
                continue

  def on_error(self, status_code):
    if status_code == 420:  # exceed rate limit
        return False
    else:
        print("Failing with status code " + str(status_code))
        return False

auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)

myStream.filter(track=['is'])

喷口文件:

^{pr2}$

相关问题 更多 >