Search in sources :

Example 31 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project play2-elasticsearch by cleverage.

the class IndexClient method start.

public void start() throws Exception {
    // Load Elasticsearch Settings
    Settings.Builder settings = loadSettings();
    // Check Model
    if (this.isLocalMode()) {
        Logger.info("ElasticSearch : Starting in Local Mode");
        NodeBuilder nb = nodeBuilder().settings(settings).local(true).client(false).data(true);
        node = nb.node();
        client = node.client();
        Logger.info("ElasticSearch : Started in Local Mode");
    } else {
        Logger.info("ElasticSearch : Starting in Client Mode");
        TransportClient c = TransportClient.builder().settings(settings).build();
        if (config.client == null) {
            throw new Exception("Configuration required - elasticsearch.client when local model is disabled!");
        }
        String[] hosts = config.client.trim().split(",");
        boolean done = false;
        for (String host : hosts) {
            String[] parts = host.split(":");
            if (parts.length != 2) {
                throw new Exception("Invalid Host: " + host);
            }
            Logger.info("ElasticSearch : Client - Host: " + parts[0] + " Port: " + parts[1]);
            c.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(parts[0]), Integer.valueOf(parts[1])));
            done = true;
        }
        if (!done) {
            throw new Exception("No Hosts Provided for ElasticSearch!");
        }
        client = c;
        Logger.info("ElasticSearch : Started in Client Mode");
    }
    // Check Client
    if (client == null) {
        throw new Exception("ElasticSearch Client cannot be null - please check the configuration provided and the health of your ElasticSearch instances.");
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) NodeBuilder(org.elasticsearch.node.NodeBuilder) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) SettingsException(org.elasticsearch.common.settings.SettingsException)

Aggregations

TransportClient (org.elasticsearch.client.transport.TransportClient)31 Settings (org.elasticsearch.common.settings.Settings)17 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)15 TransportAddress (org.elasticsearch.common.transport.TransportAddress)8 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)7 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)7 Client (org.elasticsearch.client.Client)5 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Test (org.junit.Test)3 Settings (org.sonar.api.config.Settings)3 File (java.io.File)2 String.format (java.lang.String.format)2 InetAddress (java.net.InetAddress)2 Logger (org.apache.log4j.Logger)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Condition (org.assertj.core.api.Condition)2 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)2 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)2 Node (org.elasticsearch.node.Node)2