Apache Nutch is an extensible and scalable web crawler
- fix for NUTCH-677 Segment merge filtering based on segment content
Chris Mattmanncac66cbbded7
diff --git a/CHANGES.txt b/CHANGES.txtindex a6a5f96c7..6cecf632d 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogRelease 2.0 - Current Development+* NUTCH-677 Segment merge filtering based on segment content (Marcin Okraszewski via mattmann)+* NUTCH-774 Retry interval in crawl date is set to 0 (Reinhard Schwab via mattmann)* NUTCH-697 Generate log output for solr indexer and dedup (Dmitry Lihachev, Jeroen van Vianen via mattmann)diff --git a/src/java/org/apache/nutch/segment/SegmentMergeFilter.java b/src/java/org/apache/nutch/segment/SegmentMergeFilter.javanew file mode 100644index 000000000..7e8caea44--- /dev/null+++ b/src/java/org/apache/nutch/segment/SegmentMergeFilter.java@@ -0,0 +1,47 @@+/*+ * 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.segment;++import java.util.Collection;++import org.apache.hadoop.io.WritableComparable;+import org.apache.nutch.crawl.CrawlDatum;+import org.apache.nutch.parse.ParseData;+import org.apache.nutch.parse.ParseText;+import org.apache.nutch.protocol.Content;++/**+ * Interface used to filter segments during segment merge. It allows filtering+ * on more sophisticated criteria than just URLs. In particular it allows+ * filtering based on metadata collected while parsing page.+ *+ */+public interface SegmentMergeFilter {+ /** The name of the extension point. */+ public final static String X_POINT_ID = SegmentMergeFilter.class.getName();++ /**+ * The filtering method which gets all information being merged for a given+ * key (URL).+ *+ * @return <tt>true</tt> values for this <tt>key</tt> (URL) should be merged+ * into the new segment.+ */+ public boolean filter(WritableComparable key, CrawlDatum generateData,+ CrawlDatum fetchData, CrawlDatum sigData, Content content,+ ParseData parseData, ParseText parseText, Collection<CrawlDatum> linked);+}diff --git a/src/java/org/apache/nutch/segment/SegmentMergeFilters.java b/src/java/org/apache/nutch/segment/SegmentMergeFilters.javanew file mode 100644index 000000000..2ef18581f--- /dev/null+++ b/src/java/org/apache/nutch/segment/SegmentMergeFilters.java@@ -0,0 +1,85 @@+/*+ * 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.segment;++import java.util.Collection;++import org.apache.commons.logging.Log;+import org.apache.commons.logging.LogFactory;+import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.io.WritableComparable;+import org.apache.nutch.crawl.CrawlDatum;+import org.apache.nutch.net.URLFilter;+import org.apache.nutch.parse.ParseData;+import org.apache.nutch.parse.ParseText;+import org.apache.nutch.plugin.Extension;+import org.apache.nutch.plugin.ExtensionPoint;+import org.apache.nutch.plugin.PluginRepository;+import org.apache.nutch.plugin.PluginRuntimeException;+import org.apache.nutch.protocol.Content;++/**+ * This class wraps all {@link SegmentMergeFilter} extensions in a single object+ * so it is easier to operate on them. If any of extensions returns+ * <tt>false</tt> this one will return <tt>false</tt> as well.+ *+ */+public class SegmentMergeFilters {+ private static final Log LOG = LogFactory.getLog(SegmentMergeFilters.class);+ private SegmentMergeFilter[] filters;++ public SegmentMergeFilters(Configuration conf) {+ try {+ ExtensionPoint point = PluginRepository.get(conf).getExtensionPoint(+ SegmentMergeFilter.X_POINT_ID);+ if (point == null)+ throw new RuntimeException(URLFilter.X_POINT_ID + " not found.");+ Extension[] extensions = point.getExtensions();+ filters = new SegmentMergeFilter[extensions.length];+ for (int i = 0; i < extensions.length; i++) {+ filters[i] = (SegmentMergeFilter) extensions[i].getExtensionInstance();+ }+ } catch (PluginRuntimeException e) {+ throw new RuntimeException(e);+ }+ }++ /**+ * Iterates over all {@link SegmentMergeFilter} extensions and if any of them+ * returns false, it will return false as well.+ *+ * @return <tt>true</tt> values for this <tt>key</tt> (URL) should be merged+ * into the new segment.+ */+ public boolean filter(WritableComparable key, CrawlDatum generateData,+ CrawlDatum fetchData, CrawlDatum sigData, Content content,+ ParseData parseData, ParseText parseText, Collection<CrawlDatum> linked) {+ for (SegmentMergeFilter filter : filters) {+ if (!filter.filter(key, generateData, fetchData, sigData, content,+ parseData, parseText, linked)) {+ if (LOG.isTraceEnabled())+ LOG+ .trace("Key " + key + " dropped by "+ + filter.getClass().getName());+ return false;+ }+ }+ if (LOG.isTraceEnabled())+ LOG.trace("Key " + key + " accepted for merge.");+ return true;+ }+}diff --git a/src/java/org/apache/nutch/segment/SegmentMerger.java b/src/java/org/apache/nutch/segment/SegmentMerger.javaindex 1c033897b..ae322bda1 100644--- a/src/java/org/apache/nutch/segment/SegmentMerger.java+++ b/src/java/org/apache/nutch/segment/SegmentMerger.java@@ -120,6 +120,7 @@ public class SegmentMerger extends Configured implementsprivate URLFilters filters = null;private URLNormalizers normalizers = null;+ private SegmentMergeFilters mergeFilters = null;private long sliceSize = -1;private long curCount = 0;@@ -312,8 +313,10 @@ public class SegmentMerger extends Configured implementspublic void setConf(Configuration conf) {super.setConf(conf);if (conf == null) return;- if (conf.getBoolean("segment.merger.filter", false))+ if (conf.getBoolean("segment.merger.filter", false)) {filters = new URLFilters(conf);+ mergeFilters = new SegmentMergeFilters(conf);+ }if (conf.getBoolean("segment.merger.normalizer", false))normalizers = new URLNormalizers(conf, URLNormalizers.SCOPE_DEFAULT);sliceSize = conf.getLong("segment.merger.slice", -1);@@ -470,6 +473,13 @@ public class SegmentMerger extends Configured implements}}}+ // perform filtering based on full merge record+ if (mergeFilters != null &&+ !mergeFilters.filter(key, lastG, lastF, lastSig, lastC, lastPD, lastPT,+ linked.isEmpty() ? null : linked.lastEntry().getValue())){+ return;+ }+curCount++;String sliceName = null;MetaWrapper wrapper = new MetaWrapper();diff --git a/src/plugin/nutch-extensionpoints/plugin.xml b/src/plugin/nutch-extensionpoints/plugin.xmlindex 7b8aa67de..f9d5f9275 100644--- a/src/plugin/nutch-extensionpoints/plugin.xml+++ b/src/plugin/nutch-extensionpoints/plugin.xml@@ -52,4 +52,8 @@id="org.apache.nutch.scoring.ScoringFilter"name="Nutch Scoring"/>+<extension-point+ id="org.apache.nutch.segment.SegmentMergeFilter"+ name="Nutch Segment Merge Filter"/>+</plugin>