Search in sources :

Example 31 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project jena by apache.

the class BaseESTest method setupTransportClient.

/**
     * Make sure that we have connectivity to the locally running ES node.
     * The ES is started during the pre-integration-test phase
     */
@BeforeClass
public static void setupTransportClient() {
    Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
    transportClient = new PreBuiltTransportClient(settings);
    try {
        transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ADDRESS), PORT));
    } catch (UnknownHostException ex) {
        Assert.fail("Failed to create transport client" + ex.getMessage());
    }
    classToTest = new TextIndexES(config(), transportClient, INDEX_NAME);
    Assert.assertNotNull("Transport client was not created successfully", transportClient);
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) UnknownHostException(java.net.UnknownHostException) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) TextIndexES(org.apache.jena.query.text.es.TextIndexES) BeforeClass(org.junit.BeforeClass)

Example 32 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project samza by apache.

the class TransportClientFactory method getClient.

@Override
public Client getClient() {
    Settings settings = Settings.settingsBuilder().put(clientSettings).build();
    TransportAddress address = new InetSocketTransportAddress(new InetSocketAddress(transportHost, transportPort));
    return TransportClient.builder().settings(settings).build().addTransportAddress(address);
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) InetSocketAddress(java.net.InetSocketAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 33 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project API by ca-cwds.

the class DataAccessModule method elasticsearchClient.

// @Singleton
@Provides
public synchronized Client elasticsearchClient(ApiConfiguration apiConfiguration) {
    if (client == null) {
        ElasticsearchConfiguration config = apiConfiguration.getElasticsearchConfiguration();
        try {
            // Settings settings = Settings.settingsBuilder()
            // .put("cluster.name", config.getElasticsearchCluster()).build();
            // client = TransportClient.builder().settings(settings).build().addTransportAddress(
            // new InetSocketTransportAddress(InetAddress.getByName(config.getElasticsearchHost()),
            // Integer.parseInt(config.getElasticsearchPort())));
            TransportClient ret = new PreBuiltTransportClient(Settings.builder().put("cluster.name", config.getElasticsearchCluster()).build());
            ret.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(config.getElasticsearchHost()), Integer.parseInt(config.getElasticsearchPort())));
            client = ret;
        } catch (Exception e) {
            LOGGER.error("Error initializing Elasticsearch client: {}", e.getMessage(), e);
            throw new ApiException("Error initializing Elasticsearch client: " + e.getMessage(), e);
        }
    }
    return client;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ElasticsearchConfiguration(gov.ca.cwds.rest.ElasticsearchConfiguration) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) ApiException(gov.ca.cwds.rest.api.ApiException) ApiException(gov.ca.cwds.rest.api.ApiException) Provides(com.google.inject.Provides)

Example 34 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project fess by codelibs.

the class FessEsClient method open.

@PostConstruct
public void open() {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String transportAddressesValue = System.getProperty(Constants.FESS_ES_TRANSPORT_ADDRESSES);
    if (StringUtil.isNotBlank(transportAddressesValue)) {
        for (final String transportAddressValue : transportAddressesValue.split(",")) {
            final String[] addressPair = transportAddressValue.trim().split(":");
            if (addressPair.length < 3) {
                final String host = addressPair[0];
                int port = 9300;
                if (addressPair.length == 2) {
                    port = Integer.parseInt(addressPair[1]);
                }
                addTransportAddress(host, port);
            } else {
                logger.warn("Invalid address format: " + transportAddressValue);
            }
        }
    }
    if (transportAddressList.isEmpty()) {
        if (runner == null) {
            runner = new ElasticsearchClusterRunner();
            final Configs config = newConfigs().clusterName(fessConfig.getElasticsearchClusterName()).numOfNode(1).useLogger();
            final String esDir = System.getProperty("fess.es.dir");
            if (esDir != null) {
                config.basePath(esDir);
            }
            config.disableESLogger();
            runner.onBuild((number, settingsBuilder) -> {
                final File pluginDir = new File(esDir, "plugins");
                if (pluginDir.isDirectory()) {
                    settingsBuilder.put("path.plugins", pluginDir.getAbsolutePath());
                } else {
                    settingsBuilder.put("path.plugins", new File(System.getProperty("user.dir"), "plugins").getAbsolutePath());
                }
                if (settings != null) {
                    settingsBuilder.put(settings);
                }
            });
            runner.build(config);
        }
        client = runner.client();
        addTransportAddress("localhost", runner.node().settings().getAsInt("transport.tcp.port", 9300));
    } else {
        final Builder settingsBuilder = Settings.builder();
        settingsBuilder.put("cluster.name", fessConfig.getElasticsearchClusterName());
        settingsBuilder.put("client.transport.sniff", fessConfig.isElasticsearchTransportSniff());
        settingsBuilder.put("client.transport.ping_timeout", fessConfig.getElasticsearchTransportPingTimeout());
        settingsBuilder.put("client.transport.nodes_sampler_interval", fessConfig.getElasticsearchTransportNodesSamplerInterval());
        final Settings settings = settingsBuilder.build();
        final TransportClient transportClient = new PreBuiltTransportClient(settings);
        for (final TransportAddress address : transportAddressList) {
            transportClient.addTransportAddress(address);
        }
        client = transportClient;
    }
    if (StringUtil.isBlank(transportAddressesValue)) {
        final StringBuilder buf = new StringBuilder();
        for (final TransportAddress transportAddress : transportAddressList) {
            if (transportAddress instanceof InetSocketTransportAddress) {
                if (buf.length() > 0) {
                    buf.append(',');
                }
                final InetSocketTransportAddress inetTransportAddress = (InetSocketTransportAddress) transportAddress;
                buf.append(inetTransportAddress.address().getHostName());
                buf.append(':');
                buf.append(inetTransportAddress.address().getPort());
            }
        }
        if (buf.length() > 0) {
            System.setProperty(Constants.FESS_ES_TRANSPORT_ADDRESSES, buf.toString());
        }
    }
    waitForYellowStatus();
    indexConfigList.forEach(configName -> {
        final String[] values = configName.split("/");
        if (values.length == 2) {
            final String configIndex = values[0];
            final String configType = values[1];
            final String indexName;
            final boolean isFessIndex = configIndex.equals("fess");
            if (isFessIndex) {
                indexName = fessConfig.getIndexDocumentUpdateIndex();
            } else {
                indexName = configIndex;
            }
            boolean exists = existsIndex(indexName);
            if (!exists) {
                final String createdIndexName;
                if (isFessIndex) {
                    createdIndexName = generateNewIndexName(configIndex);
                } else {
                    createdIndexName = configIndex;
                }
                createIndex(configIndex, configType, createdIndexName);
                createAlias(configIndex, createdIndexName);
            }
            final String updatedIndexName;
            if (isFessIndex) {
                client.admin().cluster().prepareHealth(fessConfig.getIndexDocumentUpdateIndex()).setWaitForYellowStatus().execute().actionGet(fessConfig.getIndexIndicesTimeout());
                final GetIndexResponse response = client.admin().indices().prepareGetIndex().addIndices(fessConfig.getIndexDocumentUpdateIndex()).execute().actionGet(fessConfig.getIndexIndicesTimeout());
                final String[] indices = response.indices();
                if (indices.length == 1) {
                    updatedIndexName = indices[0];
                } else {
                    updatedIndexName = configIndex;
                }
            } else {
                updatedIndexName = configIndex;
            }
            addMapping(configIndex, configType, updatedIndexName);
        } else {
            logger.warn("Invalid index config name: " + configName);
        }
    });
}
Also used : ElasticsearchClusterRunner(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder) TermVectorsRequestBuilder(org.elasticsearch.action.termvectors.TermVectorsRequestBuilder) ClearScrollRequestBuilder(org.elasticsearch.action.search.ClearScrollRequestBuilder) ExplainRequestBuilder(org.elasticsearch.action.explain.ExplainRequestBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) MultiTermVectorsRequestBuilder(org.elasticsearch.action.termvectors.MultiTermVectorsRequestBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) FieldCapabilitiesRequestBuilder(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) FieldStatsRequestBuilder(org.elasticsearch.action.fieldstats.FieldStatsRequestBuilder) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) IndicesAliasesRequestBuilder(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchScrollRequestBuilder(org.elasticsearch.action.search.SearchScrollRequestBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) ElasticsearchClusterRunner.newConfigs(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs) Configs(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.Configs) File(java.io.File) Settings(org.elasticsearch.common.settings.Settings) PostConstruct(javax.annotation.PostConstruct)

Example 35 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress 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

InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)35 TransportClient (org.elasticsearch.client.transport.TransportClient)15 Settings (org.elasticsearch.common.settings.Settings)14 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)7 TransportAddress (org.elasticsearch.common.transport.TransportAddress)7 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)7 InetSocketAddress (java.net.InetSocketAddress)5 UnknownHostException (java.net.UnknownHostException)5 IOException (java.io.IOException)4 InetAddress (java.net.InetAddress)3 ArrayList (java.util.ArrayList)3 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)3 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Logger (org.apache.log4j.Logger)2 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)2