Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-965 Skip parsing for truncated documents

Ferdy Galema1065a5f80a98
diff --git a/CHANGES.txt b/CHANGES.txt
index b4ffca7bb..956edee70 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,5 +1,7 @@
Nutch Change Log
+* NUTCH-965 Skip parsing for truncated documents (alexis, lewismc, ferdy)
+
* NUTCH-1193 Incorrect url transform to lowercase: parameter solr (Eduardo dos Santos Leggiero via lewismc)
* NUTCH-1272 Wrong property name for index-static in nutch-default.xml (Daniel Baur via jnioche)
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index a4a334167..cc9fe7a8b 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -1062,6 +1062,14 @@
<description>Whether the parser will normalize URLs (with the configured URL normalizers).</description>
</property>
+<property>
+ <name>parser.skip.truncated</name>
+ <value>true</value>
+ <description>Boolean value for whether we should skip parsing for truncated documents. By default this
+ property is activated due to extremely high levels of CPU which parsing can sometimes take.
+ </description>
+</property>
+
<!-- urlfilter plugin properties -->
<property>
diff --git a/src/java/org/apache/nutch/fetcher/Fetcher.java b/src/java/org/apache/nutch/fetcher/Fetcher.java
index 1207abf8e..638f8d2b4 100644
--- a/src/java/org/apache/nutch/fetcher/Fetcher.java
+++ b/src/java/org/apache/nutch/fetcher/Fetcher.java
@@ -589,6 +589,7 @@ public class Fetcher extends Configured implements Tool,
private int maxOutlinkDepth;
private int maxOutlinkDepthNumLinks;
private int outlinksDepthDivisor;
+ private boolean checkTruncated;
public FetcherThread(Configuration conf) {
this.setDaemon(true); // don't hang JVM on exit
@@ -597,6 +598,7 @@ public class Fetcher extends Configured implements Tool,
this.urlFilters = new URLFilters(conf);
this.scfilters = new ScoringFilters(conf);
this.parseUtil = new ParseUtil(conf);
+ this.checkTruncated = conf.getBoolean(ParseSegment.SKIP_TRUNCATED, true);
this.protocolFactory = new ProtocolFactory(conf);
this.normalizers = new URLNormalizers(conf, URLNormalizers.SCOPE_FETCHER);
this.maxCrawlDelay = conf.getInt("fetcher.max.crawl.delay", 30) * 1000;
@@ -945,17 +947,19 @@ public class Fetcher extends Configured implements Tool,
/* Note: Fetcher will only follow meta-redirects coming from the
* original URL. */
if (parsing && status == CrawlDatum.STATUS_FETCH_SUCCESS) {
- try {
- parseResult = this.parseUtil.parse(content);
- } catch (Exception e) {
- LOG.warn("Error parsing: " + key + ": " + StringUtils.stringifyException(e));
- }
-
- if (parseResult == null) {
- byte[] signature =
- SignatureFactory.getSignature(getConf()).calculate(content,
- new ParseStatus().getEmptyParse(conf));
- datum.setSignature(signature);
+ if (!checkTruncated || (checkTruncated && ParseSegment.isTruncated(content))) {
+ try {
+ parseResult = this.parseUtil.parse(content);
+ } catch (Exception e) {
+ LOG.warn("Error parsing: " + key + ": " + StringUtils.stringifyException(e));
+ }
+
+ if (parseResult == null) {
+ byte[] signature =
+ SignatureFactory.getSignature(getConf()).calculate(content,
+ new ParseStatus().getEmptyParse(conf));
+ datum.setSignature(signature);
+ }
}
}
diff --git a/src/java/org/apache/nutch/parse/ParseSegment.java b/src/java/org/apache/nutch/parse/ParseSegment.java
index 513acb8f1..0855f3116 100644
--- a/src/java/org/apache/nutch/parse/ParseSegment.java
+++ b/src/java/org/apache/nutch/parse/ParseSegment.java
@@ -17,26 +17,44 @@
package org.apache.nutch.parse;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Iterator;
+import java.util.Map.Entry;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.conf.Configured;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.UTF8;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.FileOutputFormat;
+import org.apache.hadoop.mapred.JobClient;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reducer;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hadoop.mapred.SequenceFileInputFormat;
+import org.apache.hadoop.util.StringUtils;
+import org.apache.hadoop.util.Tool;
+import org.apache.hadoop.util.ToolRunner;
import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.crawl.SignatureFactory;
-import org.apache.hadoop.io.*;
-import org.apache.hadoop.mapred.*;
-import org.apache.hadoop.util.*;
-import org.apache.hadoop.conf.*;
+import org.apache.nutch.metadata.Metadata;
import org.apache.nutch.metadata.Nutch;
-import org.apache.nutch.protocol.*;
+import org.apache.nutch.net.protocols.Response;
+import org.apache.nutch.protocol.Content;
import org.apache.nutch.scoring.ScoringFilterException;
import org.apache.nutch.scoring.ScoringFilters;
-import org.apache.nutch.util.*;
-import org.apache.hadoop.fs.Path;
-
-import java.io.*;
-import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.Map.Entry;
+import org.apache.nutch.util.NutchConfiguration;
+import org.apache.nutch.util.NutchJob;
+import org.apache.nutch.util.StringUtil;
+import org.apache.nutch.util.TimingUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/* Parse content in a segment. */
public class ParseSegment extends Configured implements Tool,
@@ -45,8 +63,12 @@ public class ParseSegment extends Configured implements Tool,
public static final Logger LOG = LoggerFactory.getLogger(ParseSegment.class);
+ public static final String SKIP_TRUNCATED = "parser.skip.truncated";
+
private ScoringFilters scfilters;
+ private boolean skipTruncated;
+
public ParseSegment() {
this(null);
}
@@ -58,6 +80,7 @@ public class ParseSegment extends Configured implements Tool,
public void configure(JobConf job) {
setConf(job);
this.scfilters = new ScoringFilters(job);
+ skipTruncated=job.getBoolean(SKIP_TRUNCATED, true);
}
public void close() {}
@@ -80,6 +103,10 @@ public class ParseSegment extends Configured implements Tool,
LOG.debug("Skipping " + key + " as content is not fetched successfully");
return;
}
+
+ if (isTruncated(content)) {
+ return;
+ }
ParseResult parseResult = null;
try {
@@ -128,6 +155,38 @@ public class ParseSegment extends Configured implements Tool,
parse.getData(), parse.isCanonical()));
}
}
+
+ /**
+ * Checks if the page's content is truncated.
+ * @param content
+ * @return If the page is truncated <code>true</code>. When it is not,
+ * or when it could be determined, <code>false</code>.
+ */
+ public static boolean isTruncated(Content content) {
+ byte[] contentBytes = content.getContent();
+ if (contentBytes == null) return false;
+ Metadata metadata = content.getMetadata();
+ if (metadata == null) return false;
+
+ String lengthStr = metadata.get(Response.CONTENT_LENGTH);
+ if (lengthStr != null) lengthStr=lengthStr.trim();
+ if (StringUtil.isEmpty(lengthStr)) {
+ return false;
+ }
+ int contentLength;
+ try {
+ contentLength = Integer.parseInt(lengthStr);
+ } catch (NumberFormatException e) {
+ LOG.warn("Wrong contentlength format for " + content.getUrl(), e);
+ return false;
+ }
+ if (contentLength > contentBytes.length) {
+ LOG.info(content.getUrl() + " skipped. Content of size " + contentLength
+ + " was truncated to " + contentBytes.length);
+ return true;
+ }
+ return false;
+ }
public void reduce(Text key, Iterator<Writable> values,
OutputCollector<Text, Writable> output, Reporter reporter)