Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-474 - Replace usage of ObjectWritable with something based on GenericWritable.

Tacettin Guney0e0981696571
diff --git a/CHANGES.txt b/CHANGES.txt
index fd28b49a7..23b8bdc74 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -67,6 +67,9 @@ Unreleased changes (1.0-dev)
21. NUTCH-497 - Extreme Nested Tags causes StackOverflowException in
DomContentUtils...Spider Trap. (kubes)
+22. NUTCH-434 - Replace usage of ObjectWritable with something based on
+ GenericWritable. (dogacan)
+
Release 0.9 - 2007-04-02
1. Changed log4j confiquration to log to stdout on commandline
diff --git a/src/java/org/apache/nutch/crawl/NutchWritable.java b/src/java/org/apache/nutch/crawl/NutchWritable.java
new file mode 100644
index 000000000..8c4ebd562
--- /dev/null
+++ b/src/java/org/apache/nutch/crawl/NutchWritable.java
@@ -0,0 +1,49 @@
+package org.apache.nutch.crawl;
+
+import org.apache.hadoop.io.Writable;
+import org.apache.nutch.util.GenericWritableConfigurable;
+
+public class NutchWritable extends GenericWritableConfigurable {
+
+ private static Class<? extends Writable>[] CLASSES = null;
+
+ static {
+ CLASSES = (Class<? extends Writable>[]) new Class[] {
+ org.apache.hadoop.io.NullWritable.class,
+ org.apache.hadoop.io.LongWritable.class,
+ org.apache.hadoop.io.BytesWritable.class,
+ org.apache.hadoop.io.FloatWritable.class,
+ org.apache.hadoop.io.IntWritable.class,
+ org.apache.hadoop.io.Text.class,
+ org.apache.hadoop.io.MD5Hash.class,
+ org.apache.nutch.crawl.CrawlDatum.class,
+ org.apache.nutch.crawl.Inlink.class,
+ org.apache.nutch.crawl.Inlinks.class,
+ org.apache.nutch.crawl.MapWritable.class,
+ org.apache.nutch.fetcher.FetcherOutput.class,
+ org.apache.nutch.metadata.Metadata.class,
+ org.apache.nutch.parse.Outlink.class,
+ org.apache.nutch.parse.ParseText.class,
+ org.apache.nutch.parse.ParseData.class,
+ org.apache.nutch.parse.ParseImpl.class,
+ org.apache.nutch.parse.ParseStatus.class,
+ org.apache.nutch.protocol.Content.class,
+ org.apache.nutch.protocol.ProtocolStatus.class,
+ org.apache.nutch.searcher.Hit.class,
+ org.apache.nutch.searcher.HitDetails.class,
+ org.apache.nutch.searcher.Hits.class
+ };
+ }
+
+ public NutchWritable() { }
+
+ public NutchWritable(Writable instance) {
+ set(instance);
+ }
+
+ @Override
+ protected Class<? extends Writable>[] getTypes() {
+ return CLASSES;
+ }
+
+}
diff --git a/src/java/org/apache/nutch/fetcher/Fetcher.java b/src/java/org/apache/nutch/fetcher/Fetcher.java
index 241e25047..3b276508a 100644
--- a/src/java/org/apache/nutch/fetcher/Fetcher.java
+++ b/src/java/org/apache/nutch/fetcher/Fetcher.java
@@ -32,6 +32,7 @@ import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.util.ToolBase;
import org.apache.nutch.crawl.CrawlDatum;
+import org.apache.nutch.crawl.NutchWritable;
import org.apache.nutch.crawl.SignatureFactory;
import org.apache.nutch.metadata.Metadata;
import org.apache.nutch.metadata.Nutch;
@@ -321,9 +322,9 @@ public class Fetcher extends ToolBase implements MapRunnable {
}
try {
- output.collect(key, new ObjectWritable(datum));
+ output.collect(key, new NutchWritable(datum));
if (content != null && storingContent)
- output.collect(key, new ObjectWritable(content));
+ output.collect(key, new NutchWritable(content));
if (parseResult != null) {
for (Entry<Text, Parse> entry : parseResult) {
Text url = entry.getKey();
@@ -357,7 +358,7 @@ public class Fetcher extends ToolBase implements MapRunnable {
LOG.warn("Couldn't pass score, url " + key + " (" + e + ")");
}
}
- output.collect(url, new ObjectWritable(
+ output.collect(url, new NutchWritable(
new ParseImpl(new ParseText(parse.getText()),
parse.getData(), parse.isCanonical())));
}
@@ -493,7 +494,7 @@ public class Fetcher extends ToolBase implements MapRunnable {
job.setOutputPath(segment);
job.setOutputFormat(FetcherOutputFormat.class);
job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(ObjectWritable.class);
+ job.setOutputValueClass(NutchWritable.class);
JobClient.runJob(job);
if (LOG.isInfoEnabled()) { LOG.info("Fetcher: done"); }
diff --git a/src/java/org/apache/nutch/fetcher/Fetcher2.java b/src/java/org/apache/nutch/fetcher/Fetcher2.java
index 615b24b31..c94d0fb93 100644
--- a/src/java/org/apache/nutch/fetcher/Fetcher2.java
+++ b/src/java/org/apache/nutch/fetcher/Fetcher2.java
@@ -36,6 +36,7 @@ import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.StringUtils;
import org.apache.nutch.crawl.CrawlDatum;
+import org.apache.nutch.crawl.NutchWritable;
import org.apache.nutch.crawl.SignatureFactory;
import org.apache.nutch.metadata.Metadata;
import org.apache.nutch.metadata.Nutch;
@@ -695,9 +696,9 @@ public class Fetcher2 extends Configured implements MapRunnable {
}
try {
- output.collect(key, new ObjectWritable(datum));
+ output.collect(key, new NutchWritable(datum));
if (content != null && storingContent)
- output.collect(key, new ObjectWritable(content));
+ output.collect(key, new NutchWritable(content));
if (parseResult != null) {
for (Entry<Text, Parse> entry : parseResult) {
Text url = entry.getKey();
@@ -731,7 +732,7 @@ public class Fetcher2 extends Configured implements MapRunnable {
LOG.warn("Couldn't pass score, url " + key + " (" + e + ")");
}
}
- output.collect(url, new ObjectWritable(
+ output.collect(url, new NutchWritable(
new ParseImpl(new ParseText(parse.getText()),
parse.getData(), parse.isCanonical())));
}
@@ -873,7 +874,7 @@ public class Fetcher2 extends Configured implements MapRunnable {
job.setOutputPath(segment);
job.setOutputFormat(FetcherOutputFormat.class);
job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(ObjectWritable.class);
+ job.setOutputValueClass(NutchWritable.class);
JobClient.runJob(job);
if (LOG.isInfoEnabled()) { LOG.info("Fetcher: done"); }
diff --git a/src/java/org/apache/nutch/fetcher/FetcherOutputFormat.java b/src/java/org/apache/nutch/fetcher/FetcherOutputFormat.java
index 5ba889743..d4cfcd786 100644
--- a/src/java/org/apache/nutch/fetcher/FetcherOutputFormat.java
+++ b/src/java/org/apache/nutch/fetcher/FetcherOutputFormat.java
@@ -20,11 +20,11 @@ package org.apache.nutch.fetcher;
import java.io.IOException;
import org.apache.nutch.crawl.CrawlDatum;
+import org.apache.nutch.crawl.NutchWritable;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.MapFile;
-import org.apache.hadoop.io.ObjectWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.Text;
@@ -81,7 +81,7 @@ public class FetcherOutputFormat implements OutputFormat {
public void write(WritableComparable key, Writable value)
throws IOException {
- Writable w = (Writable)((ObjectWritable)value).get();
+ Writable w = ((NutchWritable)value).get();
if (w instanceof CrawlDatum)
fetchOut.append(key, w);
diff --git a/src/java/org/apache/nutch/indexer/Indexer.java b/src/java/org/apache/nutch/indexer/Indexer.java
index a4c0630c9..e1174c765 100644
--- a/src/java/org/apache/nutch/indexer/Indexer.java
+++ b/src/java/org/apache/nutch/indexer/Indexer.java
@@ -43,6 +43,7 @@ import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.crawl.CrawlDb;
import org.apache.nutch.crawl.Inlinks;
import org.apache.nutch.crawl.LinkDb;
+import org.apache.nutch.crawl.NutchWritable;
import org.apache.lucene.index.*;
import org.apache.lucene.document.*;
@@ -55,6 +56,32 @@ public class Indexer extends ToolBase implements Reducer, Mapper {
public static final String DONE_NAME = "index.done";
public static final Log LOG = LogFactory.getLog(Indexer.class);
+
+ /** A utility class used to pass a lucene document from Indexer.reduce
+ * to Indexer.OutputFormat.
+ * Note: Despite its name, it can't properly wrap a lucene document - it
+ * doesn't know how to serialize/deserialize a lucene document.
+ */
+ private static class LuceneDocumentWrapper implements Writable {
+ private Document doc;
+
+ public LuceneDocumentWrapper(Document doc) {
+ this.doc = doc;
+ }
+
+ public Document get() {
+ return doc;
+ }
+
+ public void readFields(DataInput in) throws IOException {
+ // intentionally left blank
+ }
+
+ public void write(DataOutput out) throws IOException {
+ // intentionally left blank
+ }
+
+ }
/** Unwrap Lucene Documents created by reduce and add them to an index. */
public static class OutputFormat
@@ -87,7 +114,7 @@ public class Indexer extends ToolBase implements Reducer, Mapper {
public void write(WritableComparable key, Writable value)
throws IOException { // unwrap & index doc
- Document doc = (Document)((ObjectWritable)value).get();
+ Document doc = ((LuceneDocumentWrapper) value).get();
NutchAnalyzer analyzer = factory.get(doc.get("lang"));
if (LOG.isInfoEnabled()) {
LOG.info(" Indexing [" + doc.getField("url").stringValue() + "]" +
@@ -156,7 +183,7 @@ public class Indexer extends ToolBase implements Reducer, Mapper {
ParseData parseData = null;
ParseText parseText = null;
while (values.hasNext()) {
- Object value = ((ObjectWritable)values.next()).get(); // unwrap
+ Writable value = ((NutchWritable)values.next()).get(); // unwrap
if (value instanceof Inlinks) {
inlinks = (Inlinks)value;
} else if (value instanceof CrawlDatum) {
@@ -240,7 +267,7 @@ public class Indexer extends ToolBase implements Reducer, Mapper {
doc.add(new Field("boost", Float.toString(boost),
Field.Store.YES, Field.Index.NO));
- output.collect(key, new ObjectWritable(doc));
+ output.collect(key, new LuceneDocumentWrapper(doc));
}
public void index(Path indexDir, Path crawlDb, Path linkDb, Path[] segments)
@@ -274,7 +301,7 @@ public class Indexer extends ToolBase implements Reducer, Mapper {
job.setOutputPath(indexDir);
job.setOutputFormat(OutputFormat.class);
job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(ObjectWritable.class);
+ job.setOutputValueClass(NutchWritable.class);
JobClient.runJob(job);
if (LOG.isInfoEnabled()) { LOG.info("Indexer: done"); }
@@ -309,7 +336,7 @@ public class Indexer extends ToolBase implements Reducer, Mapper {
public void map(WritableComparable key, Writable value,
OutputCollector output, Reporter reporter) throws IOException {
- output.collect(key, new ObjectWritable(value));
+ output.collect(key, new NutchWritable(value));
}
}
diff --git a/src/java/org/apache/nutch/metadata/MetaWrapper.java b/src/java/org/apache/nutch/metadata/MetaWrapper.java
index d11424200..5315962a4 100644
--- a/src/java/org/apache/nutch/metadata/MetaWrapper.java
+++ b/src/java/org/apache/nutch/metadata/MetaWrapper.java
@@ -22,17 +22,18 @@ import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.io.ObjectWritable;
+import org.apache.hadoop.io.Writable;
+import org.apache.nutch.crawl.NutchWritable;
/**
- * This is a simple decorator that adds metadata to any Object-s that can be
- * serialized by <tt>ObjectWritable</tt>. This is useful when data needs to be
+ * This is a simple decorator that adds metadata to any Writable-s that can be
+ * serialized by <tt>NutchWritable</tt>. This is useful when data needs to be
* temporarily enriched during processing, but this
* temporary metadata doesn't need to be permanently stored after the job is done.
*
* @author Andrzej Bialecki
*/
-public class MetaWrapper extends ObjectWritable {
+public class MetaWrapper extends NutchWritable {
private Metadata metadata;
public MetaWrapper() {
@@ -40,14 +41,14 @@ public class MetaWrapper extends ObjectWritable {
metadata = new Metadata();
}
- public MetaWrapper(Object object, Configuration conf) {
- super(object);
+ public MetaWrapper(Writable instance, Configuration conf) {
+ super(instance);
metadata = new Metadata();
setConf(conf);
}
- public MetaWrapper(Metadata metadata, Object object, Configuration conf) {
- super(object);
+ public MetaWrapper(Metadata metadata, Writable instance, Configuration conf) {
+ super(instance);
if (metadata == null) metadata = new Metadata();
this.metadata = metadata;
setConf(conf);
diff --git a/src/java/org/apache/nutch/segment/SegmentMerger.java b/src/java/org/apache/nutch/segment/SegmentMerger.java
index 1ee1fc753..98fc47ff0 100644
--- a/src/java/org/apache/nutch/segment/SegmentMerger.java
+++ b/src/java/org/apache/nutch/segment/SegmentMerger.java
@@ -153,7 +153,7 @@ public class SegmentMerger extends Configured implements Mapper, Reducer {
MetaWrapper wrapper = (MetaWrapper) value;
try {
- wrapper.set(getValueClass().newInstance());
+ wrapper.set((Writable)getValueClass().newInstance());
} catch (Exception e) {
throw new IOException(e.toString());
}
diff --git a/src/java/org/apache/nutch/segment/SegmentReader.java b/src/java/org/apache/nutch/segment/SegmentReader.java
index 2583499c8..e2f5b00f9 100644
--- a/src/java/org/apache/nutch/segment/SegmentReader.java
+++ b/src/java/org/apache/nutch/segment/SegmentReader.java
@@ -42,7 +42,6 @@ import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
import org.apache.hadoop.io.MapFile;
-import org.apache.hadoop.io.ObjectWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.UTF8;
@@ -61,6 +60,7 @@ import org.apache.hadoop.mapred.SequenceFileInputFormat;
import org.apache.hadoop.mapred.SequenceFileOutputFormat;
import org.apache.hadoop.util.Progressable;
import org.apache.nutch.crawl.CrawlDatum;
+import org.apache.nutch.crawl.NutchWritable;
import org.apache.nutch.parse.ParseData;
import org.apache.nutch.parse.ParseText;
import org.apache.nutch.protocol.Content;
@@ -87,7 +87,7 @@ public class SegmentReader extends Configured implements Reducer {
newKey.set(key.toString());
key = newKey;
}
- collector.collect(key, new ObjectWritable(value));
+ collector.collect(key, new NutchWritable(value));
}
}
@@ -104,8 +104,7 @@ public class SegmentReader extends Configured implements Reducer {
final PrintStream printStream = new PrintStream(fs.create(segmentDumpFile));
return new RecordWriter() {
public synchronized void write(WritableComparable key, Writable value) throws IOException {
- ObjectWritable writable = (ObjectWritable) value;
- printStream.println((String) writable.get());
+ printStream.println(value);
}
public synchronized void close(Reporter reporter) throws IOException {
@@ -170,7 +169,7 @@ public class SegmentReader extends Configured implements Reducer {
dump.append("\nRecno:: ").append(recNo++).append("\n");
dump.append("URL:: " + key.toString() + "\n");
while (values.hasNext()) {
- Object value = ((ObjectWritable) values.next()).get(); // unwrap
+ Writable value = ((NutchWritable) values.next()).get(); // unwrap
if (value instanceof CrawlDatum) {
dump.append("\nCrawlDatum::\n").append(((CrawlDatum) value).toString());
} else if (value instanceof Content) {
@@ -183,7 +182,7 @@ public class SegmentReader extends Configured implements Reducer {
LOG.warn("Unrecognized type: " + value.getClass());
}
}
- output.collect(key, new ObjectWritable(dump.toString()));
+ output.collect(key, new Text(dump.toString()));
}
public void dump(Path segment, Path output) throws IOException {
@@ -212,7 +211,7 @@ public class SegmentReader extends Configured implements Reducer {
job.setOutputPath(tempDir);
job.setOutputFormat(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(ObjectWritable.class);
+ job.setOutputValueClass(NutchWritable.class);
JobClient.runJob(job);
diff --git a/src/java/org/apache/nutch/util/GenericWritableConfigurable.java b/src/java/org/apache/nutch/util/GenericWritableConfigurable.java
new file mode 100644
index 000000000..a7d879e75
--- /dev/null
+++ b/src/java/org/apache/nutch/util/GenericWritableConfigurable.java
@@ -0,0 +1,41 @@
+package org.apache.nutch.util;
+
+import java.io.DataInput;
+import java.io.IOException;
+
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.GenericWritable;
+import org.apache.hadoop.io.Writable;
+
+/** A generic Writable wrapper that can inject Configuration to {@link Configurable}s */
+public abstract class GenericWritableConfigurable extends GenericWritable
+ implements Configurable {
+
+ private Configuration conf;
+
+ public Configuration getConf() {
+ return conf;
+ }
+
+ public void setConf(Configuration conf) {
+ this.conf = conf;
+ }
+
+ @Override
+ public void readFields(DataInput in) throws IOException {
+ byte type = in.readByte();
+ Class clazz = getTypes()[type];
+ try {
+ set((Writable) clazz.newInstance());
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new IOException("Cannot initialize the class: " + clazz);
+ }
+ Writable w = get();
+ if (w instanceof Configurable)
+ ((Configurable)w).setConf(conf);
+ w.readFields(in);
+ }
+
+}