Apache Nutch is an extensible and scalable web crawler
NUTCH-716 - subcollections
Julien Nioche7cbcf9f3f132
diff --git a/CHANGES.txt b/CHANGES.txtindex 6c7fed08d..07900e0bd 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogRelease 2.0 - Current Development+* NUTCH-716 Make subcollection index filed multivalued (Dmitry Lihachev via jnioche)+* NUTCH-884 FetcherJob should run more reduce tasks than default (ab)* NUTCH-883 Remove unused parameters from nutch-default.xml (jnioche)diff --git a/src/plugin/subcollection/src/java/org/apache/nutch/collection/CollectionManager.java b/src/plugin/subcollection/src/java/org/apache/nutch/collection/CollectionManager.javaindex f2d203abb..b7a1bd67e 100644--- a/src/plugin/subcollection/src/java/org/apache/nutch/collection/CollectionManager.java+++ b/src/plugin/subcollection/src/java/org/apache/nutch/collection/CollectionManager.java@@ -22,9 +22,12 @@ import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;+import java.util.ArrayList;+import java.util.Arrays;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;+import java.util.List;import java.util.Map;import org.slf4j.Logger;@@ -43,196 +46,199 @@ import org.w3c.dom.NodeList;public class CollectionManager extends Configured {- public static final String DEFAULT_FILE_NAME = "subcollections.xml";-- static final Logger LOG = LoggerFactory.getLogger(CollectionManager.class);-- transient Map collectionMap = new HashMap();-- transient URL configfile;-- public CollectionManager(Configuration conf) {- super(conf);- init();- }-- /**- * Used for testing- */- protected CollectionManager(){- super(NutchConfiguration.create());- }-- protected void init(){- try {- if (LOG.isInfoEnabled()) { LOG.info("initializing CollectionManager"); }- // initialize known subcollections- configfile = getConf().getResource(- getConf().get("subcollections.config", DEFAULT_FILE_NAME));-- InputStream input = getConf().getConfResourceAsInputStream(- getConf().get("subcollections.config", DEFAULT_FILE_NAME));- parse(input);- } catch (Exception e) {- if (LOG.isWarnEnabled()) {- LOG.warn("Error occured:" + e);- e.printStackTrace(LogUtil.getWarnStream(LOG));- }- }- }-- protected void parse(InputStream input) {- Element collections = DomUtil.getDom(input);-- if (collections != null) {- NodeList nodeList = collections- .getElementsByTagName(Subcollection.TAG_COLLECTION);-- if (LOG.isInfoEnabled()) {- LOG.info("file has" + nodeList.getLength() + " elements");- }-- for (int i = 0; i < nodeList.getLength(); i++) {- Element scElem = (Element) nodeList.item(i);- Subcollection subCol = new Subcollection(getConf());- subCol.initialize(scElem);- collectionMap.put(subCol.name, subCol);- }- } else if (LOG.isInfoEnabled()) {- LOG.info("Cannot find collections");- }- }-- public static CollectionManager getCollectionManager(Configuration conf) {- String key = "collectionmanager";- ObjectCache objectCache = ObjectCache.get(conf);- CollectionManager impl = (CollectionManager)objectCache.getObject(key);- if (impl == null) {- try {- if (LOG.isInfoEnabled()) {- LOG.info("Instantiating CollectionManager");- }- impl=new CollectionManager(conf);- objectCache.setObject(key,impl);- } catch (Exception e) {- throw new RuntimeException("Couldn't create CollectionManager",e);- }- }- return impl;- }-- /**- * Returns named subcollection- *- * @param id- * @return Named SubCollection (or null if not existing)- */- public Subcollection getSubColection(final String id) {- return (Subcollection) collectionMap.get(id);- }-- /**- * Delete named subcollection- *- * @param id- * Id of SubCollection to delete- */- public void deleteSubCollection(final String id) throws IOException {- final Subcollection subCol = getSubColection(id);- if (subCol != null) {- collectionMap.remove(id);- }- }-- /**- * Create a new subcollection.- *- * @param name- * Name of SubCollection to create- * @return Created SubCollection or null if allready existed- */- public Subcollection createSubCollection(final String id, final String name) {- Subcollection subCol = null;-- if (!collectionMap.containsKey(id)) {- subCol = new Subcollection(id, name, getConf());- collectionMap.put(id, subCol);- }-- return subCol;- }-- /**- * Return names of collections url is part of- *- * @param url- * The url to test against Collections- * @return Space delimited string of collection names url is part of- */- public String getSubCollections(final String url) {- StringBuilder collections = new StringBuilder();- final Iterator iterator = collectionMap.values().iterator();-- while (iterator.hasNext()) {- final Subcollection subCol = (Subcollection) iterator.next();- if (subCol.filter(url) != null) {- if (collections.length() > 0) {- collections.append(' ');- }- collections.append(subCol.name);- }- }- if (LOG.isTraceEnabled()) { LOG.trace("subcollections:" + collections); }-- return collections.toString();- }-- /**- * Returns all collections- *- * @return All collections CollectionManager knows about- */- public Collection getAll() {- return collectionMap.values();- }-- /**- * Save collections into file- *- * @throws Exception- */- public void save() throws IOException {- try {- final FileOutputStream fos = new FileOutputStream(new File(configfile- .getFile()));- final Document doc = new DocumentImpl();- final Element collections = doc- .createElement(Subcollection.TAG_COLLECTIONS);- final Iterator iterator = collectionMap.values().iterator();-- while (iterator.hasNext()) {- final Subcollection subCol = (Subcollection) iterator.next();- final Element collection = doc- .createElement(Subcollection.TAG_COLLECTION);- collections.appendChild(collection);- final Element name = doc.createElement(Subcollection.TAG_NAME);- name.setNodeValue(subCol.getName());- collection.appendChild(name);- final Element whiteList = doc- .createElement(Subcollection.TAG_WHITELIST);- whiteList.setNodeValue(subCol.getWhiteListString());- collection.appendChild(whiteList);- final Element blackList = doc- .createElement(Subcollection.TAG_BLACKLIST);- blackList.setNodeValue(subCol.getBlackListString());- collection.appendChild(blackList);- }-- DomUtil.saveDom(fos, collections);- fos.flush();- fos.close();- } catch (FileNotFoundException e) {- throw new IOException(e.toString());- }- }+ public static final String DEFAULT_FILE_NAME = "subcollections.xml";++ static final Logger LOG = LoggerFactory.getLogger(CollectionManager.class);++ transient Map collectionMap = new HashMap();++ transient URL configfile;++ public CollectionManager(Configuration conf) {+ super(conf);+ init();+ }++ /**+ * Used for testing+ */+ protected CollectionManager() {+ super(NutchConfiguration.create());+ }++ protected void init() {+ try {+ if (LOG.isInfoEnabled()) {+ LOG.info("initializing CollectionManager");+ }+ // initialize known subcollections+ configfile = getConf().getResource(+ getConf().get("subcollections.config", DEFAULT_FILE_NAME));++ InputStream input = getConf().getConfResourceAsInputStream(+ getConf().get("subcollections.config", DEFAULT_FILE_NAME));+ parse(input);+ } catch (Exception e) {+ if (LOG.isWarnEnabled()) {+ LOG.warn("Error occured:" + e);+ e.printStackTrace(LogUtil.getWarnStream(LOG));+ }+ }+ }++ protected void parse(InputStream input) {+ Element collections = DomUtil.getDom(input);++ if (collections != null) {+ NodeList nodeList = collections+ .getElementsByTagName(Subcollection.TAG_COLLECTION);++ if (LOG.isInfoEnabled()) {+ LOG.info("file has" + nodeList.getLength() + " elements");+ }++ for (int i = 0; i < nodeList.getLength(); i++) {+ Element scElem = (Element) nodeList.item(i);+ Subcollection subCol = new Subcollection(getConf());+ subCol.initialize(scElem);+ collectionMap.put(subCol.name, subCol);+ }+ } else if (LOG.isInfoEnabled()) {+ LOG.info("Cannot find collections");+ }+ }++ public static CollectionManager getCollectionManager(Configuration conf) {+ String key = "collectionmanager";+ ObjectCache objectCache = ObjectCache.get(conf);+ CollectionManager impl = (CollectionManager) objectCache.getObject(key);+ if (impl == null) {+ try {+ if (LOG.isInfoEnabled()) {+ LOG.info("Instantiating CollectionManager");+ }+ impl = new CollectionManager(conf);+ objectCache.setObject(key, impl);+ } catch (Exception e) {+ throw new RuntimeException("Couldn't create CollectionManager",+ e);+ }+ }+ return impl;+ }++ /**+ * Returns named subcollection+ *+ * @param id+ * @return Named SubCollection (or null if not existing)+ */+ public Subcollection getSubColection(final String id) {+ return (Subcollection) collectionMap.get(id);+ }++ /**+ * Delete named subcollection+ *+ * @param id+ * Id of SubCollection to delete+ */+ public void deleteSubCollection(final String id) throws IOException {+ final Subcollection subCol = getSubColection(id);+ if (subCol != null) {+ collectionMap.remove(id);+ }+ }++ /**+ * Create a new subcollection.+ *+ * @param name+ * Name of SubCollection to create+ * @return Created SubCollection or null if allready existed+ */+ public Subcollection createSubCollection(final String id, final String name) {+ Subcollection subCol = null;++ if (!collectionMap.containsKey(id)) {+ subCol = new Subcollection(id, name, getConf());+ collectionMap.put(id, subCol);+ }++ return subCol;+ }++ /**+ * Return names of collections url is part of+ *+ * @param url+ * The url to test against Collections+ * @return Space delimited string of collection names url is part of+ */+ public List<String> getSubCollections(final String url) {+ List<String> collections = new ArrayList<String>();+ final Iterator iterator = collectionMap.values().iterator();++ while (iterator.hasNext()) {+ final Subcollection subCol = (Subcollection) iterator.next();+ if (subCol.filter(url) != null) {+ collections.add(subCol.name);+ }+ }+ if (LOG.isTraceEnabled()) {+ LOG.trace("subcollections:"+ + Arrays.toString(collections.toArray()));+ }++ return collections;+ }++ /**+ * Returns all collections+ *+ * @return All collections CollectionManager knows about+ */+ public Collection getAll() {+ return collectionMap.values();+ }++ /**+ * Save collections into file+ *+ * @throws Exception+ */+ public void save() throws IOException {+ try {+ final FileOutputStream fos = new FileOutputStream(new File(+ configfile.getFile()));+ final Document doc = new DocumentImpl();+ final Element collections = doc+ .createElement(Subcollection.TAG_COLLECTIONS);+ final Iterator iterator = collectionMap.values().iterator();++ while (iterator.hasNext()) {+ final Subcollection subCol = (Subcollection) iterator.next();+ final Element collection = doc+ .createElement(Subcollection.TAG_COLLECTION);+ collections.appendChild(collection);+ final Element name = doc.createElement(Subcollection.TAG_NAME);+ name.setNodeValue(subCol.getName());+ collection.appendChild(name);+ final Element whiteList = doc+ .createElement(Subcollection.TAG_WHITELIST);+ whiteList.setNodeValue(subCol.getWhiteListString());+ collection.appendChild(whiteList);+ final Element blackList = doc+ .createElement(Subcollection.TAG_BLACKLIST);+ blackList.setNodeValue(subCol.getBlackListString());+ collection.appendChild(blackList);+ }++ DomUtil.saveDom(fos, collections);+ fos.flush();+ fos.close();+ } catch (FileNotFoundException e) {+ throw new IOException(e.toString());+ }+ }}diff --git a/src/plugin/subcollection/src/java/org/apache/nutch/indexer/subcollection/SubcollectionIndexingFilter.java b/src/plugin/subcollection/src/java/org/apache/nutch/indexer/subcollection/SubcollectionIndexingFilter.javaindex 70da7b8d7..8ec4f59b0 100644--- a/src/plugin/subcollection/src/java/org/apache/nutch/indexer/subcollection/SubcollectionIndexingFilter.java+++ b/src/plugin/subcollection/src/java/org/apache/nutch/indexer/subcollection/SubcollectionIndexingFilter.java@@ -60,9 +60,9 @@ public class SubcollectionIndexingFilter extends Configured implements* @param url*/private void addSubCollectionField(NutchDocument doc, String url) {- String collname = CollectionManager.getCollectionManager(getConf())- .getSubCollections(url);- doc.add(FIELD_NAME, collname);+ for (String collname: CollectionManager.getCollectionManager(getConf()).getSubCollections(url)) {+ doc.add(FIELD_NAME, collname);+ }}@Override