Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Fixes for NUTCH-1480: Sections for all indexer-* plugins, relaxed schema, copy to the same field is not allowed and IndexWriterParams class to facilitate the process of obtaining values from index-writers.xml file.

r0ann3l84246a9e8fb1
diff --git a/conf/index-writers.xml.template b/conf/index-writers.xml.template
index abb50fb03..e9639ce53 100644
--- a/conf/index-writers.xml.template
+++ b/conf/index-writers.xml.template
@@ -72,4 +72,69 @@
</remove>
</mapping>
</writer>
+ <writer id="indexer_dummy_1" class="org.apache.nutch.indexwriter.dummy.DummyIndexWriter">
+ <parameters>
+ <param name="delete" value="false"/>
+ <param name="path" value="/"/>
+ </parameters>
+ <mapping>
+ <copy />
+ <rename />
+ <remove />
+ </mapping>
+ </writer>
+ <writer id="indexer_elastic_1" class="org.apache.nutch.indexwriter.elastic.ElasticIndexWriter">
+ <parameters>
+ <param name="host" value=""/>
+ <param name="port" value="9300"/>
+ <param name="cluster" value=""/>
+ <param name="index" value="nutch"/>
+ <param name="max.bulk.docs" value="250"/>
+ <param name="max.bulk.size" value="2500500"/>
+ <param name="exponential.backoff.millis" value="100"/>
+ <param name="exponential.backoff.retries" value="10"/>
+ <param name="bulk.close.timeout" value="600"/>
+ </parameters>
+ <mapping>
+ <copy>
+ <field source="title" dest="title,search"/>
+ </copy>
+ <rename />
+ <remove />
+ </mapping>
+ </writer>
+ <writer id="indexer_elastic_rest_1" class="org.apache.nutch.indexwriter.elasticrest.ElasticRestIndexWriter">
+ <parameters>
+ <param name="host" value=""/>
+ <param name="port" value="9200"/>
+ <param name="index" value="nutch"/>
+ <param name="max.bulk.docs" value="250"/>
+ <param name="max.bulk.size" value="2500500"/>
+ <param name="user" value="user"/>
+ <param name="password" value="password"/>
+ <param name="type" value="doc"/>
+ <param name="https" value="false"/>
+ <param name="trustallhostnames" value="false"/>
+ </parameters>
+ <mapping>
+ <copy>
+ <field source="title" dest="search"/>
+ </copy>
+ <rename />
+ <remove />
+ </mapping>
+ </writer>
+ <writer id="indexer_cloud_search_1" class="org.apache.nutch.indexwriter.elasticrest.ElasticRestIndexWriter">
+ <parameters>
+ <param name="endpoint" value=""/>
+ <param name="region" value=""/>
+ <param name="batch.dump" value="false"/>
+ <param name="batch.maxSize" value="-1"/>
+ </parameters>
+ <mapping>
+ <copy />
+ <rename />
+ <remove />
+ </mapping>
+ </writer>
</writers>
diff --git a/src/java/org/apache/nutch/indexer/IndexWriter.java b/src/java/org/apache/nutch/indexer/IndexWriter.java
index 495cc82b0..1e9ead015 100644
--- a/src/java/org/apache/nutch/indexer/IndexWriter.java
+++ b/src/java/org/apache/nutch/indexer/IndexWriter.java
@@ -38,7 +38,7 @@ public interface IndexWriter extends Pluggable, Configurable {
* @param parameters Params from the index writer configuration.
* @throws IOException Some exception thrown by writer.
*/
- void open(Map<String, String> parameters) throws IOException;
+ void open(IndexWriterParams parameters) throws IOException;
public void write(NutchDocument doc) throws IOException;
diff --git a/src/java/org/apache/nutch/indexer/IndexWriterConfig.java b/src/java/org/apache/nutch/indexer/IndexWriterConfig.java
index 86b6f03d2..9a38676b6 100644
--- a/src/java/org/apache/nutch/indexer/IndexWriterConfig.java
+++ b/src/java/org/apache/nutch/indexer/IndexWriterConfig.java
@@ -29,14 +29,14 @@ public class IndexWriterConfig {
private String clazz;
- private Map<String, String> params;
+ private IndexWriterParams params;
private Map<MappingReader.Actions, Map<String, List<String>>> mapping;
private IndexWriterConfig(String id, String clazz, Map<String, String> params, Map<MappingReader.Actions, Map<String, List<String>>> mapping) {
this.id = id;
this.clazz = clazz;
- this.params = params;
+ this.params = new IndexWriterParams(params);
this.mapping = mapping;
}
@@ -64,7 +64,7 @@ public class IndexWriterConfig {
return clazz;
}
- Map<String, String> getParams() {
+ IndexWriterParams getParams() {
return params;
}
diff --git a/src/java/org/apache/nutch/indexer/IndexWriterParams.java b/src/java/org/apache/nutch/indexer/IndexWriterParams.java
new file mode 100644
index 000000000..b6c25c870
--- /dev/null
+++ b/src/java/org/apache/nutch/indexer/IndexWriterParams.java
@@ -0,0 +1,58 @@
+package org.apache.nutch.indexer;
+
+import org.apache.hadoop.util.StringUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class IndexWriterParams extends HashMap<String, String> {
+
+ /**
+ * Constructs a new <tt>HashMap</tt> with the same mappings as the
+ * specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
+ * default load factor (0.75) and an initial capacity sufficient to
+ * hold the mappings in the specified <tt>Map</tt>.
+ *
+ * @param m the map whose mappings are to be placed in this map
+ * @throws NullPointerException if the specified map is null
+ */
+ IndexWriterParams(Map<? extends String, ? extends String> m) {
+ super(m);
+ }
+
+ public String get(String name, String defaultValue) {
+ return this.getOrDefault(name, defaultValue);
+ }
+
+ public boolean getBoolean(String name, boolean defaultValue) {
+ String value;
+ if ((value = this.get(name)) != null) {
+ return Boolean.parseBoolean(value);
+ }
+
+ return defaultValue;
+ }
+
+ public long getLong(String name, long defaultValue) {
+ String value;
+ if ((value = this.get(name)) != null) {
+ return Long.parseLong(value);
+ }
+
+ return defaultValue;
+ }
+
+ public int getInt(String name, int defaultValue) {
+ String value;
+ if ((value = this.get(name)) != null) {
+ return Integer.parseInt(value);
+ }
+
+ return defaultValue;
+ }
+
+ public String[] getStrings(String name) {
+ String value = this.get(name);
+ return StringUtils.getStrings(value);
+ }
+}
diff --git a/src/java/org/apache/nutch/indexer/IndexWriters.java b/src/java/org/apache/nutch/indexer/IndexWriters.java
index 0b49f608f..2891971eb 100644
--- a/src/java/org/apache/nutch/indexer/IndexWriters.java
+++ b/src/java/org/apache/nutch/indexer/IndexWriters.java
@@ -147,8 +147,11 @@ public class IndexWriters {
//Checking whether the field to copy exists or not
if (mappedDocument.getField(key) != null) {
for (String field : value) {
- for (Object val : mappedDocument.getField(key).getValues()) {
- mappedDocument.add(field, val);
+ //To avoid duplicate the values
+ if (!key.equals(field)) {
+ for (Object val : mappedDocument.getField(key).getValues()) {
+ mappedDocument.add(field, val);
+ }
}
}
}
diff --git a/src/plugin/indexer-cloudsearch/src/java/org/apache/nutch/indexwriter/cloudsearch/CloudSearchConstants.java b/src/plugin/indexer-cloudsearch/src/java/org/apache/nutch/indexwriter/cloudsearch/CloudSearchConstants.java
index 8bfb16146..064a4f6ed 100644
--- a/src/plugin/indexer-cloudsearch/src/java/org/apache/nutch/indexwriter/cloudsearch/CloudSearchConstants.java
+++ b/src/plugin/indexer-cloudsearch/src/java/org/apache/nutch/indexwriter/cloudsearch/CloudSearchConstants.java
@@ -18,10 +18,8 @@
package org.apache.nutch.indexwriter.cloudsearch;
public interface CloudSearchConstants {
- public static final String CLOUDSEARCH_PREFIX = "cloudsearch.";
- public static final String ENDPOINT = CLOUDSEARCH_PREFIX + "endpoint";
- public static final String REGION = CLOUDSEARCH_PREFIX + "region";
- public static final String BATCH_DUMP = CLOUDSEARCH_PREFIX + "batch.dump";
- public static final String MAX_DOCS_BATCH = CLOUDSEARCH_PREFIX
- + "batch.maxSize";
+ public static final String ENDPOINT = "endpoint";
+ public static final String REGION = "region";
+ public static final String BATCH_DUMP = "batch.dump";
+ public static final String MAX_DOCS_BATCH = "batch.maxSize";
}
diff --git a/src/plugin/indexer-cloudsearch/src/java/org/apache/nutch/indexwriter/cloudsearch/CloudSearchIndexWriter.java b/src/plugin/indexer-cloudsearch/src/java/org/apache/nutch/indexwriter/cloudsearch/CloudSearchIndexWriter.java
index 75e5d2fb9..7248d3778 100644
--- a/src/plugin/indexer-cloudsearch/src/java/org/apache/nutch/indexwriter/cloudsearch/CloudSearchIndexWriter.java
+++ b/src/plugin/indexer-cloudsearch/src/java/org/apache/nutch/indexwriter/cloudsearch/CloudSearchIndexWriter.java
@@ -35,9 +35,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapred.JobConf;
-import org.apache.nutch.indexer.IndexWriter;
-import org.apache.nutch.indexer.NutchDocument;
-import org.apache.nutch.indexer.NutchField;
+import org.apache.nutch.indexer.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -87,14 +85,26 @@ public class CloudSearchIndexWriter implements IndexWriter {
@Override
public void open(JobConf job, String name) throws IOException {
- LOG.debug("CloudSearchIndexWriter.open() name={} ", name);
+ //Implementation not required
+ }
- maxDocsInBatch = job.getInt(CloudSearchConstants.MAX_DOCS_BATCH, -1);
+ @Override
+ public void open(IndexWriterParams parameters) throws IOException {
+ String endpoint = parameters.get(CloudSearchConstants.ENDPOINT);
+ boolean dumpBatchFilesToTemp = parameters
+ .getBoolean(CloudSearchConstants.BATCH_DUMP, false);
+ this.regionName = parameters.get(CloudSearchConstants.REGION);
- buffer = new StringBuffer(MAX_SIZE_BATCH_BYTES).append('[');
+ if (StringUtils.isBlank(endpoint) && !dumpBatchFilesToTemp) {
+ String message = "Missing CloudSearch endpoint. Should set it in index-writers.xml";
+ message += "\n" + describe();
+ LOG.error(message);
+ throw new RuntimeException(message);
+ }
- dumpBatchFilesToTemp = job.getBoolean(CloudSearchConstants.BATCH_DUMP,
- false);
+ maxDocsInBatch = parameters.getInt(CloudSearchConstants.MAX_DOCS_BATCH, -1);
+
+ buffer = new StringBuffer(MAX_SIZE_BATCH_BYTES).append('[');
if (dumpBatchFilesToTemp) {
// only dumping to local file
@@ -102,12 +112,11 @@ public class CloudSearchIndexWriter implements IndexWriter {
return;
}
- String endpoint = job.get(CloudSearchConstants.ENDPOINT);
-
if (StringUtils.isBlank(endpoint)) {
throw new RuntimeException("endpoint not set for CloudSearch");
}
+ this.regionName = parameters.get(CloudSearchConstants.REGION);
AmazonCloudSearchClient cl = new AmazonCloudSearchClient();
if (StringUtils.isNotBlank(regionName)) {
cl.setRegion(RegionUtils.getRegion(regionName));
@@ -117,7 +126,7 @@ public class CloudSearchIndexWriter implements IndexWriter {
// retrieve the domain name
DescribeDomainsResult domains = cl
- .describeDomains(new DescribeDomainsRequest());
+ .describeDomains(new DescribeDomainsRequest());
Iterator<DomainStatus> dsiter = domains.getDomainStatusList().iterator();
while (dsiter.hasNext()) {
@@ -131,11 +140,11 @@ public class CloudSearchIndexWriter implements IndexWriter {
// check domain name
if (StringUtils.isBlank(domainName)) {
throw new RuntimeException(
- "No domain name found for CloudSearch endpoint");
+ "No domain name found for CloudSearch endpoint");
}
DescribeIndexFieldsResult indexDescription = cl.describeIndexFields(
- new DescribeIndexFieldsRequest().withDomainName(domainName));
+ new DescribeIndexFieldsRequest().withDomainName(domainName));
for (IndexFieldStatus ifs : indexDescription.getIndexFields()) {
String indexname = ifs.getOptions().getIndexFieldName();
String indextype = ifs.getOptions().getIndexFieldType();
@@ -145,12 +154,6 @@ public class CloudSearchIndexWriter implements IndexWriter {
client = new AmazonCloudSearchDomainClient();
client.setEndpoint(endpoint);
-
- }
-
- @Override
- public void open(Map<String, String> parameters) throws IOException {
-
}
@Override
diff --git a/src/plugin/indexer-dummy/src/java/org/apache/nutch/indexwriter/dummy/DummyConstants.java b/src/plugin/indexer-dummy/src/java/org/apache/nutch/indexwriter/dummy/DummyConstants.java
new file mode 100644
index 000000000..7dea970b4
--- /dev/null
+++ b/src/plugin/indexer-dummy/src/java/org/apache/nutch/indexwriter/dummy/DummyConstants.java
@@ -0,0 +1,7 @@
+package org.apache.nutch.indexwriter.dummy;
+
+public interface DummyConstants {
+ String DELETE = "delete";
+
+ String PATH = "path";
+}
diff --git a/src/plugin/indexer-dummy/src/java/org/apache/nutch/indexwriter/dummy/DummyIndexWriter.java b/src/plugin/indexer-dummy/src/java/org/apache/nutch/indexwriter/dummy/DummyIndexWriter.java
index 740e3905e..9f371917e 100644
--- a/src/plugin/indexer-dummy/src/java/org/apache/nutch/indexwriter/dummy/DummyIndexWriter.java
+++ b/src/plugin/indexer-dummy/src/java/org/apache/nutch/indexwriter/dummy/DummyIndexWriter.java
@@ -26,6 +26,7 @@ import java.util.Map;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapred.JobConf;
import org.apache.nutch.indexer.IndexWriter;
+import org.apache.nutch.indexer.IndexWriterParams;
import org.apache.nutch.indexer.IndexerMapReduce;
import org.apache.nutch.indexer.NutchDocument;
import org.slf4j.Logger;
@@ -43,8 +44,9 @@ public class DummyIndexWriter implements IndexWriter {
private Writer writer;
private boolean delete = false;
+ @Override
public void open(JobConf job, String name) throws IOException {
- delete = job.getBoolean(IndexerMapReduce.INDEXER_DELETE, false);
+ //Implementation not required
}
/**
@@ -54,8 +56,21 @@ public class DummyIndexWriter implements IndexWriter {
* @throws IOException Some exception thrown by writer.
*/
@Override
- public void open(Map<String, String> parameters) throws IOException {
- delete = config.getBoolean(IndexerMapReduce.INDEXER_DELETE, false);
+ public void open(IndexWriterParams parameters) throws IOException {
+ delete = parameters.getBoolean(DummyConstants.DELETE, false);
+
+ String path = parameters.get(DummyConstants.PATH, "/");
+ if (path == null) {
+ String message = "Missing path.";
+ message += "\n" + describe();
+ LOG.error(message);
+ throw new RuntimeException(message);
+ }
+
+ try {
+ writer = new BufferedWriter(new FileWriter(path));
+ } catch (IOException ignored) {
+ }
}
@Override
@@ -93,18 +108,6 @@ public class DummyIndexWriter implements IndexWriter {
@Override
public void setConf(Configuration conf) {
config = conf;
- String path = conf.get("dummy.path");
- if (path == null) {
- String message = "Missing path. Should be set via -Ddummy.path";
- message += "\n" + describe();
- LOG.error(message);
- throw new RuntimeException(message);
- }
-
- try {
- writer = new BufferedWriter(new FileWriter(conf.get("dummy.path")));
- } catch (IOException e) {
- }
}
public String describe() {
diff --git a/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestConstants.java b/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestConstants.java
index 322ff44fc..1161717f7 100644
--- a/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestConstants.java
+++ b/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestConstants.java
@@ -17,17 +17,15 @@
package org.apache.nutch.indexwriter.elasticrest;
public interface ElasticRestConstants {
- public static final String ELASTIC_PREFIX = "elastic.rest.";
+ public static final String HOST = "host";
+ public static final String PORT = "port";
+ public static final String INDEX = "index";
+ public static final String MAX_BULK_DOCS = "max.bulk.docs";
+ public static final String MAX_BULK_LENGTH = "max.bulk.size";
- public static final String HOST = ELASTIC_PREFIX + "host";
- public static final String PORT = ELASTIC_PREFIX + "port";
- public static final String INDEX = ELASTIC_PREFIX + "index";
- public static final String MAX_BULK_DOCS = ELASTIC_PREFIX + "max.bulk.docs";
- public static final String MAX_BULK_LENGTH = ELASTIC_PREFIX + "max.bulk.size";
-
- public static final String USER = ELASTIC_PREFIX + "user";
- public static final String PASSWORD = ELASTIC_PREFIX + "password";
- public static final String TYPE = ELASTIC_PREFIX + "type";
- public static final String HTTPS = ELASTIC_PREFIX + "https";
- public static final String HOSTNAME_TRUST = ELASTIC_PREFIX + "trustallhostnames";
+ public static final String USER = "user";
+ public static final String PASSWORD = "password";
+ public static final String TYPE = "type";
+ public static final String HTTPS = "https";
+ public static final String HOSTNAME_TRUST = "trustallhostnames";
}
\ No newline at end of file
diff --git a/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestIndexWriter.java b/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestIndexWriter.java
index 5dc5e53c0..252d27b19 100644
--- a/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestIndexWriter.java
+++ b/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestIndexWriter.java
@@ -42,6 +42,7 @@ import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.nutch.indexer.IndexWriter;
+import org.apache.nutch.indexer.IndexWriterParams;
import org.apache.nutch.indexer.NutchDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -97,26 +98,37 @@ public class ElasticRestIndexWriter implements IndexWriter {
@Override
public void open(JobConf job, String name) throws IOException {
+ //Implementation not required
+ }
- host = job.get(ElasticRestConstants.HOST);
- port = job.getInt(ElasticRestConstants.PORT, 9200);
- user = job.get(ElasticRestConstants.USER);
- password = job.get(ElasticRestConstants.PASSWORD);
- https = job.getBoolean(ElasticRestConstants.HTTPS, false);
- trustAllHostnames = job.getBoolean(ElasticRestConstants.HOSTNAME_TRUST, false);
+ @Override
+ public void open(IndexWriterParams parameters) throws IOException {
+ host = parameters.get(ElasticRestConstants.HOST);
+ if (StringUtils.isBlank(host)) {
+ String message = "Missing host. It should be set in index-writers.xml";
+ message += "\n" + describe();
+ LOG.error(message);
+ throw new RuntimeException(message);
+ }
+
+ port = parameters.getInt(ElasticRestConstants.PORT, 9200);
+ user = parameters.get(ElasticRestConstants.USER);
+ password = parameters.get(ElasticRestConstants.PASSWORD);
+ https = parameters.getBoolean(ElasticRestConstants.HTTPS, false);
+ trustAllHostnames = parameters.getBoolean(ElasticRestConstants.HOSTNAME_TRUST, false);
// trust ALL certificates
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder()
- .loadTrustMaterial(new TrustStrategy() {
- public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
- return true;
- }
- }).build();
+ .loadTrustMaterial(new TrustStrategy() {
+ public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
+ return true;
+ }
+ }).build();
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
LOG.error("Failed to instantiate sslcontext object: \n{}",
- ExceptionUtils.getStackTrace(e));
+ ExceptionUtils.getStackTrace(e));
throw new SecurityException();
}
@@ -136,15 +148,15 @@ public class ElasticRestIndexWriter implements IndexWriter {
if (host != null && port > 1) {
HttpClientConfig.Builder builder = new HttpClientConfig.Builder(
- urlOfElasticsearchNode.toString()).multiThreaded(true)
+ urlOfElasticsearchNode.toString()).multiThreaded(true)
.connTimeout(300000).readTimeout(300000);
if (https) {
if (user != null && password != null) {
builder.defaultCredentials(user, password);
}
builder.defaultSchemeForDiscoveredNodes("https")
- .sslSocketFactory(sslSocketFactory) // this only affects sync calls
- .httpsIOSessionStrategy(httpsIOSessionStrategy); // this only affects async calls
+ .sslSocketFactory(sslSocketFactory) // this only affects sync calls
+ .httpsIOSessionStrategy(httpsIOSessionStrategy); // this only affects async calls
}
jestClientFactory.setHttpClientConfig(builder.build());
} else {
@@ -153,19 +165,13 @@ public class ElasticRestIndexWriter implements IndexWriter {
client = jestClientFactory.getObject();
- defaultIndex = job.get(ElasticRestConstants.INDEX, "nutch");
- defaultType = job.get(ElasticRestConstants.TYPE, "doc");
+ defaultIndex = parameters.get(ElasticRestConstants.INDEX, "nutch");
+ defaultType = parameters.get(ElasticRestConstants.TYPE, "doc");
- maxBulkDocs = job.getInt(ElasticRestConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);
- maxBulkLength = job.getInt(ElasticRestConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);
+ maxBulkDocs = parameters.getInt(ElasticRestConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);
+ maxBulkLength = parameters.getInt(ElasticRestConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);
bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex).defaultType(defaultType);
-
- }
-
- @Override
- public void open(Map<String, String> parameters) throws IOException {
-
}
@Override
@@ -316,15 +322,6 @@ public class ElasticRestIndexWriter implements IndexWriter {
@Override
public void setConf(Configuration conf) {
config = conf;
- String host = conf.get(ElasticRestConstants.HOST);
- String port = conf.get(ElasticRestConstants.PORT);
-
- if (StringUtils.isBlank(host) && StringUtils.isBlank(port)) {
- String message = "Missing elastic.rest.host and elastic.rest.port. At least one of them should be set in nutch-site.xml ";
- message += "\n" + describe();
- LOG.error(message);
- throw new RuntimeException(message);
- }
}
@Override
diff --git a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticConstants.java b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticConstants.java
index 29f36c736..7684684ed 100644
--- a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticConstants.java
+++ b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticConstants.java
@@ -17,15 +17,13 @@
package org.apache.nutch.indexwriter.elastic;
public interface ElasticConstants {
- public static final String ELASTIC_PREFIX = "elastic.";
-
- public static final String HOSTS = ELASTIC_PREFIX + "host";
- public static final String PORT = ELASTIC_PREFIX + "port";
- public static final String CLUSTER = ELASTIC_PREFIX + "cluster";
- public static final String INDEX = ELASTIC_PREFIX + "index";
- public static final String MAX_BULK_DOCS = ELASTIC_PREFIX + "max.bulk.docs";
- public static final String MAX_BULK_LENGTH = ELASTIC_PREFIX + "max.bulk.size";
- public static final String EXPONENTIAL_BACKOFF_MILLIS = ELASTIC_PREFIX + "exponential.backoff.millis";
- public static final String EXPONENTIAL_BACKOFF_RETRIES = ELASTIC_PREFIX + "exponential.backoff.retries";
- public static final String BULK_CLOSE_TIMEOUT = ELASTIC_PREFIX + "bulk.close.timeout";
+ public static final String HOSTS = "host";
+ public static final String PORT = "port";
+ public static final String CLUSTER = "cluster";
+ public static final String INDEX = "index";
+ public static final String MAX_BULK_DOCS = "max.bulk.docs";
+ public static final String MAX_BULK_LENGTH = "max.bulk.size";
+ public static final String EXPONENTIAL_BACKOFF_MILLIS = "exponential.backoff.millis";
+ public static final String EXPONENTIAL_BACKOFF_RETRIES = "exponential.backoff.retries";
+ public static final String BULK_CLOSE_TIMEOUT = "bulk.close.timeout";
}
\ No newline at end of file
diff --git a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java
index e6f6fb959..cac1af270 100644
--- a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java
+++ b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java
@@ -31,6 +31,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapred.JobConf;
import org.apache.nutch.indexer.IndexWriter;
+import org.apache.nutch.indexer.IndexWriterParams;
import org.apache.nutch.indexer.NutchDocument;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.bulk.BulkRequest;
@@ -75,29 +76,7 @@ public class ElasticIndexWriter implements IndexWriter {
@Override
public void open(JobConf job, String name) throws IOException {
- bulkCloseTimeout = job.getLong(ElasticConstants.BULK_CLOSE_TIMEOUT,
- DEFAULT_BULK_CLOSE_TIMEOUT);
- defaultIndex = job.get(ElasticConstants.INDEX, DEFAULT_INDEX);
-
- int maxBulkDocs = job.getInt(ElasticConstants.MAX_BULK_DOCS,
- DEFAULT_MAX_BULK_DOCS);
- int maxBulkLength = job.getInt(ElasticConstants.MAX_BULK_LENGTH,
- DEFAULT_MAX_BULK_LENGTH);
- int expBackoffMillis = job.getInt(ElasticConstants.EXPONENTIAL_BACKOFF_MILLIS,
- DEFAULT_EXP_BACKOFF_MILLIS);
- int expBackoffRetries = job.getInt(ElasticConstants.EXPONENTIAL_BACKOFF_RETRIES,
- DEFAULT_EXP_BACKOFF_RETRIES);
-
- client = makeClient(job);
-
- LOG.debug("Creating BulkProcessor with maxBulkDocs={}, maxBulkLength={}", maxBulkDocs, maxBulkLength);
- bulkProcessor = BulkProcessor.builder(client, bulkProcessorListener())
- .setBulkActions(maxBulkDocs)
- .setBulkSize(new ByteSizeValue(maxBulkLength, ByteSizeUnit.BYTES))
- .setConcurrentRequests(1)
- .setBackoffPolicy(BackoffPolicy.exponentialBackoff(
- TimeValue.timeValueMillis(expBackoffMillis), expBackoffRetries))
- .build();
+ //Implementation not required
}
/**
@@ -107,26 +86,52 @@ public class ElasticIndexWriter implements IndexWriter {
* @throws IOException Some exception thrown by writer.
*/
@Override
- public void open(Map<String, String> parameters) throws IOException {
+ public void open(IndexWriterParams parameters) throws IOException {
+ String cluster = parameters.get(ElasticConstants.CLUSTER);
+ String hosts = parameters.get(ElasticConstants.HOSTS);
+
+ if (StringUtils.isBlank(cluster) && StringUtils.isBlank(hosts)) {
+ String message = "Missing elastic.cluster and elastic.host. At least one of them should be set in index-writers.xml ";
+ message += "\n" + describe();
+ LOG.error(message);
+ throw new RuntimeException(message);
+ }
+
+ bulkCloseTimeout = parameters.getLong(ElasticConstants.BULK_CLOSE_TIMEOUT, DEFAULT_BULK_CLOSE_TIMEOUT);
+ defaultIndex = parameters.get(ElasticConstants.INDEX, DEFAULT_INDEX);
+
+ int maxBulkDocs = parameters.getInt(ElasticConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);
+ int maxBulkLength = parameters.getInt(ElasticConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);
+ int expBackoffMillis = parameters.getInt(ElasticConstants.EXPONENTIAL_BACKOFF_MILLIS, DEFAULT_EXP_BACKOFF_MILLIS);
+ int expBackoffRetries = parameters.getInt(ElasticConstants.EXPONENTIAL_BACKOFF_RETRIES, DEFAULT_EXP_BACKOFF_RETRIES);
+ makeClient(parameters);
+
+ LOG.debug("Creating BulkProcessor with maxBulkDocs={}, maxBulkLength={}", maxBulkDocs, maxBulkLength);
+ bulkProcessor = BulkProcessor.builder(client, bulkProcessorListener())
+ .setBulkActions(maxBulkDocs)
+ .setBulkSize(new ByteSizeValue(maxBulkLength, ByteSizeUnit.BYTES))
+ .setConcurrentRequests(1)
+ .setBackoffPolicy(BackoffPolicy.exponentialBackoff(
+ TimeValue.timeValueMillis(expBackoffMillis), expBackoffRetries))
+ .build();
}
/** Generates a TransportClient or NodeClient */
- protected Client makeClient(Configuration conf) throws IOException {
- String clusterName = conf.get(ElasticConstants.CLUSTER);
- String[] hosts = conf.getStrings(ElasticConstants.HOSTS);
- int port = conf.getInt(ElasticConstants.PORT, DEFAULT_PORT);
+ protected void makeClient(IndexWriterParams parameters) throws IOException {
+ String clusterName = parameters.get(ElasticConstants.CLUSTER);
+ String[] hosts = parameters.getStrings(ElasticConstants.HOSTS);
+ int port = parameters.getInt(ElasticConstants.PORT, DEFAULT_PORT);
Settings.Builder settingsBuilder = Settings.settingsBuilder();
BufferedReader reader = new BufferedReader(
- conf.getConfResourceAsReader("elasticsearch.conf"));
+ config.getConfResourceAsReader("elasticsearch.conf"));
String line;
- String parts[];
+ String[] parts;
while ((line = reader.readLine()) != null) {
if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
- line.trim();
- parts = line.split("=");
+ parts = line.trim().split("=");
if (parts.length == 2) {
settingsBuilder.put(parts[0].trim(), parts[1].trim());
@@ -135,25 +140,23 @@ public class ElasticIndexWriter implements IndexWriter {
}
// Set the cluster name and build the settings
- if (StringUtils.isNotBlank(clusterName))
+ if (StringUtils.isNotBlank(clusterName)) {
settingsBuilder.put("cluster.name", clusterName);
+ }
Settings settings = settingsBuilder.build();
- Client client = null;
-
// Prefer TransportClient
if (hosts != null && port > 1) {
TransportClient transportClient = TransportClient.builder().settings(settings).build();
- for (String host: hosts)
+ for (String host: hosts) {
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
+ }
client = transportClient;
} else if (clusterName != null) {
node = nodeBuilder().settings(settings).client(true).node();
client = node.client();
}
-
- return client;
}
/** Generates a default BulkProcessor.Listener */
@@ -256,15 +259,6 @@ public class ElasticIndexWriter implements IndexWriter {
@Override
public void setConf(Configuration conf) {
config = conf;
- String cluster = conf.get(ElasticConstants.CLUSTER);
- String hosts = conf.get(ElasticConstants.HOSTS);
-
- if (StringUtils.isBlank(cluster) && StringUtils.isBlank(hosts)) {
- String message = "Missing elastic.cluster and elastic.host. At least one of them should be set in nutch-site.xml ";
- message += "\n" + describe();
- LOG.error(message);
- throw new RuntimeException(message);
- }
}
@Override
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.java
index 1735c228e..31e31b753 100644
--- 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.java
@@ -22,6 +22,7 @@ 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.IndexWriterParams;
import org.apache.nutch.indexer.NutchDocument;
import org.apache.hadoop.mapred.JobConf;
@@ -84,22 +85,22 @@ public class RabbitIndexWriter implements IndexWriter {
* @throws IOException Some exception thrown by writer.
*/
@Override
- public void open(Map<String, String> parameters) throws IOException {
- serverHost = parameters.getOrDefault(RabbitMQConstants.SERVER_HOST, "localhost");
- serverPort = Integer.parseInt(parameters.getOrDefault(RabbitMQConstants.SERVER_PORT, "5672"));
- serverVirtualHost = parameters.getOrDefault(RabbitMQConstants.SERVER_VIRTUAL_HOST, null);
+ public void open(IndexWriterParams parameters) throws IOException {
+ serverHost = parameters.get(RabbitMQConstants.SERVER_HOST, "localhost");
+ serverPort = parameters.getInt(RabbitMQConstants.SERVER_PORT, 5672);
+ serverVirtualHost = parameters.get(RabbitMQConstants.SERVER_VIRTUAL_HOST, null);
- serverUsername = parameters.getOrDefault(RabbitMQConstants.SERVER_USERNAME, "admin");
- serverPassword = parameters.getOrDefault(RabbitMQConstants.SERVER_PASSWORD, "admin");
+ serverUsername = parameters.get(RabbitMQConstants.SERVER_USERNAME, "admin");
+ serverPassword = parameters.get(RabbitMQConstants.SERVER_PASSWORD, "admin");
- exchangeServer = parameters.getOrDefault(RabbitMQConstants.EXCHANGE_SERVER, "nutch.exchange");
- exchangeType = parameters.getOrDefault(RabbitMQConstants.EXCHANGE_TYPE, "direct");
+ exchangeServer = parameters.get(RabbitMQConstants.EXCHANGE_SERVER, "nutch.exchange");
+ exchangeType = parameters.get(RabbitMQConstants.EXCHANGE_TYPE, "direct");
- queueName = parameters.getOrDefault(RabbitMQConstants.QUEUE_NAME, "nutch.queue");
- queueDurable = Boolean.parseBoolean(parameters.getOrDefault(RabbitMQConstants.QUEUE_DURABLE, "true"));
- queueRoutingKey = parameters.getOrDefault(RabbitMQConstants.QUEUE_ROUTING_KEY, "nutch.key");
+ queueName = parameters.get(RabbitMQConstants.QUEUE_NAME, "nutch.queue");
+ queueDurable = parameters.getBoolean(RabbitMQConstants.QUEUE_DURABLE, true);
+ queueRoutingKey = parameters.get(RabbitMQConstants.QUEUE_ROUTING_KEY, "nutch.key");
- commitSize = Integer.parseInt(parameters.getOrDefault(RabbitMQConstants.COMMIT_SIZE, "250"));
+ commitSize = parameters.getInt(RabbitMQConstants.COMMIT_SIZE, 250);
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(serverHost);
diff --git a/src/plugin/indexer-solr/src/java/org/apache/nutch/indexwriter/solr/SolrIndexWriter.java b/src/plugin/indexer-solr/src/java/org/apache/nutch/indexwriter/solr/SolrIndexWriter.java
index 2a8fd6fee..ecd714f22 100644
--- a/src/plugin/indexer-solr/src/java/org/apache/nutch/indexwriter/solr/SolrIndexWriter.java
+++ b/src/plugin/indexer-solr/src/java/org/apache/nutch/indexwriter/solr/SolrIndexWriter.java
@@ -19,10 +19,7 @@ package org.apache.nutch.indexwriter.solr;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.util.StringUtils;
-import org.apache.nutch.indexer.IndexWriter;
-import org.apache.nutch.indexer.IndexerMapReduce;
-import org.apache.nutch.indexer.NutchDocument;
-import org.apache.nutch.indexer.NutchField;
+import org.apache.nutch.indexer.*;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
@@ -82,10 +79,10 @@ public class SolrIndexWriter implements IndexWriter {
* @throws IOException Some exception thrown by writer.
*/
@Override
- public void open(Map<String, String> parameters) throws IOException {
- String type = parameters.getOrDefault("type", "http");
+ public void open(IndexWriterParams parameters) throws IOException {
+ String type = parameters.get("type", "http");
- String[] urls = StringUtils.getStrings(parameters.get("url"));
+ String[] urls = parameters.getStrings("url");
if (urls == null) {
String message = "Missing SOLR URL.\n" + describe();
@@ -121,7 +118,7 @@ public class SolrIndexWriter implements IndexWriter {
init(parameters);
}
- private void init(Map<String, String> properties) {
+ private void init(IndexWriterParams properties) {
batchSize = Integer.parseInt(properties.getOrDefault(SolrConstants.COMMIT_SIZE, "1000"));
delete = config.getBoolean(IndexerMapReduce.INDEXER_DELETE, false);
// parse optional params