Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-779 Mechanism for passing metadata from parse to crawldb

Julien Nioche03bc3ebd4419
diff --git a/CHANGES.txt b/CHANGES.txt
index d7503ad24..7a4296397 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -2,6 +2,8 @@ Nutch Change Log
Unreleased Changes
+* NUTCH-779 Mechanism for passing metadata from parse to crawldb (jnioche)
+
* NUTCH-784 CrawlDBScanner (jnioche)
* NUTCH-762 Generator can generate several segments in one parse of the crawlDB (jnioche)
diff --git a/conf/nutch-default.xml b/conf/nutch-default.xml
index 003ac13bf..52a2a3868 100644
--- a/conf/nutch-default.xml
+++ b/conf/nutch-default.xml
@@ -479,6 +479,15 @@
</description>
</property>
+ <property>
+ <name>db.parsemeta.to.crawldb</name>
+ <value></value>
+ <description>Comma-separated list of parse metadata keys to transfer to the crawldb (NUTCH-779).
+ Assuming for instance that the languageidentifier plugin is enabled, setting the value to 'lang'
+ will copy both the key 'lang' and its value to the corresponding entry in the crawldb.
+ </description>
+</property>
+
<property>
<name>db.fetch.retry.max</name>
<value>3</value>
diff --git a/src/java/org/apache/nutch/crawl/CrawlDatum.java b/src/java/org/apache/nutch/crawl/CrawlDatum.java
index 28091c721..dd8415f7e 100644
--- a/src/java/org/apache/nutch/crawl/CrawlDatum.java
+++ b/src/java/org/apache/nutch/crawl/CrawlDatum.java
@@ -82,6 +82,8 @@ public class CrawlDatum implements WritableComparable<CrawlDatum>, Cloneable {
public static final byte STATUS_INJECTED = 0x42;
/** Page discovered through a link. */
public static final byte STATUS_LINKED = 0x43;
+ /** Page got metadata from a parser */
+ public static final byte STATUS_PARSE_META = 0x44;
public static final HashMap<Byte, String> statNames = new HashMap<Byte, String>();
@@ -101,6 +103,7 @@ public class CrawlDatum implements WritableComparable<CrawlDatum>, Cloneable {
statNames.put(STATUS_FETCH_REDIR_PERM, "fetch_redir_perm");
statNames.put(STATUS_FETCH_GONE, "fetch_gone");
statNames.put(STATUS_FETCH_NOTMODIFIED, "fetch_notmodified");
+ statNames.put(STATUS_PARSE_META, "parse_metadata");
oldToNew.put(OLD_STATUS_DB_UNFETCHED, STATUS_DB_UNFETCHED);
oldToNew.put(OLD_STATUS_DB_FETCHED, STATUS_DB_FETCHED);
diff --git a/src/java/org/apache/nutch/crawl/CrawlDbReducer.java b/src/java/org/apache/nutch/crawl/CrawlDbReducer.java
index eee146327..2b532b8b1 100644
--- a/src/java/org/apache/nutch/crawl/CrawlDbReducer.java
+++ b/src/java/org/apache/nutch/crawl/CrawlDbReducer.java
@@ -20,6 +20,7 @@ package org.apache.nutch.crawl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Map.Entry;
import java.io.IOException;
// Commons Logging imports
@@ -71,7 +72,8 @@ public class CrawlDbReducer implements Reducer<Text, CrawlDatum, Text, CrawlDatu
byte[] signature = null;
boolean multiple = false; // avoid deep copy when only single value exists
linked.clear();
-
+ org.apache.hadoop.io.MapWritable metaFromParse = null;
+
while (values.hasNext()) {
CrawlDatum datum = (CrawlDatum)values.next();
if (!multiple && values.hasNext()) multiple = true;
@@ -120,6 +122,9 @@ public class CrawlDbReducer implements Reducer<Text, CrawlDatum, Text, CrawlDatu
case CrawlDatum.STATUS_SIGNATURE:
signature = datum.getSignature();
break;
+ case CrawlDatum.STATUS_PARSE_META:
+ metaFromParse = datum.getMetaData();
+ break;
default:
LOG.warn("Unknown status, key: " + key + ", datum: " + datum);
}
@@ -233,6 +238,11 @@ public class CrawlDbReducer implements Reducer<Text, CrawlDatum, Text, CrawlDatu
else result.setStatus(CrawlDatum.STATUS_DB_UNFETCHED);
}
result.setSignature(signature);
+ if (metaFromParse != null) {
+ for (Entry<Writable, Writable> e : metaFromParse.entrySet()) {
+ result.getMetaData().put(e.getKey(), e.getValue());
+ }
+ }
}
// if fetchInterval is larger than the system-wide maximum, trigger
// an unconditional recrawl. This prevents the page to be stuck at
diff --git a/src/java/org/apache/nutch/indexer/IndexerMapReduce.java b/src/java/org/apache/nutch/indexer/IndexerMapReduce.java
index 8a93d76b7..76cab2523 100644
--- a/src/java/org/apache/nutch/indexer/IndexerMapReduce.java
+++ b/src/java/org/apache/nutch/indexer/IndexerMapReduce.java
@@ -88,7 +88,8 @@ implements Mapper<Text, Writable, Text, NutchWritable>,
if (datum.getStatus() != CrawlDatum.STATUS_FETCH_NOTMODIFIED)
fetchDatum = datum;
} else if (CrawlDatum.STATUS_LINKED == datum.getStatus() ||
- CrawlDatum.STATUS_SIGNATURE == datum.getStatus()) {
+ CrawlDatum.STATUS_SIGNATURE == datum.getStatus() ||
+ CrawlDatum.STATUS_PARSE_META == datum.getStatus()) {
continue;
} else {
throw new RuntimeException("Unexpected status: "+datum.getStatus());
diff --git a/src/java/org/apache/nutch/parse/ParseOutputFormat.java b/src/java/org/apache/nutch/parse/ParseOutputFormat.java
index 44cbe45e4..09cadb9e5 100644
--- a/src/java/org/apache/nutch/parse/ParseOutputFormat.java
+++ b/src/java/org/apache/nutch/parse/ParseOutputFormat.java
@@ -98,6 +98,8 @@ public class ParseOutputFormat implements OutputFormat<Text, Parse> {
Path data = new Path(new Path(out, ParseData.DIR_NAME), name);
Path crawl = new Path(new Path(out, CrawlDatum.PARSE_DIR_NAME), name);
+ final String[] parseMDtoCrawlDB = job.get("db.parsemeta.to.crawldb","").split(" *, *");
+
final MapFile.Writer textOut =
new MapFile.Writer(job, fs, text.toString(), Text.class, ParseText.class,
CompressionType.RECORD, progress);
@@ -133,6 +135,20 @@ public class ParseOutputFormat implements OutputFormat<Text, Parse> {
crawlOut.append(key, d);
}
}
+
+ // see if the parse metadata contain things that we'd like
+ // to pass to the metadata of the crawlDB entry
+ CrawlDatum parseMDCrawlDatum = null;
+ for (String mdname : parseMDtoCrawlDB) {
+ String mdvalue = parse.getData().getParseMeta().get(mdname);
+ if (mdvalue != null) {
+ if (parseMDCrawlDatum == null) parseMDCrawlDatum = new CrawlDatum(
+ CrawlDatum.STATUS_PARSE_META, 0);
+ parseMDCrawlDatum.getMetaData().put(new Text(mdname),
+ new Text(mdvalue));
+ }
+ }
+ if (parseMDCrawlDatum != null) crawlOut.append(key, parseMDCrawlDatum);
try {
ParseStatus pstatus = parseData.getStatus();