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;
}
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();
}
}
Aggregations