Use ElasticSearch with Dropwizard
I am trying to use java API ElasticSearch in Dropwizard app.
I found the dropwizard-elasticsearch package: https://github.com/dropwizard/dropwizard-elasticsearch , which seems to be exactly what I need. Unfortunately, it provides zero "helpful" documentation and usage examples.
I still haven't figured out how to connect to remote servers using TransportClient because due to lack of configuration documentation for drop wizard-elasticsearch I have to try "by accident" until I find the correct configuration keys ...
Has anyone tried using dropwizard-elasticsearch? Or does anyone have a real use case?
Thanks in advance,
source to share
Too long for a comment.
Please check README.md → "Usage" and "Configuration". If you want the dropwizard to create a managed TransportClient, your config 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 do I get a managed dropwizard 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 blogging app using Dropwizard and ElasticSearch. See the Acknowledgments section in the README.md.
source to share
Unless you really need to join an Elasticsearch cluster, I would avoid using the Java classes provided by Elasticsearch. If you 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 through a REST interface like all other client libraries.
You will need to create a simple config block for Elasticsearch to point to the URL of the REST interface. Additionally, you will need to create a dispatcher to start and stop the JestClient.
Update . You can find the Dropwizard package that I use to connect to Elasticsearch on GitHub . Here are some basic instructions for using Java 8:
Include the package dependency in your POM project.
<dependency>
<groupId>com.meltmedia.dropwizard</groupId>
<artifactId>dropwizard-jest</artifactId>
<version>0.1.0</version>
</dependency>
Define the JestConfiguraion class somewhere in your application config.
import com.meltmedia.dropwizard.jest.JestConfiguration;
...
@JsonProperty
protected JestConfiguration elasticsearch;
public JestConfiguration getElasticsearch() {
return jest;
}
Then include the package in initialize
your application method .
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 package to access the client's provider.
@Override
public void run(ExampleConfiguration config, Environment env) throws Exception {
JestClient client = jestBundle.getClientSupplier().get();
}
source to share
I used the Java api for elasticsearch and the thing is the suite you are using, I also looked into it, but some of the documentation did not encourage its use. Here you can use elastic without this bundle: -
-
Define your elastic configs in the .yml file.
elasticsearchHost: 127.0.0.1 elasticPort: 9300 clusterName: elasticsearch
-
Now in the config file (which in my case is mkApiConfiguration ) create static functions that will actually be getter methods for this elastic config: -
@NotNull private static String elasticsearchHost; @NotNull private static Integer elasticPort; @NotNull private static String clusterName; @JsonProperty public static String getElasticsearchHost() { return elasticsearchHost; } //This function will be called while reading configurations from yml file @JsonProperty public void setElasticsearchHost(String elasticsearchHost) { mkApiConfiguration.elasticsearchHost = elasticsearchHost; } @JsonProperty public void setClusterName(String clusterName) { mkApiConfiguration.clusterName = clusterName; } public void setElasticPort(Integer elasticPort) { mkApiConfiguration.elasticPort = elasticPort; } @JsonProperty public static String getClusterName() { return clusterName; } @JsonProperty public static Integer getElasticPort() { return elasticPort; }
-
Now create an elastic factory that you can get the transport client from, better create it as a singleton class to create only one instance and share the elastic configuration that we can get using the getter method of the configuration class as they are a static method. therefore, we don't need to create an object to access these methods. So the code for this factory looks like this:
public class ElasticFactory { //Private constructor private ElasticFactory(){}; public static Client getElasticClient(){ try { /* * Creating Transport client Instance */ Client client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(mkApiConfiguration.getElasticsearchHost()), mkApiConfiguration.getElasticPort())); return client; } catch (Exception e){ e.printStackTrace(); return null; }
}
-
Now you can call this elastic factory method from any class as shown below: -
/*As getInstance is a static method so we can access it directly without creating an object of ElasticFactory class */ Client elasticInstance= ElasticFactory.getElasticClient();
source to share