Apache Nutch is an extensible and scalable web crawler
Merge branch 'master' into NUTCH-1480
Roannel Fernández Hernández54b7fa877f73
diff --cc src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestConstants.javaindex 1161717f7,b36f0271c..0fc4eb790--- 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,15 -17,21 +17,19 @@@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";- }++ public static final String LANGUAGES = INDEX + ".languages";+ public static final String SEPARATOR = INDEX + ".separator";+ public static final String SINK = INDEX + ".sink";+ }diff --cc src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/ElasticRestIndexWriter.javaindex 5b0f3ce18,6371362e9..67a843c7c--- 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@@@ -100,25 -101,17 +102,29 @@@ public class ElasticRestIndexWriter imp@Overridepublic void open(JobConf job, String name) throws IOException {+ //Implementation not required+ }++ @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);- 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);+ languages = job.getStrings(ElasticRestConstants.LANGUAGES);+ separator = job.get(ElasticRestConstants.SEPARATOR, DEFAULT_SEPARATOR);+ sink = job.get(ElasticRestConstants.SINK, DEFAULT_SINK);+// trust ALL certificatesSSLContext sslContext = null;try {@@@ -167,14 -160,27 +173,26 @@@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);-}++ private static Object normalizeValue(Object value) {+ if (value == null) {+ return null;+ }++ if (value instanceof Map) {+ return value;+ }++ return value.toString();+ }@Overridepublic void write(NutchDocument doc) throws IOException {diff --cc src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.javaindex cac1af270,d9671c41a..bdb65db07--- 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,8 -30,8 +30,9 @@@ import org.apache.commons.lang.StringUtimport 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.apache.nutch.indexer.NutchField;import org.elasticsearch.action.bulk.BulkResponse;import org.elasticsearch.action.bulk.BulkRequest;import org.elasticsearch.action.bulk.BackoffPolicy;@@@ -118,20 -103,21 +120,20 @@@ public class ElasticIndexWriter impleme}/** 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();+ Settings.Builder settingsBuilder = Settings.builder();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 = line.trim();- parts = line.split("=");+ parts = line.trim().split("=");if (parts.length == 2) {settingsBuilder.put(parts[0].trim(), parts[1].trim());@@@ -146,17 -131,21 +148,18 @@@Settings settings = settingsBuilder.build();- Client client = null;-// Prefer TransportClientif (hosts != null && port > 1) {- TransportClient transportClient = TransportClient.builder().settings(settings).build();- for (String host: hosts) {+ TransportClient transportClient = new PreBuiltTransportClient(settings);++ 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();+ node = new Node(settings);client = node.client();}-- return client;}/** Generates a default BulkProcessor.Listener */