Search in sources :

Example 1 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project elasticsearch by elastic.

the class Netty4TransportMultiPortIntegrationIT method testThatTransportClientCanConnect.

public void testThatTransportClientCanConnect() throws Exception {
    Settings settings = Settings.builder().put("cluster.name", internalCluster().getClusterName()).put(NetworkModule.TRANSPORT_TYPE_KEY, Netty4Plugin.NETTY_TRANSPORT_NAME).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    try (TransportClient transportClient = new MockTransportClient(settings, Netty4Plugin.class)) {
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), randomPort));
        ClusterHealthResponse response = transportClient.admin().cluster().prepareHealth().get();
        assertThat(response.getStatus(), is(ClusterHealthStatus.GREEN));
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) MockTransportClient(org.elasticsearch.transport.MockTransportClient) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) MockTransportClient(org.elasticsearch.transport.MockTransportClient) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project elasticsearch by elastic.

the class ESSmokeClientTestCase method startClient.

private static Client startClient(Path tempDir, TransportAddress... transportAddresses) {
    Settings.Builder builder = Settings.builder().put("node.name", "qa_smoke_client_" + counter.getAndIncrement()).put("client.transport.ignore_cluster_name", true).put(Environment.PATH_HOME_SETTING.getKey(), tempDir);
    final Collection<Class<? extends Plugin>> plugins;
    if (random().nextBoolean()) {
        builder.put(NetworkModule.TRANSPORT_TYPE_KEY, MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME);
        plugins = Collections.singleton(MockTcpTransportPlugin.class);
    } else {
        plugins = Collections.emptyList();
    }
    TransportClient client = new PreBuiltTransportClient(builder.build(), plugins).addTransportAddresses(transportAddresses);
    logger.info("--> Elasticsearch Java TransportClient started");
    Exception clientException = null;
    try {
        ClusterHealthResponse health = client.admin().cluster().prepareHealth().get();
        logger.info("--> connected to [{}] cluster which is running [{}] node(s).", health.getClusterName(), health.getNumberOfNodes());
    } catch (Exception e) {
        clientException = e;
    }
    assumeNoException("Sounds like your cluster is not running at " + clusterAddresses, clientException);
    return client;
}
Also used : MockTcpTransportPlugin(org.elasticsearch.transport.MockTcpTransportPlugin) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) Settings(org.elasticsearch.common.settings.Settings) IOException(java.io.IOException) MockTcpTransportPlugin(org.elasticsearch.transport.MockTcpTransportPlugin) Plugin(org.elasticsearch.plugins.Plugin)

Example 3 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project elasticsearch by elastic.

the class PreBuiltTransportClientTests method testPluginInstalled.

@Test
public void testPluginInstalled() {
    try (TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)) {
        Settings settings = client.settings();
        assertEquals(Netty4Plugin.NETTY_TRANSPORT_NAME, NetworkModule.HTTP_DEFAULT_TYPE_SETTING.get(settings));
        assertEquals(Netty4Plugin.NETTY_TRANSPORT_NAME, NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.get(settings));
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 4 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project qi4j-sdk by Qi4j.

the class ESClusterSupport method activateElasticSearch.

@Override
protected void activateElasticSearch() throws Exception {
    configuration.refresh();
    ElasticSearchClusterConfiguration config = configuration.get();
    String clusterName = config.clusterName().get() == null ? DEFAULT_CLUSTER_NAME : config.clusterName().get();
    index = config.index().get() == null ? DEFAULT_INDEX_NAME : config.index().get();
    indexNonAggregatedAssociations = config.indexNonAggregatedAssociations().get();
    String[] nodes = config.nodes().get() == null ? new String[] { "localhost:9300" } : config.nodes().get().split(",");
    boolean clusterSniff = config.clusterSniff().get();
    boolean ignoreClusterName = config.ignoreClusterName().get();
    String pingTimeout = config.pingTimeout().get() == null ? "5s" : config.pingTimeout().get();
    String samplerInterval = config.samplerInterval().get() == null ? "5s" : config.samplerInterval().get();
    Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).put("client.transport.sniff", clusterSniff).put("client.transport.ignore_cluster_name", ignoreClusterName).put("client.transport.ping_timeout", pingTimeout).put("client.transport.nodes_sampler_interval", samplerInterval).build();
    TransportClient transportClient = new TransportClient(settings);
    for (String node : nodes) {
        String[] split = node.split(":");
        String host = split[0];
        int port = Integer.valueOf(split[1]);
        transportClient.addTransportAddress(new InetSocketTransportAddress(host, port));
    }
    client = transportClient;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) ElasticSearchClusterConfiguration(org.qi4j.index.elasticsearch.ElasticSearchClusterConfiguration) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) ImmutableSettings(org.elasticsearch.common.settings.ImmutableSettings)

Example 5 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project sonarqube by SonarSource.

the class EsClientProvider method provide.

public EsClient provide(Settings settings) {
    if (cache == null) {
        TransportClient nativeClient;
        org.elasticsearch.common.settings.Settings.Builder esSettings = org.elasticsearch.common.settings.Settings.builder();
        // mandatory property defined by bootstrap process
        esSettings.put("cluster.name", settings.getString(ProcessProperties.CLUSTER_NAME));
        boolean clusterEnabled = settings.getBoolean(ProcessProperties.CLUSTER_ENABLED);
        if (clusterEnabled && settings.getBoolean(ProcessProperties.CLUSTER_SEARCH_DISABLED)) {
            esSettings.put("client.transport.sniff", true);
            nativeClient = TransportClient.builder().settings(esSettings).build();
            Arrays.stream(settings.getStringArray(ProcessProperties.CLUSTER_SEARCH_HOSTS)).map(Host::parse).forEach(h -> h.addTo(nativeClient));
            LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(nativeClient));
        } else {
            nativeClient = TransportClient.builder().settings(esSettings).build();
            Host host = new Host(settings.getString(ProcessProperties.SEARCH_HOST), settings.getInt(ProcessProperties.SEARCH_PORT));
            host.addTo(nativeClient);
            LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(nativeClient));
        }
        cache = new EsClient(nativeClient);
    }
    return cache;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) Settings(org.sonar.api.config.Settings)

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