MapReduce1:选择要获取的urls
[list]
输入:爬虫数据库文件
public Path generate(...) {
...
job.setInputPath(new Path(dbDir, CrawlDb.CURRENT_NAME));
job.setInputFormat(SequenceFileInputFormat.class);
}
Map() -> 如果date <= now, 反转成<CrawlDatum, url>
/** Selects entries due for fetch. */
public static class Selector implements Mapper ...{
private SelectorEntry entry = new SelectorEntry();
/** Select & invert subset due for fetch. */
public void map(WritableComparable key, Writable value,
OutputCollector output, Reporter reporter)
throws IOException {
Text url = (Text)key;
...
CrawlDatum crawlDatum = (CrawlDatum)value;
if (crawlDatum.getStatus() == CrawlDatum.STATUS_DB_GONE ||
crawlDatum.getStatus() == CrawlDatum.STATUS_DB_REDIR_PERM)
return; // don't retry
if (crawlDatum.getFetchTime() > curTime)
return; // not time yet
LongWritable oldGenTime = (LongWritable)crawlDatum.getMetaData().get(Nutch.WRITABLE_GENERATE_TIME_KEY);
if (oldGenTime != null) { // awaiting fetch & update
if (oldGenTime.get() + genDelay > curTime) // still wait for update
return;
}
...
// record generation time
crawlDatum.getMetaData().put(Nutch.WRITABLE_GENERATE_TIME_KEY, genTime);
entry.datum = crawlDatum;
entry.url = (Text)key;
output.collect(sortValue, entry); // invert for sort by score
}
}
以随机整数为种子, 用hash函数来划分数据块
/**
* Generate fetchlists in a segment.
* @return Path to generated segment or null if no entries were selected.
* */
public Path generate(...) {
...
job.setInt("partition.url.by.host.seed", new Random().nextInt());
}
public static class Selector implements Mapper, Partitioner, Reducer {
private Partitioner hostPartitioner = new PartitionUrlByHost();
...
/** Partition by host. */
public int getPartition(WritableComparable key, Writable value,
int numReduceTasks) {
return hostPartitioner.getPartition(((SelectorEntry)value).url, key,
numReduceTasks);
}
...
}
/** Partition urls by hostname. */
public class PartitionUrlByHost implements Partitioner {
private int seed;
...
public void configure(JobConf job) {
seed = job.getInt("partition.url.by.host.seed", 0);
...
}
/** Hash by hostname. */
public int getPartition(WritableComparable key, Writable value,
int numReduceTasks) {
...
int hashCode = (url==null ? urlString : url.getHost()).hashCode();
// make hosts wind up in different partitions on different runs
hashCode ^= seed;
return (hashCode & Integer.MAX_VALUE) % numReduceTasks;
}
}
Reduce()是同一化
以CrawlDatum.linkCount降序排序
输出链接数最多的N个CrawlDatum实体
[/list]
MapReduce2:准备获取
- Map()是反向;Partition()根据主机划分;Reduce()是同一化
- Reduce: 合并CrawlDatum成单个入口
- 输出: <url,CrawlDatum>文件集,用来并行地获取
分享到:
相关推荐
### Nutch 1.2 源码阅读深入解析 #### Crawl类核心作用与流程概览 在深入了解Nutch 1.2源码之前,我们先明确Nutch的架构和工作流程。Nutch作为一款开源搜索引擎框架,其功能涵盖网页抓取、索引构建以及查询处理。...
通过理解和分析 `Crawl` 类的源码,开发者可以更好地掌握 Nutch 如何处理输入,以及如何根据这些输入配置和执行复杂的网络抓取任务。对于想要深入了解 Nutch 或者想要定制 Nutch 功能的人来说,研究 `Crawl` 类的...
7. **Indexing**:Nutch 可以与 Solr 结合,通过 `solrindex` 命令将解析后的数据索引到 Solr 中,便于快速查询和分析。 8. **Search**:最后,用户可以通过 Solr 的搜索接口查询索引数据,得到搜索结果。 Nutch ...
1. **源码目录结构**:解压后的apache-nutch-2.3目录包含了源代码、配置文件、构建脚本等。主要目录有`src/main/`,其中`src/main/java`存放Java源码,`src/main/resources`存储配置文件,`src/main/webapp`包含Web...
本篇将基于提供的文件内容对 Nutch 的参数设置进行深入解析,帮助读者更好地理解 Nutch 中各个组件的工作原理及配置方式。 #### Injector.java **功能概述:** Injector 主要负责将待爬取的 URL 注入到 CrawlDB...
1. 下载与安装:获取HiBench的源代码,编译并安装。 2. 配置环境:根据目标测试环境配置HiBench的配置文件。 3. 选择工作负载:根据需求选择合适的基准测试。 4. 执行测试:运行测试驱动器,启动测试。 5. 分析结果...