Dissecting The Nutch Crawler -Command "fetch": net.nutch.fetcher.Fetcher

      英文原文出处: DissectingTheNutchCrawler
  转载本文请注明出处:http://blog.csdn.net/pwlazy

Command "fetch": net.nutch.fetcher.Fetcher

> "fetch: fetch a segment's pages"
> Usage: Fetcher [-logLevel level] [-showThreadID] [-threads n] dir

So far we've created a webdb, primed it withURLs, and created a segment that a Fetcher can write to. Now let's look at the Fetcher itself, and try running it to see what comes out.

net.nutch.fetcher.Fetcher relies on several other classes:

Fetcher.main() reads arguments, instantiates a new Fetcher object, sets options, then calls run(). The Fetcher constructor is similarly simple; it just instantiates all of the input/output streams:

instance variable

class

arguments

fetchList

ArrayFile.Reader

(dir, "fetchlist")

fetchWriter

ArrayFile.Writer

(dir, "fetcher", FetcherOutput.class)

contentWriter

ArrayFile.Writer

(dir, "content", Content.class)

parseTextWriter

ArrayFile.Writer

(dir, "parse_text", ParseText.class)

parseDataWriter

ArrayFile.Writer

(dir, "parse_data", ParseData.class)

Fetcher.run() instantiates 1..threadCount FetcherThread objects, calls thread.start() on each, sleeps until all threads are gone or a fatal error is logged, then calls close() on the i/o streams.

FetcherThread is an inner class of net.nutch.fetcher.Fetcher that extends java.lang.Thread. It has one instance method, run(), and three static methods: handleFetch(), handleNoFetch(), and logError().

FetcherThread.run() instantiates a new FetchListEntry called "fle", then runs the following in an infinite loop:

  1. If a fatal error was logged, break

  2. Get the next entry in the FetchList, break if none remain

  3. Extract url from FetchListEntry

  4. If the FetchListEntry is not tagged "fetch", call this.handleNoFetch() with status=1. This in turn does:

  5. If is tagged "fetch", call ProtocolFactory and get Protocol and Content objects for this url

  6. Call this.handleFetch(url, fle, content). This in turn does:

    • Call ParserFactory.getParser() for this content type

    • Call parser.getParse(content)

    • Call Fetcher.outputPage() with a new FetcherOutput, including url MD5, the populated Content object, and a new ParseText

  7. On every 100th pass through loop, write a status message to the log

  8. Catch any exceptions and log as necessary

As we can see here, the fetcher relies on Factory classes to choose the code it uses for different content types: ProtocolFactory() finds a Protocol instance for a given url, and ParserFactory finds a Parser for a given contentType.

It should now be apparent that implementing a custom crawler with Nutch will revolve around creating new Protocol/Parser classes, and updating ProtocolFactory/ParserFactory to load them as needed. Let's examine these classes now.


命令fetch对应net.nutch.fetcher.Fetcher类
该命令用于抓取一个segment的所有网页
该类的调用方式如下: Fetcher [-logLevel level] [-showThreadID] [-threads n] dir

到目前为止,我们产生了一个新的webdb,并注入url,也产生了一个segment待fetcher写入,现在我们看看fetcher本身,我们可以运行它看看到底发生了什么

net.nutch.fetcher.Fetcher类依赖一下个类

Fetcher的main方法读取输入参数,接着实例化一个新的Fetcher对象,接着设置选项,然后调用run方法。Fetcher构造函数相当简单,仅仅是实例化以下几个输入输出流

实例变量名

所属类

构造函数参数

fetchList

ArrayFile.Reader

(dir, "fetchlist")

fetchWriter

ArrayFile.Writer

(dir, "fetcher", FetcherOutput.class)

contentWriter

ArrayFile.Writer

(dir, "content", Content.class)

parseTextWriter

ArrayFile.Writer

(dir, "parse_text", ParseText.class)

parseDataWriter

ArrayFile.Writer

(dir, "parse_data", ParseData



Fetch类的run方法实例化1到threadCount(译注:在调用CrawlTool的main是传入,默认为10)个 FetcherThread 对象,然后调用每个对象的start方法,接着主线程休眠直到所有子线程运行完毕或者发生了严重错误,最后调用个输入输出流的close方法。

FetcherThread 是一个net.nutch.fetcher.Fetcher的内部类,该类继承了java.lang.Thread,它有一个实例方法run()和3个静态方法:handleFetch(), handleNoFetch(), and logError().

FetcherThread 的run()方法首先实例化一个新的 FetchListEntry 对象叫"fle",接着以无限循环的方式运行一下步骤:

  1. 如果有严重错误发生,跳出循环

  2. 获取FetchList 的下一个条目,如果没有跳出循环 

  3. 从fle中获取url

  4. 如果该fle并未标记"fetch", 那么调用 this.handleNoFetch() ,调用时传入值为1的status参数. 接着会发生如下步骤:

  5. 如果该fle标记"fetch", 调用 ProtocolFactory ,并从该url获取Protocol and Content 对象

  6. 调用 this.handleFetch(url, fle, content). 以下各步骤会发生

    • 调用ParserFactory.getParser(contentType, url) (译注:contentType=content.getContentType();)

    • 调用parser.getParse(content)(译注:parser=ParserFactory.getParser(contentType, url) )

    • 接着调用outputPage(new FetcherOutput(fle, hash, protocolStatus),
                      content, new ParseText(parse.getText()), parse.getData());参数中包含一个新的 FetcherOutput 对象, including url MD5(译注:即参数中的hash),也包含一个被植入的Content对象和一个新的ParseText
  7. 每循环100次, 将状态信息写入日志

  8. 如果有必要捕捉任何意外并作记录

正如我们所见, fetcher依靠各类工厂根据不同内容类型选择代码,例如 ProtocolFactory() 根据给定的url返回相应的 Protocol实例,  ParserFactory 根据给定的内容类型返回相应的Parser实例

很明显,扩展nutch crawler可以通过产生新的Protocol/Parser 或者通过更新 ProtocolFactory /ParserFactory 按需加载他们来实现. 我们现在可以好好看看这些类。
深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值