Apache Nutch is an extensible and scalable web crawler
Fixes for NUTCH-2333: Added the logic for indexing process
r0ann3l62496aec84cb
diff --git a/src/plugin/indexer-rabbit/build-ivy.xml b/src/plugin/indexer-rabbit/build-ivy.xmlnew file mode 100644index 000000000..74548a8b0--- /dev/null+++ b/src/plugin/indexer-rabbit/build-ivy.xml@@ -0,0 +1,54 @@+<?xml version="1.0"?>+<!--+ 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.+-->+<project name="indexer-rabbit" default="deps-jar" xmlns:ivy="antlib:org.apache.ivy.ant">++ <property name="ivy.install.version" value="2.1.0" />+ <condition property="ivy.home" value="${env.IVY_HOME}">+ <isset property="env.IVY_HOME" />+ </condition>+ <property name="ivy.home" value="${user.home}/.ant" />+ <property name="ivy.checksums" value="" />+ <property name="ivy.jar.dir" value="${ivy.home}/lib" />+ <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />++ <target name="download-ivy" unless="offline">++ <mkdir dir="${ivy.jar.dir}"/>+ <!-- download Ivy from web site so that it can be used even without any special installation -->+ <get src="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"+ dest="${ivy.jar.file}" usetimestamp="true"/>+ </target>++ <target name="init-ivy" depends="download-ivy">+ <!-- try to load ivy here from ivy home, in case the user has not already dropped+ it into ant's lib dir (note that the latter copy will always take precedence).+ We will not fail as long as local lib dir exists (it may be empty) and+ ivy is in at least one of ant's lib dir or the local lib dir. -->+ <path id="ivy.lib.path">+ <fileset dir="${ivy.jar.dir}" includes="*.jar"/>++ </path>+ <taskdef resource="org/apache/ivy/ant/antlib.xml"+ uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>+ </target>++ <target name="deps-jar" depends="init-ivy">+ <ivy:retrieve pattern="lib/[artifact]-[revision].[ext]"/>+ </target>++</project>diff --git a/src/plugin/indexer-rabbit/build.xml b/src/plugin/indexer-rabbit/build.xmlnew file mode 100644index 000000000..e996becc1--- /dev/null+++ b/src/plugin/indexer-rabbit/build.xml@@ -0,0 +1,22 @@+<?xml version="1.0"?>+<!--+ 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.+-->+<project name="indexer-rabbit" default="jar-core">++ <import file="../build-plugin.xml" />++</project>diff --git a/src/plugin/indexer-rabbit/ivy.xml b/src/plugin/indexer-rabbit/ivy.xmlnew file mode 100644index 000000000..0954a3d47--- /dev/null+++ b/src/plugin/indexer-rabbit/ivy.xml@@ -0,0 +1,43 @@+<?xml version="1.0" ?>++<!--+ 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.+-->++<ivy-module version="1.0">+ <info organisation="org.apache.nutch" module="${ant.project.name}">+ <license name="Apache 2.0"/>+ <ivyauthor name="Apache Nutch Team" url="http://nutch.apache.org"/>+ <description>+ Apache Nutch+ </description>+ </info>++ <configurations>+ <include file="../../..//ivy/ivy-configurations.xml"/>+ </configurations>++ <publications>+ <!--get the artifact from our module name-->+ <artifact conf="master"/>+ </publications>++ <dependencies>+ <dependency org="com.rabbitmq" name="amqp-client" rev="3.6.5"/>+ <dependency org="com.google.code.gson" name="gson" rev="2.7"/>+ </dependencies>++</ivy-module>diff --git a/src/plugin/indexer-rabbit/plugin.xml b/src/plugin/indexer-rabbit/plugin.xmlnew file mode 100644index 000000000..051d34b33--- /dev/null+++ b/src/plugin/indexer-rabbit/plugin.xml@@ -0,0 +1,39 @@+<?xml version="1.0" encoding="UTF-8"?>+<!--+ 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.+-->+<plugin id="indexer-rabbit" name="RabbitIndexWriter" version="1.0.0" provider-name="nutch.apache.org">++ <runtime>+ <library name="indexer-rabbit.jar">+ <export name="*" />+ </library>+ <library name="amqp-client-3.6.5.jar"/>+ <library name="gson-2.7.jar"/>+ </runtime>++ <requires>+ <import plugin="nutch-extensionpoints" />+ </requires>++ <extension id="org.apache.nutch.indexer.rabbit"+ name="Rabbit Index Writer"+ point="org.apache.nutch.indexer.IndexWriter">+ <implementation id="RabbitIndexWriter"+ class="org.apache.nutch.indexwriter.rabbit.RabbitIndexWriter" />+ </extension>++</plugin>diff --git a/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitDocument.java b/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitDocument.javanew file mode 100644index 000000000..238f0171a--- /dev/null+++ b/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitDocument.java@@ -0,0 +1,54 @@+/*+ * 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.indexwriter.rabbit;++import java.util.LinkedList;+import java.util.List;++class RabbitDocument {+ private List<RabbitDocumentField> fields;++ private float documentBoost;++ RabbitDocument() {+ this.fields = new LinkedList<>();+ }++ List<RabbitDocumentField> getFields() {+ return fields;+ }++ void setDocumentBoost(float documentBoost) {+ this.documentBoost = documentBoost;+ }++ void addField(RabbitDocumentField field) {+ fields.add(field);+ }++ static class RabbitDocumentField {+ private String key;+ private float weight;+ private List<Object> values;++ RabbitDocumentField(String key, float weight, List<Object> values) {+ this.key = key;+ this.weight = weight;+ this.values = values;+ }+ }+}diff --git a/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitIndexWriter.java b/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitIndexWriter.javanew file mode 100644index 000000000..17ce1b59c--- /dev/null+++ b/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitIndexWriter.java@@ -0,0 +1,194 @@+/*+ * 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.indexwriter.rabbit;++import java.io.IOException;++import com.rabbitmq.client.Channel;+import com.rabbitmq.client.Connection;+import com.rabbitmq.client.ConnectionFactory;+import org.apache.hadoop.conf.Configuration;+import org.apache.nutch.indexer.NutchDocument;++import org.apache.hadoop.mapred.JobConf;+import org.apache.nutch.indexer.IndexWriter;++import org.apache.nutch.indexer.NutchField;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++import java.util.*;+import java.util.concurrent.TimeoutException;++public class RabbitIndexWriter implements IndexWriter {++ private String serverHost;+ private int serverPort;+ private String serverVirtualHost;+ private String serverUsername;+ private String serverPassword;++ private String exchangeServer;+ private String exchangeType;++ private String queueName;+ private boolean queueDurable;+ private String queueRoutingKey;++ private int commitSize;++ public static final Logger LOG = LoggerFactory.getLogger(RabbitIndexWriter.class);++ private Configuration config;++ private RabbitMessage rabbitMessage = new RabbitMessage();++ private Channel channel;+ private Connection connection;++ @Override+ public Configuration getConf() {+ return config;+ }++ @Override+ public void setConf(Configuration conf) {+ config = conf;++ serverHost = conf.get(RabbitMQConstants.SERVER_HOST, "localhost");+ serverPort = conf.getInt(RabbitMQConstants.SERVER_PORT, 15672);+ serverVirtualHost = conf.get(RabbitMQConstants.SERVER_VIRTUAL_HOST, null);++ serverUsername = conf.get(RabbitMQConstants.SERVER_USERNAME, "admin");+ serverPassword = conf.get(RabbitMQConstants.SERVER_PASSWORD, "admin");++ exchangeServer = conf.get(RabbitMQConstants.EXCHANGE_SERVER, "nutch.exchange");+ exchangeType = conf.get(RabbitMQConstants.EXCHANGE_TYPE, "direct");++ queueName = conf.get(RabbitMQConstants.QUEUE_NAME, "nutch.queue");+ queueDurable = conf.getBoolean(RabbitMQConstants.QUEUE_DURABLE, true);+ queueRoutingKey = conf.get(RabbitMQConstants.QUEUE_ROUTING_KEY, "nutch.key");++ commitSize = conf.getInt(RabbitMQConstants.COMMIT_SIZE, 250);+ }++ @Override+ public void open(JobConf JobConf, String name) throws IOException {+ ConnectionFactory factory = new ConnectionFactory();+ factory.setHost(serverHost);+ factory.setPort(serverPort);++ if(serverVirtualHost != null) {+ factory.setVirtualHost(serverVirtualHost);+ }++ factory.setUsername(serverUsername);+ factory.setPassword(serverPassword);++ try {+ connection = factory.newConnection();+ channel = connection.createChannel();++ channel.exchangeDeclare(exchangeServer, exchangeType, true);+ channel.queueDeclare(queueName, queueDurable, false, false, null);+ channel.queueBind(queueName, exchangeServer, queueRoutingKey);++ } catch (TimeoutException | IOException ex) {+ throw makeIOException(ex);+ }+ }++ @Override+ public void update(NutchDocument doc) throws IOException {+ RabbitDocument rabbitDocument = new RabbitDocument();++ for (final Map.Entry<String, NutchField> e : doc) {+ RabbitDocument.RabbitDocumentField field = new RabbitDocument.RabbitDocumentField(+ e.getKey(),+ e.getValue().getWeight(),+ e.getValue().getValues());+ rabbitDocument.addField(field);+ }+ rabbitDocument.setDocumentBoost(doc.getWeight());++ rabbitMessage.addDocToUpdate(rabbitDocument);+ if(rabbitMessage.size() >= commitSize) {+ commit();+ }+ }++ @Override+ public void commit() throws IOException {+ if (!rabbitMessage.isEmpty()) {+ channel.basicPublish(exchangeServer, queueRoutingKey, null, rabbitMessage.getBytes());+ }+ rabbitMessage.clear();+ }++ @Override+ public void write(NutchDocument doc) throws IOException {+ RabbitDocument rabbitDocument = new RabbitDocument();++ for (final Map.Entry<String, NutchField> e : doc) {+ RabbitDocument.RabbitDocumentField field = new RabbitDocument.RabbitDocumentField(+ e.getKey(),+ e.getValue().getWeight(),+ e.getValue().getValues());+ rabbitDocument.addField(field);+ }+ rabbitDocument.setDocumentBoost(doc.getWeight());++ rabbitMessage.addDocToWrite(rabbitDocument);++ if(rabbitMessage.size() >= commitSize) {+ commit();+ }+ }++ @Override+ public void close() throws IOException {+ commit();//TODO: This is because indexing job never call commit method. It should be fixed.+ try {+ channel.close();+ connection.close();+ } catch (IOException | TimeoutException e) {+ throw makeIOException(e);+ }+ }++ @Override+ public void delete(String url) throws IOException {+ rabbitMessage.addDocToDelete(url);++ if(rabbitMessage.size() >= commitSize) {+ commit();+ }+ }++ private static IOException makeIOException(Exception e) {+ return new IOException(e);+ }++ public String describe() {+ return "RabbitIndexWriter\n" ++ "\t" + RabbitMQConstants.SERVER_URL + " : URL of RabbitMQ server\n" ++ "\t" + RabbitMQConstants.SERVER_VIRTUAL_HOST + " : Virtualhost name\n" ++ "\t" + RabbitMQConstants.SERVER_USERNAME + " : Username for authentication\n" ++ "\t" + RabbitMQConstants.SERVER_PASSWORD + " : Password for authentication\n" ++ "\t" + RabbitMQConstants.COMMIT_SIZE + " : Buffer size when sending to RabbitMQ (default 250)\n";+ }+}diff --git a/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitMQConstants.java b/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitMQConstants.javanew file mode 100644index 000000000..1d58da6ab--- /dev/null+++ b/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitMQConstants.java@@ -0,0 +1,46 @@+/*+ * 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.indexwriter.rabbit;++interface RabbitMQConstants {+ String RABBIT_PREFIX = "rabbitmq.indexer";++ String SERVER_HOST = RABBIT_PREFIX + "server.host";++ String SERVER_PORT = RABBIT_PREFIX + "server.port";++ String SERVER_VIRTUAL_HOST = RABBIT_PREFIX + "server.virtualhost";++ String SERVER_USERNAME = RABBIT_PREFIX + "server.username";++ String SERVER_PASSWORD = RABBIT_PREFIX + "server.password";++ String EXCHANGE_SERVER = RABBIT_PREFIX + "exchange.server";++ String EXCHANGE_TYPE = RABBIT_PREFIX + "exchange.type";++ String QUEUE_NAME = RABBIT_PREFIX + "queue.name";++ String QUEUE_DURABLE = RABBIT_PREFIX + "queue.durable";++ String QUEUE_ROUTING_KEY = RABBIT_PREFIX + "queue.routingkey";+++ String COMMIT_SIZE = RABBIT_PREFIX + "commit.size";++ String SERVER_URL = SERVER_HOST + ":" + SERVER_PORT;+}diff --git a/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitMessage.java b/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitMessage.javanew file mode 100644index 000000000..ec76174f5--- /dev/null+++ b/src/plugin/indexer-rabbit/src/java/org/apache/nutch/indexwriter/rabbit/RabbitMessage.java@@ -0,0 +1,71 @@+/*+ * 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.indexwriter.rabbit;++import com.google.gson.Gson;++import java.util.LinkedList;+import java.util.List;++class RabbitMessage {+ private List<RabbitDocument> docsToWrite = new LinkedList<>();+ private List<RabbitDocument> docsToUpdate = new LinkedList<>();+ private List<String> docsToDelete = new LinkedList<>();++ boolean addDocToWrite (RabbitDocument doc) {+ return docsToWrite.add(doc);+ }++ boolean addDocToUpdate (RabbitDocument doc) {+ return docsToUpdate.add(doc);+ }++ boolean addDocToDelete (String url) {+ return docsToDelete.add(url);+ }++ byte[] getBytes() {+ Gson gson = new Gson();+ return gson.toJson(this).getBytes();+ }++ boolean isEmpty () {+ return docsToWrite.isEmpty() && docsToUpdate.isEmpty() && docsToDelete.isEmpty();+ }++ public List<RabbitDocument> getDocsToWrite() {+ return docsToWrite;+ }++ public List<RabbitDocument> getDocsToUpdate() {+ return docsToUpdate;+ }++ public List<String> getDocsToDelete() {+ return docsToDelete;+ }++ public int size () {+ return docsToWrite.size() + docsToUpdate.size() + docsToDelete.size();+ }++ public void clear() {+ docsToWrite.clear();+ docsToUpdate.clear();+ docsToDelete.clear();+ }+}