com.ottogroup.bi.asap.operator.elasticsearch.producer.ElasticSearchWriter.java Source code

Java tutorial

Introduction

Here is the source code for com.ottogroup.bi.asap.operator.elasticsearch.producer.ElasticSearchWriter.java

Source

/**
 * Copyright 2014 Otto (GmbH & Co KG)
 *
 * Licensed 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.ottogroup.bi.asap.operator.elasticsearch.producer;

import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import com.ottogroup.bi.asap.component.ComponentType;
import com.ottogroup.bi.asap.component.annotation.AsapComponent;
import com.ottogroup.bi.asap.component.emitter.Emitter;
import com.ottogroup.bi.asap.exception.RequiredInputMissingException;
import com.ottogroup.bi.asap.message.StreamingDataMessage;

/**
 * Writes all incoming {@link StreamingDataMessage} to a configured ElasticSearch instance
 * @author mnxfst
 * @since Dec 1, 2014
 *
 */
@AsapComponent(type = ComponentType.EMITTER, name = "elasticSearchWriter", version = "0.0.1", description = "Writes events to ElasticSearch")
public class ElasticSearchWriter implements Emitter {

    private static final Logger logger = Logger.getLogger(ElasticSearchWriter.class);

    public static final String CFG_ES_HOST = "elasticsearch.host";
    public static final String CFG_ES_PORT = "elasticsearch.port";
    public static final String CFG_ES_CLUSTER = "elasticsearch.cluster";
    public static final String CFG_ES_INDEX = "elasticsearch.index";
    public static final String CFG_ES_DOCUMENT_TYPE = "elasticsearch.documentType";

    ///////////////////////////////////////////////////////
    // elasticsearch connection settings
    private String esHost = null;
    private int esPort = 0;
    private String esCluster = null;
    private String esIndex = null;
    private String esDocumentType = null;
    //
    ///////////////////////////////////////////////////////

    /** unique component identifier (pipeline scope) */
    private String componentId = null;
    /** number messages processed by this instance */
    private long numProcessedMessages = 0;
    /** elasticsearch client */
    private TransportClient elasticSearchClient;

    /**
     * @see com.ottogroup.bi.asap.component.Component#init(java.util.Properties)
     */
    public void init(Properties properties) throws RequiredInputMissingException {

        /////////////////////////////////////////////////////////////////////////
        // extract and validate configuration 
        this.esHost = properties.getProperty(CFG_ES_HOST);
        if (StringUtils.isBlank(esHost))
            throw new RequiredInputMissingException("Missing required value for parameter '" + CFG_ES_HOST + "'");

        try {
            this.esPort = Integer.parseInt(StringUtils.trim(properties.getProperty(CFG_ES_PORT)));
        } catch (Exception e) {
            throw new RequiredInputMissingException("Missing required value for parameter '" + CFG_ES_PORT + "'");
        }
        if (this.esPort < 1)
            throw new RequiredInputMissingException(
                    "Invalid value found for required parameter '" + CFG_ES_PORT + "': " + this.esPort);

        this.esCluster = properties.getProperty(CFG_ES_CLUSTER);
        if (StringUtils.isBlank(esCluster))
            throw new RequiredInputMissingException(
                    "Missing required value for parameter '" + CFG_ES_CLUSTER + "'");

        this.esIndex = properties.getProperty(CFG_ES_INDEX);
        if (StringUtils.isBlank(esIndex))
            throw new RequiredInputMissingException("Missing required value for parameter '" + CFG_ES_INDEX + "'");

        this.esDocumentType = properties.getProperty(CFG_ES_DOCUMENT_TYPE);
        if (StringUtils.isBlank(esDocumentType))
            throw new RequiredInputMissingException(
                    "Missing required value for parameter '" + CFG_ES_DOCUMENT_TYPE + "'");
        //
        /////////////////////////////////////////////////////////////////////////

        logger.info("Trying to establish a connection with es server [host=" + esHost + ", port=" + esPort
                + ", cluster=" + esCluster + ", index=" + esIndex + ", docType=" + esDocumentType + "]");

        ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder();
        settingsBuilder.put("cluster.name", this.esCluster);
        settingsBuilder.put("name", this.esCluster);
        Settings settings = settingsBuilder.build();
        logger.info("Settings created");
        this.elasticSearchClient = new TransportClient(settings);
        logger.info("client instantiated");
        this.elasticSearchClient.addTransportAddress(new InetSocketTransportAddress(this.esHost, this.esPort));
        logger.info("Connected");
    }

    /**
     * @see com.ottogroup.bi.asap.component.emitter.Emitter#onMessage(com.ottogroup.bi.asap.message.StreamingDataMessage)
     */
    public void onMessage(StreamingDataMessage message) {
        if (message != null && StringUtils.isNotBlank(message.getBody())) {
            logger.info(message.getBody());
            try {
                this.elasticSearchClient.prepareIndex(this.esIndex, this.esDocumentType)
                        .setSource(message.getBody()).execute().actionGet();
                this.numProcessedMessages++;
            } catch (Exception e) {
                logger.error("Failed to write event to ElasticSearch instance [host=" + this.esHost + ", port="
                        + this.esPort + ", cluster=" + this.esCluster + ", index=" + this.esIndex
                        + ", documentType=" + this.esDocumentType + ", error=" + e.getMessage() + "]");
            }
        }
    }

    /**
     * @see com.ottogroup.bi.asap.node.pipeline.component.DataComponent#shutdown()
     */
    public boolean shutdown() {
        try {
            this.elasticSearchClient.close();
        } catch (Exception e) {
            logger.error("Error while closing elasticsearch client connection. Error: " + e.getMessage());
        }
        return true;
    }

    /**
     * @see com.ottogroup.bi.asap.component.Component#getId()
     */
    public String getId() {
        return this.componentId;
    }

    /**
     * @see com.ottogroup.bi.asap.component.Component#setId(java.lang.String)
     */
    public void setId(String id) {
        this.componentId = id;
    }

    /**
     * @see com.ottogroup.bi.asap.component.Component#getTotalNumOfMessages()
     */
    public long getTotalNumOfMessages() {
        return this.numProcessedMessages;
    }

}