Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-506 - Delegate compression to Hadoop.

Tacettin Guneyf1bebb13da82
diff --git a/CHANGES.txt b/CHANGES.txt
index e1f827499..0d90dbfa2 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -89,6 +89,8 @@ Unreleased changes (1.0-dev)
30. NUTCH-515 - Next fetch time is set incorrectly. (dogacan)
+30. NUTCH-506 - Nutch should delegate compression to Hadoop. (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/fetcher/FetcherOutputFormat.java b/src/java/org/apache/nutch/fetcher/FetcherOutputFormat.java
index d4cfcd786..de6dbcc88 100644
--- a/src/java/org/apache/nutch/fetcher/FetcherOutputFormat.java
+++ b/src/java/org/apache/nutch/fetcher/FetcherOutputFormat.java
@@ -25,6 +25,7 @@ import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.MapFile;
+import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.Text;
@@ -57,10 +58,12 @@ public class FetcherOutputFormat implements OutputFormat {
new Path(new Path(job.getOutputPath(), CrawlDatum.FETCH_DIR_NAME), name);
final Path content =
new Path(new Path(job.getOutputPath(), Content.DIR_NAME), name);
+
+ final CompressionType compType = SequenceFile.getCompressionType(job);
final MapFile.Writer fetchOut =
new MapFile.Writer(job, fs, fetch.toString(), Text.class, CrawlDatum.class,
- CompressionType.NONE, progress);
+ compType, progress);
return new RecordWriter() {
private MapFile.Writer contentOut;
@@ -70,7 +73,7 @@ public class FetcherOutputFormat implements OutputFormat {
if (Fetcher.isStoringContent(job)) {
contentOut = new MapFile.Writer(job, fs, content.toString(),
Text.class, Content.class,
- CompressionType.NONE, progress);
+ compType, progress);
}
if (Fetcher.isParsing(job)) {
diff --git a/src/java/org/apache/nutch/parse/ParseOutputFormat.java b/src/java/org/apache/nutch/parse/ParseOutputFormat.java
index ca0e3681e..e7b27ca90 100644
--- a/src/java/org/apache/nutch/parse/ParseOutputFormat.java
+++ b/src/java/org/apache/nutch/parse/ParseOutputFormat.java
@@ -85,6 +85,7 @@ public class ParseOutputFormat implements OutputFormat {
final float interval = job.getFloat("db.fetch.interval.default", 2592000.0f);
final boolean ignoreExternalLinks = job.getBoolean("db.ignore.external.links", false);
final int maxOutlinks = job.getInt("db.max.outlinks.per.page", 100);
+ final CompressionType compType = SequenceFile.getCompressionType(job);
Path text =
new Path(new Path(job.getOutputPath(), ParseText.DIR_NAME), name);
@@ -99,11 +100,11 @@ public class ParseOutputFormat implements OutputFormat {
final MapFile.Writer dataOut =
new MapFile.Writer(job, fs, data.toString(), Text.class, ParseData.class,
- CompressionType.RECORD, progress);
+ compType, progress);
final SequenceFile.Writer crawlOut =
SequenceFile.createWriter(fs, job, crawl, Text.class, CrawlDatum.class,
- CompressionType.NONE, progress);
+ compType, progress);
return new RecordWriter() {
diff --git a/src/java/org/apache/nutch/parse/ParseSegment.java b/src/java/org/apache/nutch/parse/ParseSegment.java
index 96ced9727..3157a05e3 100644
--- a/src/java/org/apache/nutch/parse/ParseSegment.java
+++ b/src/java/org/apache/nutch/parse/ParseSegment.java
@@ -69,7 +69,6 @@ public class ParseSegment extends Configured implements Mapper, Reducer {
key = newKey;
}
Content content = (Content) value;
- content.forceInflate();
ParseResult parseResult = null;
try {
diff --git a/src/java/org/apache/nutch/parse/ParseStatus.java b/src/java/org/apache/nutch/parse/ParseStatus.java
index 9e966ed6a..eb4175297 100644
--- a/src/java/org/apache/nutch/parse/ParseStatus.java
+++ b/src/java/org/apache/nutch/parse/ParseStatus.java
@@ -25,7 +25,8 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
-import org.apache.hadoop.io.VersionedWritable;
+import org.apache.hadoop.io.VersionMismatchException;
+import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableUtils;
import org.apache.hadoop.conf.Configuration;
@@ -35,9 +36,9 @@ import org.apache.nutch.metadata.Metadata;
/**
* @author Andrzej Bialecki <[email protected]>
*/
-public class ParseStatus extends VersionedWritable {
+public class ParseStatus implements Writable {
- private final static byte VERSION = 1;
+ private final static byte VERSION = 2;
// Primary status codes:
@@ -136,17 +137,32 @@ public class ParseStatus extends VersionedWritable {
}
public void readFields(DataInput in) throws IOException {
- super.readFields(in); // check version
- majorCode = in.readByte();
- minorCode = in.readShort();
- args = WritableUtils.readCompressedStringArray(in);
- }
+ byte version = in.readByte();
+ switch(version) {
+ case 1:
+ majorCode = in.readByte();
+ minorCode = in.readShort();
+ args = WritableUtils.readCompressedStringArray(in);
+ break;
+ case 2:
+ majorCode = in.readByte();
+ minorCode = in.readShort();
+ args = WritableUtils.readStringArray(in);
+ break;
+ default:
+ throw new VersionMismatchException(VERSION, version);
+ }
+ }
public void write(DataOutput out) throws IOException {
- super.write(out); // write out version
+ out.writeByte(VERSION);
out.writeByte(majorCode);
out.writeShort(minorCode);
- WritableUtils.writeCompressedStringArray(out, args);
+ if (args == null) {
+ out.writeInt(-1);
+ } else {
+ WritableUtils.writeStringArray(out, args);
+ }
}
/** A convenience method. Returns true if majorCode is SUCCESS, false
diff --git a/src/java/org/apache/nutch/parse/ParseText.java b/src/java/org/apache/nutch/parse/ParseText.java
index f55c133c2..b79fb95f7 100644
--- a/src/java/org/apache/nutch/parse/ParseText.java
+++ b/src/java/org/apache/nutch/parse/ParseText.java
@@ -26,10 +26,10 @@ import org.apache.nutch.util.NutchConfiguration;
/* The text conversion of page's content, stored using gzip compression.
* @see Parse#getText()
*/
-public final class ParseText extends VersionedWritable {
+public final class ParseText implements Writable {
public static final String DIR_NAME = "parse_text";
- private final static byte VERSION = 1;
+ private final static byte VERSION = 2;
public ParseText() {}
private String text;
@@ -38,18 +38,23 @@ public final class ParseText extends VersionedWritable {
this.text = text;
}
- public byte getVersion() { return VERSION; }
-
public void readFields(DataInput in) throws IOException {
- super.readFields(in); // check version
- text = WritableUtils.readCompressedString(in);
- return;
+ byte version = in.readByte();
+ switch (version) {
+ case 1:
+ text = WritableUtils.readCompressedString(in);
+ break;
+ case VERSION:
+ text = Text.readString(in);
+ break;
+ default:
+ throw new VersionMismatchException(VERSION, version);
+ }
}
public final void write(DataOutput out) throws IOException {
- super.write(out); // write version
- WritableUtils.writeCompressedString(out, text);
- return;
+ out.write(VERSION);
+ Text.writeString(out, text);
}
public final static ParseText read(DataInput in) throws IOException {
diff --git a/src/java/org/apache/nutch/protocol/Content.java b/src/java/org/apache/nutch/protocol/Content.java
index 919363923..36cb0d8bc 100755
--- a/src/java/org/apache/nutch/protocol/Content.java
+++ b/src/java/org/apache/nutch/protocol/Content.java
@@ -17,32 +17,35 @@
package org.apache.nutch.protocol;
+import java.io.ByteArrayInputStream;
import java.io.DataInput;
+import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
+import java.util.zip.InflaterInputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.ArrayFile;
-import org.apache.hadoop.io.CompressedWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.UTF8;
import org.apache.hadoop.io.VersionMismatchException;
+import org.apache.hadoop.io.Writable;
import org.apache.nutch.metadata.Metadata;
import org.apache.nutch.util.NutchConfiguration;
import org.apache.nutch.util.mime.MimeType;
import org.apache.nutch.util.mime.MimeTypeException;
import org.apache.nutch.util.mime.MimeTypes;
-public final class Content extends CompressedWritable {
+public final class Content implements Writable{
public static final String DIR_NAME = "content";
- private final static byte VERSION = 2;
+ private final static int VERSION = -1;
- private byte version;
+ private int version;
private String url;
@@ -58,10 +61,8 @@ public final class Content extends CompressedWritable {
private MimeTypes mimeTypes;
- private boolean inflated;
-
public Content() {
- inflated = false;
+ metadata = new Metadata();
}
public Content(String url, String base, byte[] content, String contentType,
@@ -83,21 +84,11 @@ public final class Content extends CompressedWritable {
this.mimeTypeMagic = conf.getBoolean("mime.type.magic", true);
this.mimeTypes = MimeTypes.get(conf.get("mime.types.file"));
this.contentType = getContentType(contentType, url, content);
- inflated = true;
}
- public void ensureInflated() {
- if (inflated) {
- return;
- }
- super.ensureInflated();
- inflated = true;
- }
-
- protected final void readFieldsCompressed(DataInput in) throws IOException {
- version = in.readByte();
- metadata = new Metadata();
- switch (version) {
+ private final void readFieldsCompressed(DataInput in) throws IOException {
+ byte oldVersion = in.readByte();
+ switch (oldVersion) {
case 0:
case 1:
url = UTF8.readString(in); // read url
@@ -118,7 +109,7 @@ public final class Content extends CompressedWritable {
}
}
break;
- case VERSION:
+ case 2:
url = Text.readString(in); // read url
base = Text.readString(in); // read base
@@ -129,13 +120,41 @@ public final class Content extends CompressedWritable {
metadata.readFields(in); // read meta data
break;
default:
- throw new VersionMismatchException(VERSION, version);
+ throw new VersionMismatchException((byte)2, oldVersion);
}
}
+
+ public final void readFields(DataInput in) throws IOException {
+ int sizeOrVersion = in.readInt();
+ if (sizeOrVersion < 0) { // version
+ version = sizeOrVersion;
+ switch (version) {
+ case VERSION:
+ url = Text.readString(in);
+ base = Text.readString(in);
+
+ content = new byte[in.readInt()];
+ in.readFully(content);
+
+ contentType = Text.readString(in);
+ metadata.readFields(in);
+ break;
+ default:
+ throw new VersionMismatchException((byte)VERSION, (byte)version);
+ }
+ } else { // size
+ byte[] compressed = new byte[sizeOrVersion];
+ in.readFully(compressed, 0, compressed.length);
+ ByteArrayInputStream deflated = new ByteArrayInputStream(compressed);
+ DataInput inflater =
+ new DataInputStream(new InflaterInputStream(deflated));
+ readFieldsCompressed(inflater);
+ }
+ }
- protected final void writeCompressed(DataOutput out) throws IOException {
- out.writeByte(VERSION);
+ public final void write(DataOutput out) throws IOException {
+ out.writeInt(VERSION);
Text.writeString(out, url); // write url
Text.writeString(out, base); // write base
@@ -160,7 +179,6 @@ public final class Content extends CompressedWritable {
/** The url fetched. */
public String getUrl() {
- ensureInflated();
return url;
}
@@ -168,18 +186,15 @@ public final class Content extends CompressedWritable {
* Maybe be different from url if the request redirected.
*/
public String getBaseUrl() {
- ensureInflated();
return base;
}
/** The binary content retrieved. */
public byte[] getContent() {
- ensureInflated();
return content;
}
public void setContent(byte[] content) {
- ensureInflated();
this.content = content;
}
@@ -188,34 +203,28 @@ public final class Content extends CompressedWritable {
* http://www.iana.org/assignments/media-types/</a>
*/
public String getContentType() {
- ensureInflated();
return contentType;
}
public void setContentType(String contentType) {
- ensureInflated();
this.contentType = contentType;
}
/** Other protocol-specific data. */
public Metadata getMetadata() {
- ensureInflated();
return metadata;
}
/** Other protocol-specific data. */
public void setMetadata(Metadata metadata) {
- ensureInflated();
this.metadata = metadata;
}
public boolean equals(Object o) {
- ensureInflated();
if (!(o instanceof Content)) {
return false;
}
Content that = (Content) o;
- that.ensureInflated();
return this.url.equals(that.url) && this.base.equals(that.base)
&& Arrays.equals(this.getContent(), that.getContent())
&& this.contentType.equals(that.contentType)
@@ -223,7 +232,6 @@ public final class Content extends CompressedWritable {
}
public String toString() {
- ensureInflated();
StringBuffer buffer = new StringBuffer();
buffer.append("Version: " + version + "\n");
@@ -296,13 +304,4 @@ public final class Content extends CompressedWritable {
}
return typeName;
}
-
- /**
- * By calling this method caller forces the next access to any property (via
- * getters and setters) to check if decompressing of data is really required.
- */
- public void forceInflate() {
- inflated = false;
- }
-
}
diff --git a/src/java/org/apache/nutch/protocol/ProtocolStatus.java b/src/java/org/apache/nutch/protocol/ProtocolStatus.java
index 909af4a44..c025f9b00 100644
--- a/src/java/org/apache/nutch/protocol/ProtocolStatus.java
+++ b/src/java/org/apache/nutch/protocol/ProtocolStatus.java
@@ -22,15 +22,16 @@ import java.io.DataOutput;
import java.io.IOException;
import java.util.HashMap;
-import org.apache.hadoop.io.VersionedWritable;
+import org.apache.hadoop.io.VersionMismatchException;
+import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableUtils;
/**
* @author Andrzej Bialecki
*/
-public class ProtocolStatus extends VersionedWritable {
+public class ProtocolStatus implements Writable {
- private final static byte VERSION = 1;
+ private final static byte VERSION = 2;
/** Content was retrieved without errors. */
public static final int SUCCESS = 1;
@@ -110,10 +111,6 @@ public class ProtocolStatus extends VersionedWritable {
}
- public byte getVersion() {
- return VERSION;
- }
-
public ProtocolStatus(int code, String[] args) {
this.code = code;
this.args = args;
@@ -154,17 +151,32 @@ public class ProtocolStatus extends VersionedWritable {
}
public void readFields(DataInput in) throws IOException {
- super.readFields(in); // check version
- code = in.readByte();
- lastModified = in.readLong();
- args = WritableUtils.readCompressedStringArray(in);
+ byte version = in.readByte();
+ switch(version) {
+ case 1:
+ code = in.readByte();
+ lastModified = in.readLong();
+ args = WritableUtils.readCompressedStringArray(in);
+ break;
+ case VERSION:
+ code = in.readByte();
+ lastModified = in.readLong();
+ args = WritableUtils.readStringArray(in);
+ break;
+ default:
+ throw new VersionMismatchException(VERSION, version);
+ }
}
public void write(DataOutput out) throws IOException {
- super.write(out); // write version
+ out.writeByte(VERSION);
out.writeByte((byte)code);
out.writeLong(lastModified);
- WritableUtils.writeCompressedStringArray(out, args);
+ if (args == null) {
+ out.writeInt(-1);
+ } else {
+ WritableUtils.writeStringArray(out, args);
+ }
}
public void setArgs(String[] args) {
diff --git a/src/java/org/apache/nutch/segment/SegmentMerger.java b/src/java/org/apache/nutch/segment/SegmentMerger.java
index 98fc47ff0..05ce0c510 100644
--- a/src/java/org/apache/nutch/segment/SegmentMerger.java
+++ b/src/java/org/apache/nutch/segment/SegmentMerger.java
@@ -238,13 +238,15 @@ public class SegmentMerger extends Configured implements Mapper, Reducer {
} else {
wname = new Path(new Path(new Path(job.getOutputPath(), segmentName + "-" + slice), dirName), name);
}
- res = new SequenceFile.Writer(fs, job, wname, Text.class, CrawlDatum.class, progress, new SequenceFile.Metadata());
+ res = SequenceFile.createWriter(fs, job, wname, Text.class,
+ CrawlDatum.class,
+ SequenceFile.getCompressionType(job), progress);
sliceWriters.put(slice + dirName, res);
return res;
}
// lazily create MapFile-s.
- private MapFile.Writer ensureMapFile(String slice, String dirName, Class clazz) throws IOException {
+ private MapFile.Writer ensureMapFile(String slice, String dirName, Class<? extends Writable> clazz) throws IOException {
if (slice == null) slice = DEFAULT_SLICE;
MapFile.Writer res = (MapFile.Writer)sliceWriters.get(slice + dirName);
if (res != null) return res;
@@ -254,7 +256,11 @@ public class SegmentMerger extends Configured implements Mapper, Reducer {
} else {
wname = new Path(new Path(new Path(job.getOutputPath(), segmentName + "-" + slice), dirName), name);
}
- res = new MapFile.Writer(job, fs, wname.toString(), Text.class, clazz, CompressionType.RECORD, progress);
+ CompressionType compType = SequenceFile.getCompressionType(job);
+ if (clazz.isAssignableFrom(ParseText.class)) {
+ compType = CompressionType.RECORD;
+ }
+ res = new MapFile.Writer(job, fs, wname.toString(), Text.class, clazz, compType, progress);
sliceWriters.put(slice + dirName, res);
return res;
}