Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2867 Support for custom HostDb aggregators (patch contributed by markus)

Sebastian Nagel1e7eb52f1070
diff --git a/src/java/org/apache/nutch/hostdb/AbstractCrawlDatumProcessor.java b/src/java/org/apache/nutch/hostdb/AbstractCrawlDatumProcessor.java
new file mode 100644
index 000000000..b991b4436
--- /dev/null
+++ b/src/java/org/apache/nutch/hostdb/AbstractCrawlDatumProcessor.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nutch.hostdb;
+
+import org.apache.hadoop.conf.Configuration;
+
+import org.apache.nutch.crawl.CrawlDatum;
+
+/**
+ * These are instaniated once for each host.
+ */
+public interface AbstractCrawlDatumProcessor {
+
+ /**
+ * Process a single crawl datum instance.
+ *
+ * @param crawlDatum
+ */
+ public void count(CrawlDatum crawlDatum);
+
+ /**
+ * Process the final host datum inst
+ *
+ * @param hostDatum
+ */
+ public void finalize(HostDatum hostDatum);
+
+}
diff --git a/src/java/org/apache/nutch/hostdb/FetchOverdueCrawlDatumProcessor.java b/src/java/org/apache/nutch/hostdb/FetchOverdueCrawlDatumProcessor.java
new file mode 100644
index 000000000..afb5bfce5
--- /dev/null
+++ b/src/java/org/apache/nutch/hostdb/FetchOverdueCrawlDatumProcessor.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nutch.hostdb;
+
+import java.util.Date;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.LongWritable;
+import org.apache.hadoop.io.Text;
+
+import org.apache.nutch.crawl.CrawlDatum;
+
+/**
+ * Simple custom crawl datum processor that counts the number of records
+ * that are overdue for fetching, e.g. new unfetched URLs that haven't
+ * been fetched within two days.
+ */
+public class FetchOverdueCrawlDatumProcessor implements AbstractCrawlDatumProcessor {
+
+ protected final Configuration conf;
+ protected long overDueTimeLimit = 60 * 60 * 24 * 2 * 1000l; // two days
+ protected long overDueTime;
+ protected long numOverDue = 0;
+
+ /**
+ * Constructs a new ExampleCrawlDatumProcessor.
+ */
+ public FetchOverdueCrawlDatumProcessor(Configuration conf) {
+ this.conf = conf;
+
+ overDueTimeLimit = conf.getLong("crawl.datum.processor.overdue.time.limit", 60 * 60 * 24 * 2 * 1000l);
+ overDueTime = System.currentTimeMillis() - overDueTimeLimit;
+ }
+
+ /**
+ * Process a single crawl datum instance.
+ *
+ * @param crawlDatum
+ */
+ @Override
+ public void count(CrawlDatum crawlDatum) {
+ // Only compute overdue time for unfetched records
+ if (crawlDatum.getStatus() == CrawlDatum.STATUS_DB_UNFETCHED) {
+ // Does this fetch time exceed overDueTime?
+
+ if (crawlDatum.getFetchTime() < overDueTime) {
+ numOverDue++;
+ }
+ }
+ }
+
+ /**
+ * Process the final host datum instance.
+ *
+ * @param hostDatum
+ */
+ @Override
+ public void finalize(HostDatum hostDatum) {
+ hostDatum.getMetaData().put(new Text("num.overdue"), new LongWritable(numOverDue));
+ }
+}
+
diff --git a/src/java/org/apache/nutch/hostdb/UpdateHostDb.java b/src/java/org/apache/nutch/hostdb/UpdateHostDb.java
index 60c9fa6b2..c903822f6 100644
--- a/src/java/org/apache/nutch/hostdb/UpdateHostDb.java
+++ b/src/java/org/apache/nutch/hostdb/UpdateHostDb.java
@@ -67,6 +67,7 @@ public class UpdateHostDb extends Configured implements Tool {
public static final String HOSTDB_NUMERIC_FIELDS = "hostdb.numeric.fields";
public static final String HOSTDB_STRING_FIELDS = "hostdb.string.fields";
public static final String HOSTDB_PERCENTILES = "hostdb.percentiles";
+ public static final String HOSTDB_CRAWLDATUM_PROCESSORS = "hostdb.crawldatum.processors";
private void updateHostDb(Path hostDb, Path crawlDb, Path topHosts,
boolean checkFailed, boolean checkNew, boolean checkKnown,
diff --git a/src/java/org/apache/nutch/hostdb/UpdateHostDbReducer.java b/src/java/org/apache/nutch/hostdb/UpdateHostDbReducer.java
index a5dd11e6f..1149f3054 100644
--- a/src/java/org/apache/nutch/hostdb/UpdateHostDbReducer.java
+++ b/src/java/org/apache/nutch/hostdb/UpdateHostDbReducer.java
@@ -65,6 +65,7 @@ public class UpdateHostDbReducer
protected static int[] percentiles;
protected static Text[] numericFieldWritables;
protected static Text[] stringFieldWritables;
+ protected static AbstractCrawlDatumProcessor[] crawlDatumProcessors;
protected BlockingQueue<Runnable> queue = new SynchronousQueue<>();
protected ThreadPoolExecutor executor = null;
@@ -85,6 +86,27 @@ public class UpdateHostDbReducer
numericFields = conf.getStrings(UpdateHostDb.HOSTDB_NUMERIC_FIELDS);
stringFields = conf.getStrings(UpdateHostDb.HOSTDB_STRING_FIELDS);
percentiles = conf.getInts(UpdateHostDb.HOSTDB_PERCENTILES);
+ String[] crawlDatumProcessorClassnames = conf.getStrings(UpdateHostDb.HOSTDB_CRAWLDATUM_PROCESSORS, null);
+
+ // Initialize classes for each CrawlDatumProcessor's class name
+ if (crawlDatumProcessorClassnames != null) {
+ crawlDatumProcessors = new AbstractCrawlDatumProcessor[crawlDatumProcessorClassnames.length];
+
+ for (int i = 0; i < crawlDatumProcessorClassnames.length; i++) {
+ // Get the class
+ try {
+ Class<? extends AbstractCrawlDatumProcessor> processorClass = Class.forName(crawlDatumProcessorClassnames[i]).asSubclass(AbstractCrawlDatumProcessor.class);
+
+ // Create an instance
+ AbstractCrawlDatumProcessor processorImpl = processorClass.getConstructor(Configuration.class).newInstance(conf);
+
+ // Add to array
+ crawlDatumProcessors[i] = processorImpl;
+ } catch (Exception e) {
+ LOG.error("Unable to instantiate crawldatum processor: " + crawlDatumProcessorClassnames[i] + " because: " + e.getMessage(), e);
+ }
+ }
+ }
// What fields do we need to collect metadata from
if (numericFields != null) {
@@ -258,6 +280,13 @@ public class UpdateHostDbReducer
}
}
}
+
+ // Run count phase for optional custom crawldatum processors
+ if (crawlDatumProcessors != null) {
+ for (AbstractCrawlDatumProcessor processor : crawlDatumProcessors) {
+ processor.count(buffer);
+ }
+ }
}
//
@@ -351,6 +380,13 @@ public class UpdateHostDbReducer
context.getCounter("UpdateHostDb", "skipped_not_eligible").increment(1);
LOG.info("UpdateHostDb: {}: skipped_not_eligible", key);
}
+
+ // Run finalize phase for optional custom crawldatum processors
+ if (crawlDatumProcessors != null) {
+ for (AbstractCrawlDatumProcessor processor : crawlDatumProcessors) {
+ processor.finalize(hostDatum);
+ }
+ }
// Write the host datum if it wasn't written by the resolver thread
context.write(key, hostDatum);