Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ElasticSearch with Dropwizard

I'm trying to use ElasticSearch java API in a Dropwizard application.

I found the dropwizard-elasticsearch package: https://github.com/dropwizard/dropwizard-elasticsearch, that seems to be exactly what I need. Unfortunately, it provides zero "useful" documentation, and no usage examples.

I still haven't understood how to connect to remote servers using the TransportClient, because, due to no documentation of drop wizard-elasticsearch configuration, I should try "randomly" until I find the correct configuration keys...

Does anyone have tried using dropwizard-elasticsearch? Or has someone a real usage example of this?

Thanks in advance,

like image 975
Carmine Giangregorio Avatar asked Nov 12 '14 15:11

Carmine Giangregorio


2 Answers

Unless you really need to join the Elasticsearch cluster, I would avoid using the Java classes provided by Elasticsearch. If you do connect to Elasticsearch this way, you will need to keep the JVM versions used by Elasticsearch and your application in sync.

Instead, you can connect to Elasticsearch using the Jest client found on GitHub. This will allow you to connect to Elasticsearch over the REST interface, just like all of the other client libraries.

You will need to create a simple configuration block for Elasticsearch, to specify the URL of the REST interface. Also, you will need to create a Manager for starting and stopping the JestClient.

Update: You can find the Dropwizard bundle that I use for connecting to Elasticsearch on GitHub. Here are some basic usage instructions for Java 8:

Include the dependency for the bundle in your project's POM.

<dependency>
  <groupId>com.meltmedia.dropwizard</groupId>
  <artifactId>dropwizard-jest</artifactId>
  <version>0.1.0</version>
</dependency>

Define the JestConfiguraion class somewhere in your application's configuration.

import com.meltmedia.dropwizard.jest.JestConfiguration;

...

@JsonProperty
protected JestConfiguration elasticsearch;

public JestConfiguration getElasticsearch() {
  return jest;
}

Then include the bundle in the initialize method of your application.

import com.meltmedia.dropwizard.jest.JestBundle;

...
protected JestBundle jestBundle;

@Override
public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
  bootstrap.addBundle(jestBundle = JestBundle.<ExampleConfiguration>builder()
    .withConfiguration(ExampleConfiguration::getElasticsearch)
    .build());
}

Finally, use the bundle to access the client supplier.

@Override
public void run(ExampleConfiguration config, Environment env) throws Exception {
  JestClient client = jestBundle.getClientSupplier().get();
}
like image 134
Christian Trimble Avatar answered Nov 12 '22 20:11

Christian Trimble


Too long for a comment.

Please check README.md ->"Usage" and "Configuration". If you want dropwizard to create managed TransportClient your configuration settings should be something like this. nodeClient: false clusterName: dropwizard_elasticsearch_test servers: - 127.23.42.1:9300 - 127.23.42.2:9300 - 127.23.42.3

How to obtain dropwizard-managed TransportClient? Example here: public void transportClientShouldBeCreatedFromConfig().

@Override public void run(DemoConfiguration config, Environment environment) { final ManagedEsClient managedClient = new ManagedEsClient(configuration.getEsConfiguration()); Client client = managedClient.getClient(); ((TransportClient) client).transportAddresses().size(); // [...] }

There is also a sample blog application using Dropwizard and ElasticSearch. See "Acknowledgements" section in README.md.

like image 1
zloster Avatar answered Nov 12 '22 20:11

zloster