Search in sources :

Example 26 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project elasticsearch-suggest-plugin by spinscale.

the class TransportClientTest method getTransportClient.

private TransportClient getTransportClient() {
    if (transportClient == null) {
        transportClient = new TransportClient(settingsBuilder().put("cluster.name", internalCluster().getClusterName()).put("name", "programmatic_transport_client").put("client.transport.nodes_sampler_interval", "1s").build());
        Transport transport = internalCluster().getDataNodeInstance(Transport.class);
        transportClient.addTransportAddress(transport.boundAddress().publishAddress());
    }
    return transportClient;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) Transport(org.elasticsearch.transport.Transport)

Example 27 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project titan by thinkaurelius.

the class ElasticSearchIndex method legacyConfiguration.

/**
     * Configure ElasticSearchIndex's ES client according to 0.4.x - 0.5.0 semantics.
     * This checks local-mode first.  If local-mode is true, then it creates a Node that
     * uses JVM local transport and can't talk over the network.  If local-mode is
     * false, then it creates a TransportClient that can talk over the network and
     * uses {@link com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration#INDEX_HOSTS}
     * as the server addresses.  Note that this configuration method
     * does not allow creating a Node that talks over the network.
     * <p>
     * This is activated by <b>not</b> setting an explicit value for {@link #INTERFACE} in the
     * Titan configuration.
     *
     * @see #interfaceConfiguration(com.thinkaurelius.titan.diskstorage.configuration.Configuration)
     * @param config a config passed to ElasticSearchIndex's constructor
     * @return a node and client object open and ready for use
     */
private ElasticSearchSetup.Connection legacyConfiguration(Configuration config) {
    Node node;
    Client client;
    if (config.get(LOCAL_MODE)) {
        log.debug("Configuring ES for JVM local transport");
        boolean clientOnly = config.get(CLIENT_ONLY);
        boolean local = config.get(LOCAL_MODE);
        NodeBuilder builder = NodeBuilder.nodeBuilder();
        Preconditions.checkArgument(config.has(INDEX_CONF_FILE) || config.has(INDEX_DIRECTORY), "Must either configure configuration file or base directory");
        if (config.has(INDEX_CONF_FILE)) {
            String configFile = config.get(INDEX_CONF_FILE);
            ImmutableSettings.Builder sb = ImmutableSettings.settingsBuilder();
            log.debug("Configuring ES from YML file [{}]", configFile);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(configFile);
                sb.loadFromStream(configFile, fis);
                builder.settings(sb.build());
            } catch (FileNotFoundException e) {
                throw new TitanException(e);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        } else {
            String dataDirectory = config.get(INDEX_DIRECTORY);
            log.debug("Configuring ES with data directory [{}]", dataDirectory);
            File f = new File(dataDirectory);
            if (!f.exists())
                f.mkdirs();
            ImmutableSettings.Builder b = ImmutableSettings.settingsBuilder();
            for (String sub : DATA_SUBDIRS) {
                String subdir = dataDirectory + File.separator + sub;
                f = new File(subdir);
                if (!f.exists())
                    f.mkdirs();
                b.put("path." + sub, subdir);
            }
            b.put("script.disable_dynamic", false);
            b.put("indices.ttl.interval", "5s");
            builder.settings(b.build());
            String clustername = config.get(CLUSTER_NAME);
            Preconditions.checkArgument(StringUtils.isNotBlank(clustername), "Invalid cluster name: %s", clustername);
            builder.clusterName(clustername);
        }
        node = builder.client(clientOnly).data(!clientOnly).local(local).node();
        client = node.client();
    } else {
        log.debug("Configuring ES for network transport");
        ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        if (config.has(CLUSTER_NAME)) {
            String clustername = config.get(CLUSTER_NAME);
            Preconditions.checkArgument(StringUtils.isNotBlank(clustername), "Invalid cluster name: %s", clustername);
            settings.put("cluster.name", clustername);
        } else {
            settings.put("client.transport.ignore_cluster_name", true);
        }
        log.debug("Transport sniffing enabled: {}", config.get(CLIENT_SNIFF));
        settings.put("client.transport.sniff", config.get(CLIENT_SNIFF));
        settings.put("script.disable_dynamic", false);
        TransportClient tc = new TransportClient(settings.build());
        int defaultPort = config.has(INDEX_PORT) ? config.get(INDEX_PORT) : HOST_PORT_DEFAULT;
        for (String host : config.get(INDEX_HOSTS)) {
            String[] hostparts = host.split(":");
            String hostname = hostparts[0];
            int hostport = defaultPort;
            if (hostparts.length == 2)
                hostport = Integer.parseInt(hostparts[1]);
            log.info("Configured remote host: {} : {}", hostname, hostport);
            tc.addTransportAddress(new InetSocketTransportAddress(hostname, hostport));
        }
        client = tc;
        node = null;
    }
    return new ElasticSearchSetup.Connection(node, client);
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) Node(org.elasticsearch.node.Node) FileNotFoundException(java.io.FileNotFoundException) NodeBuilder(org.elasticsearch.node.NodeBuilder) ImmutableSettings(org.elasticsearch.common.settings.ImmutableSettings) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) FileInputStream(java.io.FileInputStream) TitanException(com.thinkaurelius.titan.core.TitanException) TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) File(java.io.File)

Example 28 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project metacat by Netflix.

the class ElasticSearchConfig method elasticSearchClient.

/**
     * The ElasticSearch client.
     *
     * @param config System config
     * @return Configured client or error
     */
@Bean
public Client elasticSearchClient(final Config config) {
    final String clusterName = config.getElasticSearchClusterName();
    if (StringUtils.isBlank(clusterName)) {
        throw new IllegalStateException("No cluster name set. Unable to continue");
    }
    final Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).put("transport.tcp.connect_timeout", "60s").build();
    final Client client = new TransportClient(settings);
    // Add the transport address if exists
    final String clusterNodesStr = config.getElasticSearchClusterNodes();
    if (StringUtils.isNotBlank(clusterNodesStr)) {
        final int port = config.getElasticSearchClusterPort();
        final Iterable<String> clusterNodes = Splitter.on(',').split(clusterNodesStr);
        clusterNodes.forEach(clusterNode -> ((TransportClient) client).addTransportAddress(new InetSocketTransportAddress(clusterNode, port)));
    }
    return client;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) TransportClient(org.elasticsearch.client.transport.TransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) ImmutableSettings(org.elasticsearch.common.settings.ImmutableSettings) Settings(org.elasticsearch.common.settings.Settings) Bean(org.springframework.context.annotation.Bean)

Example 29 with TransportClient

use of org.elasticsearch.client.transport.TransportClient 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 30 with TransportClient

use of org.elasticsearch.client.transport.TransportClient 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)

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