Apache Nutch is an extensible and scalable web crawler
NUTCH-1351 DomainStatistics to aggregate by TLD
Markus Jelsmaa0decd20dfb6
diff --git a/CHANGES.txt b/CHANGES.txtindex abef77c60..a7040074a 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change Log(trunk) Current Development:+* NUTCH-1351 DomainStatistics to aggregate by TLD (markus)+* NUTCH-1381 Allow to override default subcollection field name (markus)* NUTCH-XX Commit to add configuration for separation of ant distribution targets (lewismc + jnioche)diff --git a/src/java/org/apache/nutch/util/URLUtil.java b/src/java/org/apache/nutch/util/URLUtil.javaindex e2f31180e..7217f0f9d 100644--- a/src/java/org/apache/nutch/util/URLUtil.java+++ b/src/java/org/apache/nutch/util/URLUtil.java@@ -18,7 +18,7 @@package org.apache.nutch.util;import java.net.MalformedURLException;-import java.net.URL;+import java.net.*;import java.util.regex.Pattern;import org.apache.nutch.util.domain.DomainSuffix;@@ -172,6 +172,36 @@ public class URLUtil {return getDomainName(new URL(url));}+ /** Returns the top level domain name of the url. The top level domain name+ * of a url is the substring of the url's hostname, w/o subdomain names.+ * As an example <br><code>+ * getTopLevelDomainName(conf, new http://lucene.apache.org/)+ * </code><br>+ * will return <br><code> org</code>+ * @throws MalformedURLException+ */+ public static String getTopLevelDomainName(URL url) throws MalformedURLException {+ String suffix = getDomainSuffix(url).toString();+ int idx = suffix.lastIndexOf(".");+ if (idx != -1) {+ return suffix.substring(idx + 1);+ } else {+ return suffix;+ }+ }++ /** Returns the top level domain name of the url. The top level domain name+ * of a url is the substring of the url's hostname, w/o subdomain names.+ * As an example <br><code>+ * getTopLevelDomainName(conf, new http://lucene.apache.org/)+ * </code><br>+ * will return <br><code> org</code>+ * @throws MalformedURLException+ */+ public static String getTopLevelDomainName(String url) throws MalformedURLException {+ return getTopLevelDomainName(new URL(url));+ }+/** Returns whether the given urls have the same domain name.* As an example, <br>* <code> isSameDomain(new URL("http://lucene.apache.org")@@ -434,7 +464,7 @@ public class URLUtil {return null;}}-+/** For testing */public static void main(String[] args){diff --git a/src/java/org/apache/nutch/util/domain/DomainStatistics.java b/src/java/org/apache/nutch/util/domain/DomainStatistics.javaindex 0f6f7ade2..d4afd8a3c 100644--- a/src/java/org/apache/nutch/util/domain/DomainStatistics.java+++ b/src/java/org/apache/nutch/util/domain/DomainStatistics.java@@ -58,12 +58,13 @@ public class DomainStatistics extends Configured implements Tool {private static final int MODE_HOST = 1;private static final int MODE_DOMAIN = 2;private static final int MODE_SUFFIX = 3;+ private static final int MODE_TLD = 4;private int mode = 0;public int run(String[] args) throws Exception {if (args.length < 3) {- System.out.println("usage: DomainStatistics inputDirs outDir host|domain|suffix [numOfReducer]");+ System.out.println("usage: DomainStatistics inputDirs outDir host|domain|suffix|tld [numOfReducer]");return 1;}String inputDir = args[0];@@ -89,6 +90,9 @@ public class DomainStatistics extends Configured implements Tool {} else if(args[2].equals("suffix")) {jobName = "Suffix statistics";mode = MODE_SUFFIX;+ } else if(args[2].equals("tld")) {+ jobName = "TLD statistics";+ mode = MODE_TLD;}Configuration conf = getConf();@@ -153,6 +157,9 @@ public class DomainStatistics extends Configured implements Tool {case MODE_SUFFIX:out = URLUtil.getDomainSuffix(url).getDomain();break;+ case MODE_TLD:+ out = URLUtil.getTopLevelDomainName(url);+ break;}if(out.trim().equals("")) {LOG.info("url : " + url);