Apache Nutch is an extensible and scalable web crawler
NUTCH-2950 Improve performance of UpdateHostDb - simplify map function: - remove instanceof conditions for key (it's an instance of Text by method signature) - avoid parsing the URL string multiple times
Sebastian Nagel417dee6d1ef6
diff --git a/src/java/org/apache/nutch/hostdb/UpdateHostDbMapper.java b/src/java/org/apache/nutch/hostdb/UpdateHostDbMapper.javaindex ecd225637..b8abac340 100644--- a/src/java/org/apache/nutch/hostdb/UpdateHostDbMapper.java+++ b/src/java/org/apache/nutch/hostdb/UpdateHostDbMapper.java@@ -18,6 +18,8 @@ package org.apache.nutch.hostdb;import java.io.IOException;import java.lang.invoke.MethodHandles;+import java.net.MalformedURLException;+import java.net.URL;import org.apache.hadoop.io.FloatWritable;import org.apache.hadoop.io.Text;@@ -128,8 +130,16 @@ public class UpdateHostDbMapperString keyStr = key.toString();// Check if we process records from the CrawlDB- if (key instanceof Text && value instanceof CrawlDatum) {- String hostName = URLUtil.getHost(keyStr);+ if (value instanceof CrawlDatum) {++ URL url;+ try {+ url = new URL(keyStr);+ } catch (MalformedURLException e) {+ context.getCounter("UpdateHostDb", "malformed_url").increment(1);+ return;+ }+ String hostName = URLUtil.getHost(url);// Get the normalized and filtered host of this URLbuffer = filterNormalize(hostName);@@ -143,7 +153,7 @@ public class UpdateHostDbMapper// Set the host of this URLhost.set(buffer);- crawlDatum = (CrawlDatum)value;+ crawlDatum = (CrawlDatum) value;hostDatum = new HostDatum();/**@@ -161,7 +171,7 @@ public class UpdateHostDbMapper// Do not resolve homepages when the root URL is unfetchedif (crawlDatum.getStatus() != CrawlDatum.STATUS_DB_UNFETCHED) {// Get the protocol- String protocol = URLUtil.getProtocol(keyStr);+ String protocol = URLUtil.getProtocol(url);// Get the proposed homepage URLString homepage = protocol + "://" + buffer + "/";@@ -173,8 +183,8 @@ public class UpdateHostDbMappercrawlDatum.getStatus() == CrawlDatum.STATUS_DB_REDIR_TEMP) {// Obtain the repr url for this redirect via protocolstatus from the metadata- ProtocolStatus z = (ProtocolStatus)crawlDatum.getMetaData().- get(Nutch.WRITABLE_PROTO_STATUS_KEY);+ ProtocolStatus z = (ProtocolStatus) crawlDatum.getMetaData()+ .get(Nutch.WRITABLE_PROTO_STATUS_KEY);// Get the protocol status' argumentsargs = z.getArgs();@@ -201,10 +211,9 @@ public class UpdateHostDbMapper// Always emit crawl datumcontext.write(host, new NutchWritable(crawlDatum));- }- // Check if we got a record from the hostdb- if (key instanceof Text && value instanceof HostDatum) {+ } else if (value instanceof HostDatum) {+ // we got a record from the hostdbbuffer = filterNormalize(keyStr);// Filtered out?@@ -225,10 +234,10 @@ public class UpdateHostDbMapper}context.write(key, new NutchWritable(hostDatum));- }- // Check if we got a record with host scores- if (key instanceof Text && value instanceof Text) {+ } else if (value instanceof Text) {+ // a record with host scores+buffer = filterNormalize(keyStr);// Filtered out?diff --git a/src/java/org/apache/nutch/util/URLUtil.java b/src/java/org/apache/nutch/util/URLUtil.javaindex dca911abe..cebf1b499 100644--- a/src/java/org/apache/nutch/util/URLUtil.java+++ b/src/java/org/apache/nutch/util/URLUtil.java@@ -422,21 +422,28 @@ public class URLUtil {}/**- * Returns the lowercased hostname for the url or null if the url is not well+ * Returns the lowercased hostname for the URL or null if the URL is not well-formed* formed.** @param url- * The url to check.- * @return String The hostname for the url.+ * The URL to check.+ * @return String the hostname for the URL.*/public static String getHost(String url) {try {- return new URL(url).getHost().toLowerCase(Locale.ROOT);+ return getHost(new URL(url));} catch (MalformedURLException e) {return null;}}+ /**+ * Returns the lowercased hostname for the URL.+ */+ public static String getHost(URL url) {+ return url.getHost().toLowerCase(Locale.ROOT);+ }+/*** Returns the page for the url. The page consists of the protocol, host, and* path, but does not include the query string. The host is lowercased but the