Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

fix for NUTCH-2551 contributed by Hans Brende

Hanscee42e8cb625
diff --git a/src/java/org/apache/nutch/crawl/Generator.java b/src/java/org/apache/nutch/crawl/Generator.java
index 92b337cfd..a3ef91c89 100644
--- a/src/java/org/apache/nutch/crawl/Generator.java
+++ b/src/java/org/apache/nutch/crawl/Generator.java
@@ -30,6 +30,7 @@ import java.util.List;
import java.util.Map;
import java.util.Random;
+import org.apache.hadoop.conf.Configurable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.jexl2.Expression;
@@ -138,9 +139,27 @@ public class Generator extends NutchTool implements Tool {
/** Selects entries due for fetch. */
public static class Selector extends
- Partitioner<FloatWritable, Writable> {
+ Partitioner<FloatWritable, Writable> implements Configurable {
- private static URLPartitioner partitioner = new URLPartitioner();
+ private final URLPartitioner partitioner = new URLPartitioner();
+
+ /** Partition by host / domain or IP. */
+ public int getPartition(FloatWritable key, Writable value,
+ int numReduceTasks) {
+ return partitioner.getPartition(((SelectorEntry) value).url, key,
+ numReduceTasks);
+ }
+
+ @Override
+ public Configuration getConf() {
+ return partitioner.getConf();
+ }
+
+ @Override
+ public void setConf(Configuration conf) {
+ partitioner.setConf(conf);
+ }
+ }
/** Select and invert subset due for fetch. */
@@ -165,11 +184,9 @@ public class Generator extends NutchTool implements Tool {
@Override
public void setup(Mapper<Text, CrawlDatum, FloatWritable, SelectorEntry>.Context context) throws IOException{
conf = context.getConfiguration();
- Job job = Job.getInstance(conf);
curTime = conf.getLong(GENERATOR_CUR_TIME, System.currentTimeMillis());
filters = new URLFilters(conf);
scfilters = new ScoringFilters(conf);
- partitioner.configure(job);
filter = conf.getBoolean(GENERATOR_FILTER, true);
genDelay = conf.getLong(GENERATOR_DELAY, 7L) * 3600L * 24L * 1000L;
long time = conf.getLong(Nutch.GENERATE_TIME_KEY, 0L);
@@ -257,12 +274,7 @@ public class Generator extends NutchTool implements Tool {
}
}
- /** Partition by host / domain or IP. */
- public int getPartition(FloatWritable key, Writable value,
- int numReduceTasks) {
- return partitioner.getPartition(((SelectorEntry) value).url, key,
- numReduceTasks);
- }
+
/** Collect until limit is reached. */
public static class SelectorReducer extends
@@ -532,7 +544,6 @@ public class Generator extends NutchTool implements Tool {
return null;
}
}
- }
public static class DecreasingFloatComparator extends
FloatWritable.Comparator {
@@ -606,20 +617,20 @@ public class Generator extends NutchTool implements Tool {
* Update the CrawlDB so that the next generate won't include the same URLs.
*/
public static class CrawlDbUpdater {
-
+
public static class CrawlDbUpdateMapper extends
- Mapper<Text, CrawlDatum, Text, CrawlDatum> {
+ Mapper<Text, CrawlDatum, Text, CrawlDatum> {
@Override
public void map(Text key, CrawlDatum value,
- Context context)
- throws IOException, InterruptedException {
+ Context context)
+ throws IOException, InterruptedException {
context.write(key, value);
}
}
- public static class CrawlDbUpdateReducer extends
- Reducer<Text, CrawlDatum, Text, CrawlDatum> {
-
+ public static class CrawlDbUpdateReducer extends
+ Reducer<Text, CrawlDatum, Text, CrawlDatum> {
+
private CrawlDatum orig = new CrawlDatum();
private LongWritable genTime = new LongWritable(0L);
private long generateTime;
@@ -632,13 +643,13 @@ public class Generator extends NutchTool implements Tool {
@Override
public void reduce(Text key, Iterable<CrawlDatum> values,
- Context context)
- throws IOException, InterruptedException {
+ Context context)
+ throws IOException, InterruptedException {
genTime.set(0L);
for (CrawlDatum val : values) {
if (val.getMetaData().containsKey(Nutch.WRITABLE_GENERATE_TIME_KEY)) {
LongWritable gt = (LongWritable) val.getMetaData().get(
- Nutch.WRITABLE_GENERATE_TIME_KEY);
+ Nutch.WRITABLE_GENERATE_TIME_KEY);
genTime.set(gt.get());
if (genTime.get() != generateTime) {
orig.set(val);
@@ -770,9 +781,9 @@ public class Generator extends NutchTool implements Tool {
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setJarByClass(Selector.class);
- job.setMapperClass(Selector.SelectorMapper.class);
+ job.setMapperClass(SelectorMapper.class);
job.setPartitionerClass(Selector.class);
- job.setReducerClass(Selector.SelectorReducer.class);
+ job.setReducerClass(SelectorReducer.class);
FileOutputFormat.setOutputPath(job, tempDir);
job.setOutputKeyClass(FloatWritable.class);
diff --git a/src/java/org/apache/nutch/crawl/URLPartitioner.java b/src/java/org/apache/nutch/crawl/URLPartitioner.java
index cc508962e..3d4437618 100644
--- a/src/java/org/apache/nutch/crawl/URLPartitioner.java
+++ b/src/java/org/apache/nutch/crawl/URLPartitioner.java
@@ -23,9 +23,9 @@ import java.net.URL;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
+import org.apache.hadoop.conf.Configurable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
@@ -37,7 +37,7 @@ import org.apache.hadoop.mapreduce.Partitioner;
* Partition urls by host, domain name or IP depending on the value of the
* parameter 'partition.url.mode' which can be 'byHost', 'byDomain' or 'byIP'
*/
-public class URLPartitioner extends Partitioner<Text, Writable> {
+public class URLPartitioner extends Partitioner<Text, Writable> implements Configurable {
private static final Logger LOG = LoggerFactory
.getLogger(MethodHandles.lookup().lookupClass());
@@ -51,8 +51,11 @@ public class URLPartitioner extends Partitioner<Text, Writable> {
private URLNormalizers normalizers;
private String mode = PARTITION_MODE_HOST;
- public void configure(Job job) {
- Configuration conf = job.getConfiguration();
+ private Configuration conf;
+
+ @Override
+ public void setConf(Configuration conf) {
+ this.conf = conf;
seed = conf.getInt("partition.url.seed", 0);
mode = conf.get(PARTITION_MODE_KEY, PARTITION_MODE_HOST);
// check that the mode is known
@@ -64,6 +67,11 @@ public class URLPartitioner extends Partitioner<Text, Writable> {
normalizers = new URLNormalizers(conf, URLNormalizers.SCOPE_PARTITION);
}
+ @Override
+ public Configuration getConf() {
+ return conf;
+ }
+
public void close() {
}