Search in sources :

Example 36 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project bibot by alfintech.

the class ElasticReader method initialise.

@PostConstruct
public void initialise() throws IOException {
    Settings settings = Settings.builder().put("cluster.name", clusterName).put("node.name", nodeName).build();
    client = new PreBuiltTransportClient(settings);
    client.addTransportAddress(new TransportAddress(InetAddress.getByName(elasticHost), elasticPort));
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings) PostConstruct(javax.annotation.PostConstruct)

Example 37 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project vertexium by visallo.

the class Elasticsearch5SearchIndex method createTransportClient.

private static TransportClient createTransportClient(ElasticsearchSearchIndexConfiguration config) {
    Settings settings = tryReadSettingsFromFile(config);
    if (settings == null) {
        Settings.Builder settingsBuilder = Settings.builder();
        if (config.getClusterName() != null) {
            settingsBuilder.put("cluster.name", config.getClusterName());
        }
        for (Map.Entry<String, String> esSetting : config.getEsSettings().entrySet()) {
            settingsBuilder.put(esSetting.getKey(), esSetting.getValue());
        }
        settings = settingsBuilder.build();
    }
    TransportClient transportClient = new PreBuiltTransportClient(settings);
    for (String esLocation : config.getEsLocations()) {
        String[] locationSocket = esLocation.split(":");
        String hostname;
        int port;
        if (locationSocket.length == 2) {
            hostname = locationSocket[0];
            port = Integer.parseInt(locationSocket[1]);
        } else if (locationSocket.length == 1) {
            hostname = locationSocket[0];
            port = config.getPort();
        } else {
            throw new VertexiumException("Invalid elastic search location: " + esLocation);
        }
        InetAddress host;
        try {
            host = InetAddress.getByName(hostname);
        } catch (UnknownHostException ex) {
            throw new VertexiumException("Could not resolve host: " + hostname, ex);
        }
        transportClient.addTransportAddress(new InetSocketTransportAddress(host, port));
    }
    return transportClient;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) UnknownHostException(java.net.UnknownHostException) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) InetAddress(java.net.InetAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 38 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project calcite by apache.

the class Elasticsearch5Schema method open.

private void open(List<InetSocketAddress> transportAddresses, Map<String, String> userConfig) {
    final List<TransportAddress> transportNodes = new ArrayList<>(transportAddresses.size());
    for (InetSocketAddress address : transportAddresses) {
        transportNodes.add(new InetSocketTransportAddress(address));
    }
    Settings settings = Settings.builder().put(userConfig).build();
    final TransportClient transportClient = new PreBuiltTransportClient(settings);
    for (TransportAddress transport : transportNodes) {
        transportClient.addTransportAddress(transport);
    }
    final List<DiscoveryNode> nodes = ImmutableList.copyOf(transportClient.connectedNodes());
    if (nodes.isEmpty()) {
        throw new RuntimeException("Cannot connect to any elasticsearch nodes");
    }
    client = transportClient;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 39 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project flink by apache.

the class ElasticsearchSinkITCase method getClient.

@Override
@SuppressWarnings("deprecation")
protected final Client getClient() {
    TransportAddress transportAddress = new TransportAddress(elasticsearchContainer.getTcpHost());
    String expectedClusterName = getClusterName();
    Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build();
    return new PreBuiltTransportClient(settings).addTransportAddress(transportAddress);
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 40 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project MSEC by Tencent.

the class ESHelper method ClusterStatus.

public void ClusterStatus(ArrayList<String> ips, String cluster_name, QueryESClusterDetailResponse response) {
    Logger logger = Logger.getLogger(ESHelper.class);
    TransportClient client = null;
    HashSet<String> total_ips = new HashSet<>(ips);
    try {
        Settings settings = Settings.builder().put("cluster.name", cluster_name).put("client.transport.sniff", true).build();
        for (String ip : ips) {
            if (client == null) {
                client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
            } else
                client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
        }
        ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().setTimeout(TimeValue.timeValueSeconds(5)).execute().actionGet();
        if (healthResponse != null && !healthResponse.isTimedOut()) {
            response.setActive_shards(healthResponse.getActiveShards());
            response.setTotal_shards(healthResponse.getActiveShards() + healthResponse.getUnassignedShards());
            response.setHealth_status(healthResponse.getStatus().toString().toLowerCase());
            response.setServer_port(default_port);
            logger.info(healthResponse);
            if (healthResponse.getNumberOfNodes() > 0) {
                NodesStatsResponse nodeStats = client.admin().cluster().prepareNodesStats().all().get();
                if (nodeStats != null) {
                    for (NodeStats stats : nodeStats.getNodes()) {
                        QueryESClusterDetailResponse.RTInfo info = new QueryESClusterDetailResponse().new RTInfo();
                        info.setOK(true);
                        info.setAvail_disk_size(stats.getFs().getTotal().getAvailable().getBytes());
                        info.setDoc_count(stats.getIndices().getDocs().getCount());
                        info.setDoc_disk_size(stats.getIndices().getStore().getSizeInBytes());
                        response.getInfo_map().put(stats.getNode().getAddress().getHost(), info);
                        total_ips.add(stats.getNode().getAddress().getHost());
                    }
                }
            }
        }
        // update Zen settings
        client.admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().put("discovery.zen.minimum_master_nodes", total_ips.size() / 2 + 1)).setTransientSettings(Settings.builder().put("discovery.zen.minimum_master_nodes", total_ips.size() / 2 + 1)).get();
        // update template settings
        PutIndexTemplateResponse tpl_resp = client.admin().indices().preparePutTemplate("template_msec").setTemplate("msec_*").setSettings(Settings.builder().put("number_of_replicas", 1).put("refresh_interval", "30s")).addMapping("logs", MAPPING).execute().get();
        logger.info("Create mapping: " + tpl_resp.isAcknowledged());
    } catch (UnknownHostException e) {
        logger.error(e);
    } catch (Exception e) {
        logger.error(e);
    } finally {
        if (client != null)
            client.close();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UnknownHostException(java.net.UnknownHostException) Logger(org.apache.log4j.Logger) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) Settings(org.elasticsearch.common.settings.Settings) QueryESClusterDetailResponse(beans.response.QueryESClusterDetailResponse)

Aggregations

PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)41 Settings (org.elasticsearch.common.settings.Settings)33 TransportClient (org.elasticsearch.client.transport.TransportClient)25 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)23 TransportAddress (org.elasticsearch.common.transport.TransportAddress)17 UnknownHostException (java.net.UnknownHostException)10 InetSocketAddress (java.net.InetSocketAddress)5 InetAddress (java.net.InetAddress)4 HashMap (java.util.HashMap)4 PostConstruct (javax.annotation.PostConstruct)4 SearchItem (cn.exrick.manager.dto.front.SearchItem)3 IOException (java.io.IOException)3 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 Builder (org.elasticsearch.common.settings.Settings.Builder)3 XmallException (cn.exrick.common.exception.XmallException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Logger (org.apache.log4j.Logger)2 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)2