【无标题】

2021SC@SDUSC

nutch源码分析—invertlinks
命令“bin/nutch invertlinks crawl/linkdb -dir crawl/segments”最终会调用org.apache.nutch.crawl.LinkDb的main函数。

LinkDb::main

public static void main(String[] args) throws Exception {
int res = ToolRunner.run(NutchConfiguration.create(), new LinkDb(), args);
System.exit(res);
}

ToolRunner的run函数最终调用LinkDb的run函数。

LinkDb::run

public int run(String[] args) throws Exception {
invert(db, segs.toArray(new Path[segs.size()]), normalize, filter, force);
return 0;
}

public void invert(Path linkDb, Path[] segments, boolean normalize,
boolean filter, boolean force) throws IOException {
JobConf job = LinkDb.createJob(getConf(), linkDb, normalize, filter);
Path lock = new Path(linkDb, LOCK_NAME);
FileSystem fs = FileSystem.get(getConf());
LockUtil.createLockFile(fs, lock, force);
Path currentLinkDb = new Path(linkDb, CURRENT_NAME);

for (int i = 0; i < segments.length; i++) {
  FileInputFormat.addInputPath(job, new Path(segments[i],
      ParseData.DIR_NAME));
}
JobClient.runJob(job);
if (fs.exists(currentLinkDb)) {
  Path newLinkDb = FileOutputFormat.getOutputPath(job);
  job = LinkDbMerger.createMergeJob(getConf(), linkDb, normalize, filter);
  FileInputFormat.addInputPath(job, currentLinkDb);
  FileInputFormat.addInputPath(job, newLinkDb);
  JobClient.runJob(job);
  fs.delete(newLinkDb, true);
}
LinkDb.install(job, linkDb);

}

private static JobConf createJob(Configuration config, Path linkDb,
boolean normalize, boolean filter) {
Path newLinkDb = new Path(“linkdb-”
+ Integer.toString(new Random().nextInt(Integer.MAX_VALUE)));

JobConf job = new NutchJob(config);
job.setJobName("linkdb " + linkDb);
job.setInputFormat(SequenceFileInputFormat.class);
job.setMapperClass(LinkDb.class);
job.setCombinerClass(LinkDbMerger.class);

if (normalize || filter) {
  FileSystem fs = FileSystem.get(config);
  if (!fs.exists(linkDb)) {
    job.setBoolean(LinkDbFilter.URL_FILTERING, filter);
    job.setBoolean(LinkDbFilter.URL_NORMALIZING, normalize);
  }
}
job.setReducerClass(LinkDbMerger.class);

FileOutputFormat.setOutputPath(job, newLinkDb);
job.setOutputFormat(MapFileOutputFormat.class);
job.setBoolean("mapred.output.compress", true);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Inlinks.class);

return job;

}

public static JobConf createMergeJob(Configuration config, Path linkDb,
boolean normalize, boolean filter) {
Path newLinkDb = new Path(“linkdb-merge-”
+ Integer.toString(new Random().nextInt(Integer.MAX_VALUE)));

JobConf job = new NutchJob(config);
job.setJobName("linkdb merge " + linkDb);

job.setInputFormat(SequenceFileInputFormat.class);

job.setMapperClass(LinkDbFilter.class);
job.setBoolean(LinkDbFilter.URL_NORMALIZING, normalize);
job.setBoolean(LinkDbFilter.URL_FILTERING, filter);
job.setReducerClass(LinkDbMerger.class);

FileOutputFormat.setOutputPath(job, newLinkDb);
job.setOutputFormat(MapFileOutputFormat.class);
job.setBoolean("mapred.output.compress", true);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Inlinks.class);

return job;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值