Carmine Giangregorio
Carmine Giangregorio

Reputation: 973

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,

Upvotes: 7

Views: 2788

Answers (3)

vinit payal
vinit payal

Reputation: 1271

I have used Java api for elasticsearch and the thing is the bundle you are using I also explored but documentation part discouraged using it. Here you can use elastic without this bundle:-

  1. Define your elastic configs in your .yml file.

    elasticsearchHost: 127.0.0.1
    elasticPort: 9300
    clusterName: elasticsearch
    
  2. Now in configuration file(which in my case is mkApiConfiguration) create static functions which will actually be the getter methods for these elastic configuration:-

    @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;
    }
    
  3. Now create a elastic factory from where you can get a transport client better create it as a singleton class so that only one instance is created and shared for elastic configuration we can get configuration using getter method of configuration class as these are static method so we don't need to create an object to access these methods. So code of this factory goes like that

    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;
    }
    

    }

  4. Now you can call this elastic factory method from any class like below:-

    /*As getInstance is a static method so we can access it directly without creating an object of ElasticFactory class */ Client elasticInstance= ElasticFactory.getElasticClient();

Upvotes: 0

Christian Trimble
Christian Trimble

Reputation: 2136

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

Upvotes: 4

zloster
zloster

Reputation: 1157

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.

Upvotes: 1

Related Questions