Apache Nutch is an extensible and scalable web crawler
log parameterization, code formatting
brianzhao767764ecfcd3
diff --git a/src/plugin/indexer-elastic-rest/howto_upgrade_es.txt b/src/plugin/indexer-elastic-rest/howto_upgrade_es.txtindex b57705304..4f4fbd453 100644--- a/src/plugin/indexer-elastic-rest/howto_upgrade_es.txt+++ b/src/plugin/indexer-elastic-rest/howto_upgrade_es.txt@@ -1,6 +1,23 @@-1. Upgrade elasticsearch dependency in src/plugin/indexer-elastic/ivy.xml+/*+ * 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.+ */-2. Upgrade the Elasticsearch specific dependencies in src/plugin/indexer-elastic/plugin.xml+1. Upgrade elasticsearch dependency in src/plugin/indexer-elastic-rest/ivy.xml++2. Upgrade the Elasticsearch specific dependencies in src/plugin/indexer-elastic-rest/plugin.xmlTo get the list of dependencies and their versions execute:$ ant -f ./build-ivy.xml$ ls lib/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.javaindex 684cc03f8..322ff44fc 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,17 @@package org.apache.nutch.indexwriter.elasticrest;public interface ElasticRestConstants {- public static final String ELASTIC_PREFIX = "elastic.rest.";+ public static final String ELASTIC_PREFIX = "elastic.rest.";- 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 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 = 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";}\ No newline at end of filediff --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.javaindex 639077e99..0e1d560c6 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@@ -15,7 +15,6 @@* limitations under the License.*/-//TODO trust self signed and non matching certs: http://stackoverflow.com/questions/2893819/telling-java-to-accept-self-signed-ssl-certificate//TODO refactor the dependencies out of root ivy filepackage org.apache.nutch.indexwriter.elasticrest;@@ -59,278 +58,272 @@ import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.HashMap;import java.util.Map;+import java.util.MissingResourceException;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;-/***/public class ElasticRestIndexWriter implements IndexWriter {- public static Logger LOG = LoggerFactory.getLogger(ElasticRestIndexWriter.class);-- private static final int DEFAULT_MAX_BULK_DOCS = 250;- private static final int DEFAULT_MAX_BULK_LENGTH = 2500500;-- private JestClient client;- private String defaultIndex;- private String defaultType = null;-- private Configuration config;-- private Bulk.Builder bulkBuilder;- private Future<HttpResponse> execute;- private int port = -1;- private String host = null;- private String user = null;- private Boolean https = null;- private String password = null;- private Boolean trustAllHostnames = null;-- private int maxBulkDocs;- private int maxBulkLength;- private long indexedDocs = 0;- private int bulkDocs = 0;- private int bulkLength = 0;- private boolean createNewBulk = false;- private long millis;- private BasicFuture<JestResult> basicFuture = null;-- @Override- public void open(JobConf job, String name) throws IOException {-- 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);-- // 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();- } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {- e.printStackTrace();- }-- // skip hostname checks- HostnameVerifier hostnameVerifier = null;- if (trustAllHostnames) {- hostnameVerifier = new NoopHostnameVerifier();- } else {- hostnameVerifier = new DefaultHostnameVerifier();- }-- SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);- SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);+ public static Logger LOG = LoggerFactory+ .getLogger(ElasticRestIndexWriter.class);++ private static final int DEFAULT_MAX_BULK_DOCS = 250;+ private static final int DEFAULT_MAX_BULK_LENGTH = 2500500;++ private JestClient client;+ private String defaultIndex;+ private String defaultType = null;++ private Configuration config;++ private Bulk.Builder bulkBuilder;+ private Future<HttpResponse> execute;+ private int port = -1;+ private String host = null;+ private Boolean https = null;+ private String user = null;+ private String password = null;+ private Boolean trustAllHostnames = null;++ private int maxBulkDocs;+ private int maxBulkLength;+ private long indexedDocs = 0;+ private int bulkDocs = 0;+ private int bulkLength = 0;+ private boolean createNewBulk = false;+ private long millis;+ private BasicFuture<JestResult> basicFuture = null;++ @Override+ public void open(JobConf job, String name) throws IOException {++ 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);++ // 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();+ } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {+ LOG.error("Failed to instantiate sslcontext object: \n{}",+ ExceptionUtils.getStackTrace(e));+ throw new SecurityException();+ }- JestClientFactory jestClientFactory = new JestClientFactory();- URL urlOfElasticsearchNode = new URL(https ? "https" : "http", host, port, "");+ // skip hostname checks+ HostnameVerifier hostnameVerifier = null;+ if (trustAllHostnames) {+ hostnameVerifier = NoopHostnameVerifier.INSTANCE;+ } else {+ hostnameVerifier = new DefaultHostnameVerifier();+ }+ SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);+ SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);- if (host != null && port > 1) {- HttpClientConfig.Builder builder = new HttpClientConfig.Builder(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+ JestClientFactory jestClientFactory = new JestClientFactory();+ URL urlOfElasticsearchNode = new URL(https ? "https" : "http", host, port, "");- }- jestClientFactory.setHttpClientConfig(builder.build());- } else {- throw new RuntimeException("No host and port specified");+ if (host != null && port > 1) {+ HttpClientConfig.Builder builder = new HttpClientConfig.Builder(+ 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+ }+ jestClientFactory.setHttpClientConfig(builder.build());+ } else {+ throw new IllegalStateException("No host or port specified. Please set the host and port in nutch-site.xml");+ }- client = jestClientFactory.getObject();+ client = jestClientFactory.getObject();- defaultIndex = job.get(ElasticRestConstants.INDEX, "nutch");- defaultType = job.get(ElasticRestConstants.TYPE, "doc");+ defaultIndex = job.get(ElasticRestConstants.INDEX, "nutch");+ defaultType = job.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 = job.getInt(ElasticRestConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);+ maxBulkLength = job.getInt(ElasticRestConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);- bulkBuilder = new Bulk.Builder()- .defaultIndex(defaultIndex)- .defaultType(defaultType);+ bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex).defaultType(defaultType);+ }++ @Override+ public void write(NutchDocument doc) throws IOException {+ String id = (String) doc.getFieldValue("id");+ String type = doc.getDocumentMeta().get("type");+ if (type == null) {+ type = defaultType;}- @Override- public void write(NutchDocument doc) throws IOException {- String id = (String) doc.getFieldValue("id");- String type = doc.getDocumentMeta().get("type");- if (type == null) {- type = defaultType;- }+ Map<String, Object> source = new HashMap<String, Object>();- Map<String, Object> source = new HashMap<String, Object>();-- // Loop through all fields of this doc- for (String fieldName : doc.getFieldNames()) {- if (doc.getField(fieldName).getValues().size() > 1) {- source.put(fieldName, doc.getFieldValue(fieldName));- // Loop through the values to keep track of the size of this- // document- for (Object value : doc.getField(fieldName).getValues()) {- bulkLength += value.toString().length();- }- } else {- source.put(fieldName, doc.getFieldValue(fieldName));- bulkLength += doc.getFieldValue(fieldName).toString().length();- }- }- Index indexRequest = new Index.Builder(source)- .index(defaultIndex)- .type(type)- .id(id)- .build();-- // Add this indexing request to a bulk request- bulkBuilder.addAction(indexRequest);-- indexedDocs++;- bulkDocs++;-- if (bulkDocs >= maxBulkDocs || bulkLength >= maxBulkLength) {- LOG.info("Processing bulk request [docs = " + bulkDocs + ", length = "- + bulkLength + ", total docs = " + indexedDocs- + ", last doc in bulk = '" + id + "']");- // Flush the bulk of indexing requests- createNewBulk = true;- commit();+ // Loop through all fields of this doc+ for (String fieldName : doc.getFieldNames()) {+ if (doc.getField(fieldName).getValues().size() > 1) {+ source.put(fieldName, doc.getFieldValue(fieldName));+ // Loop through the values to keep track of the size of this+ // document+ for (Object value : doc.getField(fieldName).getValues()) {+ bulkLength += value.toString().length();}+ } else {+ source.put(fieldName, doc.getFieldValue(fieldName));+ bulkLength += doc.getFieldValue(fieldName).toString().length();+ }}-- @Override- public void delete(String key) throws IOException {- try {- client.execute(new Delete.Builder(key)- .index(defaultIndex)- .type(defaultType)- .build());- } catch (IOException e) {- LOG.error(ExceptionUtils.getStackTrace(e));- throw e;- }-+ Index indexRequest = new Index.Builder(source).index(defaultIndex)+ .type(type).id(id).build();++ // Add this indexing request to a bulk request+ bulkBuilder.addAction(indexRequest);++ indexedDocs++;+ bulkDocs++;++ if (bulkDocs >= maxBulkDocs || bulkLength >= maxBulkLength) {+ LOG.info(+ "Processing bulk request [docs = {}, length = {}, total docs = {}, last doc in bulk = '{}']",+ bulkDocs, bulkLength, indexedDocs, id);+ // Flush the bulk of indexing requests+ createNewBulk = true;+ commit();}-- @Override- public void update(NutchDocument doc) throws IOException {- try {- write(doc);- } catch (IOException e) {- LOG.error(ExceptionUtils.getStackTrace(e));- throw e;- }+ }++ @Override+ public void delete(String key) throws IOException {+ try {+ client.execute(new Delete.Builder(key).index(defaultIndex)+ .type(defaultType).build());+ } catch (IOException e) {+ LOG.error(ExceptionUtils.getStackTrace(e));+ throw e;}- @Override- public void commit() throws IOException {- if (basicFuture != null) {- // wait for previous to finish- long beforeWait = System.currentTimeMillis();- try {- JestResult result = basicFuture.get();- if (result == null) {- throw new RuntimeException();- }- long msWaited = System.currentTimeMillis() - beforeWait;- LOG.info("Previous took in ms " + millis- + ", including wait " + msWaited);- } catch (InterruptedException | ExecutionException e) {- LOG.error("Error waiting for result ", e);- }- basicFuture = null;- }- if (bulkBuilder != null) {- if (bulkDocs > 0) {- // start a flush, note that this is an asynchronous call- basicFuture = new BasicFuture<>(null);- millis = System.currentTimeMillis();- client.executeAsync(bulkBuilder.build(), new JestResultHandler<BulkResult>() {- @Override- public void completed(BulkResult bulkResult) {- basicFuture.completed(bulkResult);- millis = System.currentTimeMillis() - millis;- }-- @Override- public void failed(Exception e) {- basicFuture.completed(null);- LOG.error("Failed result: ", e);- }- });- }- bulkBuilder = null;- }- if (createNewBulk) {- // Prepare a new bulk request- bulkBuilder = new Bulk.Builder()- .defaultIndex(defaultIndex)- .defaultType(defaultType);- bulkDocs = 0;- bulkLength = 0;+ }++ @Override+ public void update(NutchDocument doc) throws IOException {+ try {+ write(doc);+ } catch (IOException e) {+ LOG.error(ExceptionUtils.getStackTrace(e));+ throw e;+ }+ }++ @Override+ public void commit() throws IOException {+ if (basicFuture != null) {+ // wait for previous to finish+ long beforeWait = System.currentTimeMillis();+ try {+ JestResult result = basicFuture.get();+ if (result == null) {+ throw new RuntimeException();}+ long msWaited = System.currentTimeMillis() - beforeWait;+ LOG.info("Previous took in ms {}, including wait {}", millis, msWaited);+ } catch (InterruptedException | ExecutionException e) {+ LOG.error("Error waiting for result ", e);+ }+ basicFuture = null;}-- @Override- public void close() throws IOException {- // Flush pending requests- LOG.info("Processing remaining requests [docs = " + bulkDocs- + ", length = " + bulkLength + ", total docs = " + indexedDocs + "]");- createNewBulk = false;- commit();- // flush one more time to finalize the last bulk- LOG.info("Processing to finalize last execute");- createNewBulk = false;- commit();-- // Close- client.shutdownClient();+ if (bulkBuilder != null) {+ if (bulkDocs > 0) {+ // start a flush, note that this is an asynchronous call+ basicFuture = new BasicFuture<>(null);+ millis = System.currentTimeMillis();+ client.executeAsync(bulkBuilder.build(),+ new JestResultHandler<BulkResult>() {+ @Override+ public void completed(BulkResult bulkResult) {+ basicFuture.completed(bulkResult);+ millis = System.currentTimeMillis() - millis;+ }++ @Override+ public void failed(Exception e) {+ basicFuture.completed(null);+ LOG.error("Failed result: ", e);+ }+ });+ }+ bulkBuilder = null;}-- @Override- public String describe() {- StringBuffer sb = new StringBuffer("ElasticRestIndexWriter\n");- sb.append("\t").append(ElasticRestConstants.HOST).append(" : hostname\n");- sb.append("\t").append(ElasticRestConstants.PORT).append(" : port\n");- sb.append("\t").append(ElasticRestConstants.INDEX)- .append(" : elastic index command \n");- sb.append("\t").append(ElasticRestConstants.MAX_BULK_DOCS)- .append(" : elastic bulk index doc counts. (default 250) \n");- sb.append("\t").append(ElasticRestConstants.MAX_BULK_LENGTH)- .append(" : elastic bulk index length. (default 2500500 ~2.5MB)\n");- return sb.toString();+ if (createNewBulk) {+ // Prepare a new bulk request+ bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex)+ .defaultType(defaultType);+ bulkDocs = 0;+ bulkLength = 0;}-- @Override- public void setConf(Configuration conf) {- config = conf;- String host = conf.get(ElasticRestConstants.HOST);-- if (StringUtils.isBlank(host)) {- String message = "Missing elastic.host. It should be set in nutch-site.xml ";- message += "\n" + describe();- LOG.error(message);- throw new RuntimeException(message);- }+ }++ @Override+ public void close() throws IOException {+ // Flush pending requests+ LOG.info(+ "Processing remaining requests [docs = {}, length = {}, total docs = {}]",+ bulkDocs, bulkLength, indexedDocs);+ createNewBulk = false;+ commit();++ // flush one more time to finalize the last bulk+ LOG.info("Processing to finalize last execute");+ createNewBulk = false;+ commit();++ // Close+ client.shutdownClient();+ }++ @Override+ public String describe() {+ StringBuffer sb = new StringBuffer("ElasticRestIndexWriter\n");+ sb.append("\t").append(ElasticRestConstants.HOST).append(" : hostname\n");+ sb.append("\t").append(ElasticRestConstants.PORT).append(" : port\n");+ sb.append("\t").append(ElasticRestConstants.INDEX)+ .append(" : elastic index command \n");+ sb.append("\t").append(ElasticRestConstants.MAX_BULK_DOCS)+ .append(" : elastic bulk index doc counts. (default 250) \n");+ sb.append("\t").append(ElasticRestConstants.MAX_BULK_LENGTH)+ .append(" : elastic bulk index length. (default 2500500 ~2.5MB)\n");+ return sb.toString();+ }++ @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- public Configuration getConf() {- return config;- }+ @Override+ public Configuration getConf() {+ return config;+ }}\ No newline at end of filediff --git a/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/package-info.java b/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/package-info.javaindex fc1445116..e357cc962 100644--- a/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/package-info.java+++ b/src/plugin/indexer-elastic-rest/src/java/org/apache/nutch/indexwriter/elasticrest/package-info.java@@ -16,7 +16,7 @@*//**- * Index writer plugin for <a href="http://www.elasticsearch.org/">Elasticsearch</a>.+ * Rest based index writer plugin for <a href="http://www.elasticsearch.org/">Elasticsearch</a>.*/package org.apache.nutch.indexwriter.elasticrest;