【问题标题】:How to use MessageQueue in Crawler?如何在爬虫中使用 MessageQueue?
【发布时间】:2015-07-12 03:43:34
【问题描述】:

看起来MessageQueue应该是构建Web Crawler的一个很好的架构方案,但是我还是不明白怎么做。

让我们考虑第一种情况共享数据库,它很漂亮 明确如何做到这一点,该算法将是经典的Graph Traversal

There are multiple Workers and shared database.

- I manually put the first url into the database

while true

  - worker get random discovered url from database.
  - worker parses it and gets list of all links on the page.
  - worker updates the url in the database as processed.
  - worker lookup into the database and separates the found links 
    into processed, discovered and the new ones.
  - worker add the new ones links to database as discovered.

让我们考虑第二种情况,使用 MessageQueue

There are MessageQueue containing urls that should be processed 
and multiple Workers.

- I manually put the first url in the Queue.

while true

  - worker takes next discovered url from the Queue.
  - worker parsers it and gets list of all links on the page.
  - what it does next? How it separates found links into
    processed, discovered and the new ones?
  - worker puts the list of new urls into the Queue as discovered.

问题:

  • 可以将所有发现的 url 保留在 MessageQueue 中吗?如果有数千个网站和数千个页面,那么队列中将有数百万条消息在等待。
  • 如何将页面上找到的链接分为已处理、已发现和新链接?很清楚如何在 DB 的情况下执行此操作 - 只需在 DB 中查找并检查每个链接,但在 MessageQueue 的情况下如何执行?

【问题讨论】:

    标签: architecture web-crawler message-queue


    【解决方案1】:

    接下来会做什么?它如何将找到的链接分成 处理、发现和新的?

    您将为这些设置单独的队列,这些队列将流回您的数据库。这个想法是你可以有多个工作人员,以及一个反馈循环,将新发现的 URL 发送回队列进行处理,并发送到数据库进行存储。

    如何将页面上找到的链接分为已处理、已发现和新链接?很清楚如何在 DB 的情况下执行此操作 - 只需在 DB 中查找并检查每个链接,但在 MessageQueue 的情况下如何执行?

    您可能仍会在数据库中查找来自队列的链接。

    因此,工作流程如下所示: 链接被丢弃在队列中 队列工作人员将其捡起,并检查 db 以查看是否已处理链接 如果未处理,请致电网站以检索其他出站链接 解析页面,并将每个出站链接放入队列中进行处理

    是否可以将所有发现的 url 保留在 MessageQueue 中?如果有数千个站点和数千个页面,那么队列中将有数百万条消息在等待。

    可能不是,这就是数据库的用途。处理完事情后,您应该将它们从队列中删除。队列是为了……排队。消息传输。不适用于数据存储。数据库是为数据存储而设计的。

    现在,在它们被处理之前,是的,您可以将它们留在队列中。如果您担心队列容量,您可以修改工作流程,以便队列工作人员删除任何已处理的链接,这应该会减少队列的深度。它甚至可能更有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多