Creating an HTTP Elasticsearch client

With Elasticsearch 5.x, the Elasticsearch team has provided a custom low-level HTTP client to communicate with Elasticsearch. Its main features are as follows:

  • Minimal dependencies
  • Load balancing across all available nodes
  • Failover in the case of node failures and upon specific response codes
  • Failed connection penalization (whether a failed node is retried depends on how many consecutive times it failed; the more failed attempts, the longer the client will wait before trying that same node again)
  • Persistent connections
  • Trace logging of requests and responses
  • Optional automatic discovery of cluster nodes

Getting ready

You need an up-and-running Elasticsearch installation as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.

A Maven tool, or an IDE that natively supports it for Java programming such as Eclipse or IntelliJ IDEA, must be installed.

The code for this recipe is in the chapter_14/http_es_client directory.

How to do it...

For creating RestClient, we will perform the following steps:

  1. For these examples, we need to add the Elasticsearch HTTP client library used to execute HTTP calls. This library is available in the main Maven repository search.maven.org. To enable compilation in your Maven pom.xml project just add the following code:
            <dependency> 
                <groupId>org.elasticsearch.client</groupId> 
                <artifactId>rest</artifactId> 
                <version>5.0.0</version> 
            </dependency> 
    
  2. If we want to instantiate a client and fetch a document with a get method, the code will look like the following:
            package com.packtpub; 
     
            import org.apache.http.HttpEntity; 
            import org.apache.http.HttpHost; 
            import org.apache.http.HttpStatus; 
            import org.apache.http.util.EntityUtils; 
            import org.elasticsearch.client.Response; 
            import org.elasticsearch.client.RestClient; 
     
            import java.io.IOException; 
     
            public class App { 
     
                public static void main(String[] args) { 
                    RestClient client = RestClient.builder( 
                          new HttpHost("localhost", 9200, "http")).build(); 
     
                    try { 
                        Response response = client.performRequest("GET",  
                        "/test-index/test-type/1"); 
     
                        if (response.getStatusLine().getStatusCode() != 
                        HttpStatus.SC_OK) { 
                            System.err.println("Method failed: " + 
                            response.getStatusLine()); 
                        } else { 
                        HttpEntity entity = response.getEntity(); 
                        String responseBody = EntityUtils.toString(entity); 
                        System.out.println(responseBody); 
                            } 
     
                     } catch (IOException e) { 
                        System.err.println("Fatal transport error: " + 
                        e.getMessage()); 
                        e.printStackTrace(); 
                    } finally { 
                        // Release the connection. 
                        try { 
                           client.close(); 
                        } catch (IOException e) { 
                           e.printStackTrace(); 
                        } 
                    } 
                } 
            } 
    
  3. The result, if the document will be:
            {"_index":"test-index","_type":"test-   
            type","_id":"1","_version":1,"exists":true, "_source" : {...}} 
    

How it works...

Internally the Elasticseach RestClient uses Apache HttpComponents and wraps it with more convenient methods.

We perform the previous steps to create and use a RestClient:

  1. The first step is to initialize the RestClient object.
  2. In the previous code this is done via the following code:
            RestClient client = RestClient.builder( 
                    new HttpHost("localhost", 9200, "http")).build(); 
    
  3. The builder method accepts a multi-value HttpHost (in this way you can pass a list of HTTP addresses) and returns RestClientBuilder under the hood.
  4. RestClientBuilder allows client communication to be customized by several methods such as:
    • setDefaultHeaders(Header[] defaultHeaders): This allows the custom headers that must be sent for every request to be provided.
    • setMaxRetryTimeoutMillis(int maxRetryTimeoutMillis): This allows the max retry timeout to be defined if there are multiple attempts for the same request.
    • setPathPrefix(String pathPrefix): This allows a custom path prefix to be defined for every request.
    • setFailureListener(FailureListener failureListener): This allows a custom failure listener to be provided, which is called in an instance of node failure. This can be used to provide user defined behavior in the case of node failure.
    • setHttpClientConfigCallback(RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback): This allows modification to the HTTP client communication, such as adding compression or an encryption layer.
    • setRequestConfigCallback(RestClientBuilder.RequestConfigCallback requestConfigCallback): This allows the configuration of request authentications, timeout, and other properties that can be set a request level.
  5. After having created the RestClient, we can execute some requests against it via the several performRequest for synchronous calls and performRequestAsync methods for asynchronous ones.
  6. These methods allow the setting of parameters such as:
  7. In the previous example, we have executed the GET REST call with the following code:
                Response response = client.performRequest("GET", "/test-
                index/test-type/1"); 
    
  8. The response object is an org.elasticsearch.client.Response that wraps the Apache HttpComponents response one: for this reason, the code to manage the response is the same as the previous recipe.

Note

The RestClient is a low level one; it has no helpers on build queries or actions. For now, using it consists of building the JSON string of the request and then parsing the JSON response string.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.19.185