/** * Licensed to DigitalPebble Ltd under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * DigitalPebble 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 com.digitalpebble.stormcrawler.elasticsearch.bolt; import static com.digitalpebble.stormcrawler.Constants.StatusStreamName; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import java.io.IOException; import java.util.Iterator; import java.util.Map; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.common.xcontent.XContentBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.digitalpebble.stormcrawler.Metadata; import com.digitalpebble.stormcrawler.elasticsearch.ElasticSearchConnection; import com.digitalpebble.stormcrawler.indexing.AbstractIndexerBolt; import com.digitalpebble.stormcrawler.persistence.Status; import com.digitalpebble.stormcrawler.util.ConfUtils; import org.apache.storm.metric.api.MultiCountMetric; import org.apache.storm.task.OutputCollector; import org.apache.storm.task.TopologyContext; import org.apache.storm.tuple.Tuple; import org.apache.storm.tuple.Values; /** * Sends documents to ElasticSearch. Indexes all the fields from the tuples or a * Map <String,Object> from a named field. */ @SuppressWarnings("serial") public class IndexerBolt extends AbstractIndexerBolt { private static final Logger LOG = LoggerFactory .getLogger(IndexerBolt.class); private static final String ESBoltType = "indexer"; static final String ESIndexNameParamName = "es.indexer.index.name"; static final String ESDocTypeParamName = "es.indexer.doc.type"; private static final String ESCreateParamName = "es.indexer.create"; private OutputCollector _collector; private String indexName; private String docType; private boolean create = false; private MultiCountMetric eventCounter; private ElasticSearchConnection connection; @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void prepare(Map conf, TopologyContext context, OutputCollector collector) { super.prepare(conf, context, collector); _collector = collector; indexName = ConfUtils.getString(conf, IndexerBolt.ESIndexNameParamName, "fetcher"); docType = ConfUtils.getString(conf, IndexerBolt.ESDocTypeParamName, "doc"); create = ConfUtils.getBoolean(conf, IndexerBolt.ESCreateParamName, false); try { connection = ElasticSearchConnection .getConnection(conf, ESBoltType); } catch (Exception e1) { LOG.error("Can't connect to ElasticSearch", e1); throw new RuntimeException(e1); } this.eventCounter = context.registerMetric("ElasticSearchIndexer", new MultiCountMetric(), 10); } @Override public void cleanup() { if (connection != null) connection.close(); } @Override public void execute(Tuple tuple) { String url = tuple.getStringByField("url"); // Distinguish the value used for indexing // from the one used for the status String normalisedurl = valueForURL(tuple); Metadata metadata = (Metadata) tuple.getValueByField("metadata"); String text = tuple.getStringByField("text"); boolean keep = filterDocument(metadata); if (!keep) { eventCounter.scope("Filtered").incrBy(1); // treat it as successfully processed even if // we do not index it _collector.emit(StatusStreamName, tuple, new Values(url, metadata, Status.FETCHED)); _collector.ack(tuple); return; } try { XContentBuilder builder = jsonBuilder().startObject(); // display text of the document? if (fieldNameForText() != null) { builder.field(fieldNameForText(), text); } // send URL as field? if (fieldNameForURL() != null) { builder.field(fieldNameForURL(), normalisedurl); } // which metadata to display? Map<String, String[]> keyVals = filterMetadata(metadata); Iterator<String> iterator = keyVals.keySet().iterator(); while (iterator.hasNext()) { String fieldName = iterator.next(); String[] values = keyVals.get(fieldName); if (values.length == 1) { builder.field(fieldName, values[0]); } else if (values.length > 1) { builder.array(fieldName, values); } } builder.endObject(); String sha256hex = org.apache.commons.codec.digest.DigestUtils .sha256Hex(normalisedurl); IndexRequestBuilder request = connection.getClient() .prepareIndex(indexName, docType).setSource(builder) .setId(sha256hex); // set create? request.setCreate(create); connection.getProcessor().add(request.request()); eventCounter.scope("Indexed").incrBy(1); _collector.emit(StatusStreamName, tuple, new Values(url, metadata, Status.FETCHED)); _collector.ack(tuple); } catch (IOException e) { LOG.error("Error sending log tuple to ES", e); // do not send to status stream so that it gets replayed _collector.fail(tuple); } } }