Search in sources :

Example 1 with PreBuiltXPackTransportClient

use of org.elasticsearch.xpack.client.PreBuiltXPackTransportClient in project api-core by ca-cwds.

the class ElasticUtils method makeESTransportClient.

private static TransportClient makeESTransportClient(final ElasticsearchConfiguration config) {
    TransportClient client;
    final String cluster = config.getElasticsearchCluster();
    final String user = config.getUser();
    final String password = config.getPassword();
    final boolean secureClient = StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password);
    final Settings.Builder settings = Settings.builder().put("cluster.name", cluster);
    settings.put("client.transport.sniff", true);
    if (secureClient) {
        LOGGER.info("ENABLE X-PACK - cluster: {}", cluster);
        settings.put("xpack.security.user", user + ":" + password);
        client = new PreBuiltXPackTransportClient(settings.build());
    } else {
        LOGGER.info("DISABLE X-PACK - cluster: {}", cluster);
        client = new PreBuiltTransportClient(settings.build());
    }
    return client;
}
Also used : PreBuiltXPackTransportClient(org.elasticsearch.xpack.client.PreBuiltXPackTransportClient) PreBuiltXPackTransportClient(org.elasticsearch.xpack.client.PreBuiltXPackTransportClient) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with PreBuiltXPackTransportClient

use of org.elasticsearch.xpack.client.PreBuiltXPackTransportClient in project bboss-elastic by bbossgroups.

the class ElasticSearchTransportClient method openClient.

// @Override
// public void addEvent(Event event, IndexNameBuilder indexNameBuilder, String indexType, long ttlMs)
// throws Exception {
// if (bulkRequestBuilder == null) {
// bulkRequestBuilder = client.prepareBulk();
// }
// 
// IndexRequestBuilder indexRequestBuilder = null;
// if (indexRequestBuilderFactory == null) {
// XContentBuilder bytesStream = null;
// try {
// bytesStream = serializer.getContentBuilder(event);
// indexRequestBuilder = client.prepareIndex(indexNameBuilder.getIndexName(event), indexType)
// .setSource(bytesStream);
// } finally {
// if (bytesStream != null) {
// // bytesStream.cl
// }
// }
// 
// } else {
// indexRequestBuilder = indexRequestBuilderFactory.createIndexRequest(client,
// indexNameBuilder.getIndexPrefix(event), indexType, event);
// }
// 
// if (ttlMs > 0) {
// indexRequestBuilder.setTTL(ttlMs);
// }
// bulkRequestBuilder.add(indexRequestBuilder);
// }
/**
 * Open client to elaticsearch cluster
 *
 * @param clusterName
 */
private void openClient(String clusterName) {
    logger.info("Using ElasticSearch hostnames: {} ", Arrays.toString(serverAddresses));
    Settings settings = null;
    Settings.Builder builder = Settings.builder();
    if (this.elasticUser != null && !this.elasticUser.equals("")) {
        builder.put("cluster.name", clusterName).put("xpack.security.user", this.elasticUser + ":" + this.elasticPassword);
    // .put("shield.user",
    // this.elasticUser+":"+this.elasticPassword)
    } else {
        builder.put("cluster.name", clusterName);
    }
    if (this.extendElasticsearchPropes != null && extendElasticsearchPropes.size() > 0) {
        Iterator<Entry<Object, Object>> iterator = extendElasticsearchPropes.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<Object, Object> entry = iterator.next();
            builder.put((String) entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    settings = builder.build();
    try {
        TransportClient transportClient = this.elasticUser != null && !this.elasticUser.equals("") ? new PreBuiltXPackTransportClient(settings) : new PreBuiltTransportClient(settings);
        // TransportClient transportClient = new TransportClient(settings);
        for (TransportAddress host : serverAddresses) {
            transportClient.addTransportAddress(host);
        }
        if (client != null) {
            client.close();
        }
        client = transportClient;
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : Entry(java.util.Map.Entry) PreBuiltXPackTransportClient(org.elasticsearch.xpack.client.PreBuiltXPackTransportClient) PreBuiltXPackTransportClient(org.elasticsearch.xpack.client.PreBuiltXPackTransportClient) TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

TransportClient (org.elasticsearch.client.transport.TransportClient)2 Settings (org.elasticsearch.common.settings.Settings)2 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)2 PreBuiltXPackTransportClient (org.elasticsearch.xpack.client.PreBuiltXPackTransportClient)2 Entry (java.util.Map.Entry)1 TransportAddress (org.elasticsearch.common.transport.TransportAddress)1