Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Merge branch 'master' into NUTCH-2375

Omkarfe5bfb43b1f8
diff --cc src/java/org/apache/nutch/crawl/CrawlDbFilter.java
index fb40a88ef,8b46ecb92..f143c7d73
--- a/src/java/org/apache/nutch/crawl/CrawlDbFilter.java
+++ b/src/java/org/apache/nutch/crawl/CrawlDbFilter.java
@@@ -54,19 -55,18 +54,20 @@@ public class CrawlDbFilter extend
private static final Logger LOG = LoggerFactory
.getLogger(MethodHandles.lookup().lookupClass());
- public void configure(JobConf job) {
- urlFiltering = job.getBoolean(URL_FILTERING, false);
- urlNormalizers = job.getBoolean(URL_NORMALIZING, false);
- url404Purging = job.getBoolean(CrawlDb.CRAWLDB_PURGE_404, false);
- purgeOrphans = job.getBoolean(CrawlDb.CRAWLDB_PURGE_ORPHANS, false);
++
+ public void setup(Mapper<Text, CrawlDatum, Text, CrawlDatum>.Context context) {
+ Configuration conf = context.getConfiguration();
+ urlFiltering = conf.getBoolean(URL_FILTERING, false);
+ urlNormalizers = conf.getBoolean(URL_NORMALIZING, false);
+ url404Purging = conf.getBoolean(CrawlDb.CRAWLDB_PURGE_404, false);
+ purgeOrphans = conf.getBoolean(CrawlDb.CRAWLDB_PURGE_ORPHANS, false);
if (urlFiltering) {
- filters = new URLFilters(job);
+ filters = new URLFilters(conf);
}
if (urlNormalizers) {
- scope = job.get(URL_NORMALIZING_SCOPE, URLNormalizers.SCOPE_CRAWLDB);
- normalizers = new URLNormalizers(job, scope);
+ scope = conf.get(URL_NORMALIZING_SCOPE, URLNormalizers.SCOPE_CRAWLDB);
+ normalizers = new URLNormalizers(conf, scope);
}
}
diff --cc src/java/org/apache/nutch/crawl/CrawlDbReader.java
index d83677cf4,ddd25ef5c..db6a7d12e
--- a/src/java/org/apache/nutch/crawl/CrawlDbReader.java
+++ b/src/java/org/apache/nutch/crawl/CrawlDbReader.java
@@@ -242,10 -241,10 +242,9 @@@ public class CrawlDbReader extends Conf
public void close() {
}
- public void reduce(Text key, Iterator<NutchWritable> values,
- OutputCollector<Text, NutchWritable> output, Reporter reporter)
- throws IOException {
-
+ public void reduce(Text key, Iterable<NutchWritable> values,
+ Context context)
+ throws IOException, InterruptedException {
-
String k = key.toString();
if (k.equals("T") || k.startsWith("status") || k.startsWith("retry")
|| k.equals("ftt") || k.equals("fit")) {
@@@ -640,8 -632,8 +639,7 @@@
LOG.info("CrawlDb db: expr: " + expr);
}
if (sample != null)
- job.setFloat("sample", sample);
-
+ config.setFloat("sample", sample);
-
job.setMapperClass(CrawlDbDumpMapper.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(CrawlDatum.class);
diff --cc src/java/org/apache/nutch/crawl/CrawlDbReducer.java
index ba90d44c8,cb04e638e..b5851adbe
--- a/src/java/org/apache/nutch/crawl/CrawlDbReducer.java
+++ b/src/java/org/apache/nutch/crawl/CrawlDbReducer.java
@@@ -158,7 -155,7 +158,8 @@@ public class CrawlDbReducer extend
// still no new data - record only unchanged old data, if exists, and return
if (!fetchSet) {
- if (oldSet) { // at this point at least "old" should be present
+ if (oldSet) {// at this point at least "old" should be present
++
// set score for orphaned pages (not fetched in the current cycle and
// with no inlinks)
try {
diff --cc src/java/org/apache/nutch/crawl/Generator.java
index c9b43f907,6c74aab73..852cd3f7d
--- a/src/java/org/apache/nutch/crawl/Generator.java
+++ b/src/java/org/apache/nutch/crawl/Generator.java
@@@ -280,243 -301,193 +280,243 @@@ public class Generator extends NutchToo
}
}
}
-
- close();
- return null;
- }
-
- private JexlContext createContext(HostDatum datum) {
- JexlContext context = new MapContext();
- context.set("dnsFailures", datum.getDnsFailures());
- context.set("connectionFailures", datum.getConnectionFailures());
- context.set("unfetched", datum.getUnfetched());
- context.set("fetched", datum.getFetched());
- context.set("notModified", datum.getNotModified());
- context.set("redirTemp", datum.getRedirTemp());
- context.set("redirPerm", datum.getRedirPerm());
- context.set("gone", datum.getGone());
- context.set("conf", conf);
-
- // Set metadata variables
- for (Map.Entry<Writable, Writable> entry : datum.getMetaData().entrySet()) {
- Object value = entry.getValue();
-
- if (value instanceof FloatWritable) {
- FloatWritable fvalue = (FloatWritable)value;
- Text tkey = (Text)entry.getKey();
- context.set(tkey.toString(), fvalue.get());
- }
-
- if (value instanceof IntWritable) {
- IntWritable ivalue = (IntWritable)value;
- Text tkey = (Text)entry.getKey();
- context.set(tkey.toString(), ivalue.get());
- }
-
- if (value instanceof Text) {
- Text tvalue = (Text)value;
- Text tkey = (Text)entry.getKey();
- context.set(tkey.toString().replace("-", "_"), tvalue.toString());
- }
- }
-
- return context;
- }
- /** Collect until limit is reached. */
- public void reduce(FloatWritable key, Iterator<SelectorEntry> values,
- OutputCollector<FloatWritable, SelectorEntry> output, Reporter reporter)
- throws IOException {
-
- String hostname = null;
- HostDatum host = null;
- LongWritable variableFetchDelayWritable = null; // in millis
- Text variableFetchDelayKey = new Text("_variableFetchDelay_");
- int maxCount = this.maxCount;
- while (values.hasNext()) {
- SelectorEntry entry = values.next();
- Text url = entry.url;
- String urlString = url.toString();
- URL u = null;
-
- // Do this only once per queue
- if (host == null) {
+ public void close() {
+ if (hostdbReaders != null) {
try {
- hostname = URLUtil.getHost(urlString);
- host = getHostDatum(hostname);
- } catch (Exception e) {}
-
- // Got it?
- if (host == null) {
- // Didn't work, prevent future lookups
- host = new HostDatum();
- } else {
- if (maxCountExpr != null) {
- long variableMaxCount = Math.round((double)maxCountExpr.evaluate(createContext(host)));
- LOG.info("Generator: variable maxCount: {} for {}", variableMaxCount, hostname);
- maxCount = (int)variableMaxCount;
- }
- if (fetchDelayExpr != null) {
- long variableFetchDelay = Math.round((double)fetchDelayExpr.evaluate(createContext(host)));
- LOG.info("Generator: variable fetchDelay: {} ms for {}", variableFetchDelay, hostname);
- variableFetchDelayWritable = new LongWritable(variableFetchDelay);
+ for (int i = 0; i < hostdbReaders.length; i++) {
+ hostdbReaders[i].close();
}
+ } catch (IOException e) {
+ LOG.error("Error closing HostDB because {}", e.getMessage());
}
}
+ }
+
+ private JexlContext createContext(HostDatum datum) {
+ JexlContext context = new MapContext();
+ context.set("dnsFailures", datum.getDnsFailures());
+ context.set("connectionFailures", datum.getConnectionFailures());
+ context.set("unfetched", datum.getUnfetched());
+ context.set("fetched", datum.getFetched());
+ context.set("notModified", datum.getNotModified());
+ context.set("redirTemp", datum.getRedirTemp());
+ context.set("redirPerm", datum.getRedirPerm());
+ context.set("gone", datum.getGone());
+ context.set("conf", conf);
- if(maxCount == 0){
- continue;
- }
-
- // Got a non-zero variable fetch delay? Add it to the datum's metadata
- if (variableFetchDelayWritable != null) {
- entry.datum.getMetaData().put(variableFetchDelayKey, variableFetchDelayWritable);
+ // Set metadata variables
+ for (Map.Entry<Writable, Writable> entry : datum.getMetaData().entrySet()) {
+ Object value = entry.getValue();
+
+ if (value instanceof FloatWritable) {
+ FloatWritable fvalue = (FloatWritable)value;
+ Text tkey = (Text)entry.getKey();
+ context.set(tkey.toString(), fvalue.get());
+ }
+
+ if (value instanceof IntWritable) {
+ IntWritable ivalue = (IntWritable)value;
+ Text tkey = (Text)entry.getKey();
+ context.set(tkey.toString(), ivalue.get());
+ }
+
+ if (value instanceof Text) {
+ Text tvalue = (Text)value;
+ Text tkey = (Text)entry.getKey();
+ context.set(tkey.toString().replace("-", "_"), tvalue.toString());
+ }
}
- if (count == limit) {
- // do we have any segments left?
- if (currentsegmentnum < maxNumSegments) {
- count = 0;
- currentsegmentnum++;
- } else
- break;
+ return context;
+ }
+
+ @Override
+ public void setup(Context context) throws IOException {
+ conf = context.getConfiguration();
+ mos = new MultipleOutputs(context);
+ Job job = Job.getInstance(conf);
+ limit = conf.getLong(GENERATOR_TOP_N, Long.MAX_VALUE)
+ / job.getNumReduceTasks();
+ maxNumSegments = conf.getInt(GENERATOR_MAX_NUM_SEGMENTS, 1);
+ segCounts = new int[maxNumSegments];
+ maxCount = conf.getInt(GENERATOR_MAX_COUNT, -1);
+ if (maxCount == -1) {
+ byDomain = false;
+ }
+ if (GENERATOR_COUNT_VALUE_DOMAIN.equals(conf.get(GENERATOR_COUNT_MODE)))
+ byDomain = true;
+ normalise = conf.getBoolean(GENERATOR_NORMALISE, true);
+ if (normalise)
+ normalizers = new URLNormalizers(conf,
+ URLNormalizers.SCOPE_GENERATE_HOST_COUNT);
+
+ if (conf.get(GENERATOR_HOSTDB) != null) {
+ maxCountExpr = JexlUtil.parseExpression(conf.get(GENERATOR_MAX_COUNT_EXPR, null));
+ fetchDelayExpr = JexlUtil.parseExpression(conf.get(GENERATOR_FETCH_DELAY_EXPR, null));
}
+ }
- String hostordomain = null;
+ @Override
+ public void cleanup(Context context) throws IOException, InterruptedException {
+ mos.close();
+ }
- try {
- if (normalise && normalizers != null) {
- urlString = normalizers.normalize(urlString,
- URLNormalizers.SCOPE_GENERATE_HOST_COUNT);
+ @Override
+ public void reduce(FloatWritable key, Iterable<SelectorEntry> values,
+ Context context)
+ throws IOException, InterruptedException {
+
+ String hostname = null;
+ HostDatum host = null;
+ LongWritable variableFetchDelayWritable = null; // in millis
+ Text variableFetchDelayKey = new Text("_variableFetchDelay_");
+ int temp_maxCount = maxCount;
+ for (SelectorEntry entry : values) {
+ Text url = entry.url;
+ String urlString = url.toString();
+ URL u = null;
+
+ // Do this only once per queue
+ if (host == null) {
+ try {
+ hostname = URLUtil.getHost(urlString);
+ host = getHostDatum(hostname);
+ } catch (Exception e) {}
+
+ // Got it?
+ if (host == null) {
+ // Didn't work, prevent future lookups
+ host = new HostDatum();
+ } else {
+ if (maxCountExpr != null) {
+ long variableMaxCount = Math.round((double)maxCountExpr.evaluate(createContext(host)));
+ LOG.info("Generator: variable maxCount: {} for {}", variableMaxCount, hostname);
+ maxCount = (int)variableMaxCount;
+ }
+
+ if (fetchDelayExpr != null) {
+ long variableFetchDelay = Math.round((double)fetchDelayExpr.evaluate(createContext(host)));
+ LOG.info("Generator: variable fetchDelay: {} ms for {}", variableFetchDelay, hostname);
+ variableFetchDelayWritable = new LongWritable(variableFetchDelay);
+ }
+ }
}
-
- u = new URL(urlString);
- if (byDomain) {
- hostordomain = URLUtil.getDomainName(u);
- } else {
- hostordomain = new URL(urlString).getHost();
++
+ // Got a non-zero variable fetch delay? Add it to the datum's metadata
+ if (variableFetchDelayWritable != null) {
+ entry.datum.getMetaData().put(variableFetchDelayKey, variableFetchDelayWritable);
}
- } catch (Exception e) {
- LOG.warn("Malformed URL: '" + urlString + "', skipping ("
- + StringUtils.stringifyException(e) + ")");
- reporter.getCounter("Generator", "MALFORMED_URL").increment(1);
- continue;
- }
- hostordomain = hostordomain.toLowerCase();
-
- // only filter if we are counting hosts or domains
- if (maxCount > 0) {
- int[] hostCount = hostCounts.get(hostordomain);
- if (hostCount == null) {
- hostCount = new int[] { 1, 0 };
- hostCounts.put(hostordomain, hostCount);
+ if (count == limit) {
+ // do we have any segments left?
+ if (currentsegmentnum < maxNumSegments) {
+ count = 0;
+ currentsegmentnum++;
+ } else
+ break;
}
- // increment hostCount
- hostCount[1]++;
+ String hostordomain = null;
- // check if topN reached, select next segment if it is
- while (segCounts[hostCount[0] - 1] >= limit
- && hostCount[0] < maxNumSegments) {
- hostCount[0]++;
- hostCount[1] = 0;
+ try {
+ if (normalise && normalizers != null) {
+ urlString = normalizers.normalize(urlString,
+ URLNormalizers.SCOPE_GENERATE_HOST_COUNT);
+ }
+ u = new URL(urlString);
+ if (byDomain) {
+ hostordomain = URLUtil.getDomainName(u);
+ } else {
+ hostordomain = new URL(urlString).getHost();
+ }
+ } catch (Exception e) {
+ LOG.warn("Malformed URL: '" + urlString + "', skipping ("
+ + StringUtils.stringifyException(e) + ")");
+ context.getCounter("Generator", "MALFORMED_URL").increment(1);
+ continue;
}
- // reached the limit of allowed URLs per host / domain
- // see if we can put it in the next segment?
- if (hostCount[1] >= maxCount) {
- if (hostCount[0] < maxNumSegments) {
+ hostordomain = hostordomain.toLowerCase();
+
+ // only filter if we are counting hosts or domains
+ if (maxCount > 0) {
+ int[] hostCount = hostCounts.get(hostordomain);
+ if (hostCount == null) {
+ hostCount = new int[] { 1, 0 };
+ hostCounts.put(hostordomain, hostCount);
+ }
+
+ // increment hostCount
+ hostCount[1]++;
+
+ // check if topN reached, select next segment if it is
+ while (segCounts[hostCount[0] - 1] >= limit
+ && hostCount[0] < maxNumSegments) {
hostCount[0]++;
hostCount[1] = 0;
- } else {
- if (hostCount[1] == maxCount + 1 && LOG.isInfoEnabled()) {
- LOG.info("Host or domain "
- + hostordomain
- + " has more than "
- + maxCount
- + " URLs for all "
- + maxNumSegments
- + " segments. Additional URLs won't be included in the fetchlist.");
+ }
+
+ // reached the limit of allowed URLs per host / domain
+ // see if we can put it in the next segment?
+ if (hostCount[1] >= maxCount) {
+ if (hostCount[0] < maxNumSegments) {
+ hostCount[0]++;
+ hostCount[1] = 0;
+ } else {
+ if (hostCount[1] == maxCount + 1 && LOG.isInfoEnabled()) {
+ LOG.info("Host or domain "
+ + hostordomain
+ + " has more than "
+ + maxCount
+ + " URLs for all "
+ + maxNumSegments
+ + " segments. Additional URLs won't be included in the fetchlist.");
+ }
+ // skip this entry
+ continue;
}
- // skip this entry
- continue;
}
+ entry.segnum = new IntWritable(hostCount[0]);
+ segCounts[hostCount[0] - 1]++;
+ } else {
+ entry.segnum = new IntWritable(currentsegmentnum);
+ segCounts[currentsegmentnum - 1]++;
}
- entry.segnum = new IntWritable(hostCount[0]);
- segCounts[hostCount[0] - 1]++;
- } else {
- entry.segnum = new IntWritable(currentsegmentnum);
- segCounts[currentsegmentnum - 1]++;
- }
- output.collect(key, entry);
+ outputFile = generateFileName(entry);
+ mos.write("sequenceFiles", key, entry, outputFile);
+ context.write(key,entry);
- // Count is incremented only when we keep the URL
- // maxCount may cause us to skip it.
- count++;
+ // Count is incremented only when we keep the URL
+ // maxCount may cause us to skip it.
+ count++;
+ }
+ maxCount = temp_maxCount;
+ }
+
+ private String generateFileName(SelectorEntry entry) {
+ return "fetchlist-" + entry.segnum.toString() + "/part";
}
- }
- }
- // Allows the reducers to generate one subfile per
- public static class GeneratorOutputFormat extends
- MultipleSequenceFileOutputFormat<FloatWritable, SelectorEntry> {
- // generate a filename based on the segnum stored for this entry
- protected String generateFileNameForKeyValue(FloatWritable key,
- SelectorEntry value, String name) {
- return "fetchlist-" + value.segnum.toString() + "/" + name;
- }
+ private HostDatum getHostDatum(String host) throws Exception {
+ Text key = new Text();
+ HostDatum value = new HostDatum();
+
+ open();
+ for (int i = 0; i < hostdbReaders.length; i++) {
+ while (hostdbReaders[i].next(key, value)) {
+ if (host.equals(key.toString())) {
+ close();
+ return value;
+ }
+ }
+ }
+ close();
+ return null;
+ }
+ }
}
public static class DecreasingFloatComparator extends