Interacting with Amazon Elasticsearch Service using JAVA SDK

Elasticsearch is a popular, open-source, distributed, RESTful search and analytics engine, built on Apache Lucene. It is flexibly designed to handle multiple types of data with ease, making it a go-to option for log and event data management.

Interacting with Amazon Elasticsearch Service using JAVA SDK

Elasticsearch is a popular, open-source, distributed, RESTful search and analytics engine, built on Apache Lucene. It is flexibly designed to handle multiple types of data with ease, making it a go-to option for log and event data management.

Amazon Elasticsearch Service (Amazon ES) is a managed service that simplifies the deployment, operation, and scaling of Elasticsearch clusters in the AWS cloud. In this article, we’ll delve into how you can interact with Amazon Elasticsearch Service using the JAVA SDK.

What is Amazon Elasticsearch Service?

Amazon Elasticsearch Service (Amazon ES) is a fully managed service that simplifies the deployment, operation, and scaling of Elasticsearch in the AWS Cloud. It provides a user-friendly interface for handling all the administrative tasks like hardware procurement, software patching, failure recovery, backups, and monitoring.

Why Java SDK?

The Java SDK for Amazon Elasticsearch Service provides a high-level API that greatly simplifies tasks such as instance management, index creation, and data querying. It’s a powerful tool for developers working in a Java environment.

Getting Started with Amazon ES and Java SDK

To interact with Amazon ES using the Java SDK, you need to:

  1. Set Up Your AWS and Elasticsearch Service: Sign up for an AWS account and set up an Amazon ES domain. Make sure to note the endpoint for your domain.

  2. Set Up Your Development Environment: Install Java Development Kit (JDK) and Apache Maven, which are prerequisites for using the Java High-Level REST client.

  3. Install the Java High-Level REST Client: This client provides a straightforward method to interact with Amazon ES using Java. Use the following dependency in your Maven pom.xml file to install it:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.10.2</version>
</dependency>

Interacting with Amazon ES

Connect to Amazon ES

Create a RestHighLevelClient instance to connect to Amazon ES:

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http")));

Replace “localhost” and “9200” with your Amazon ES domain endpoint and port number.

Create an Index

You can create an index in Amazon ES like this:

CreateIndexRequest request = new CreateIndexRequest("my_index");
client.indices().create(request, RequestOptions.DEFAULT);

Index a Document

Index a document in your newly created index:

IndexRequest request = new IndexRequest("my_index");
request.id("1");
String jsonString = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
        "}";
request.source(jsonString, XContentType.JSON);
client.index(request, RequestOptions.DEFAULT);

Search for a Document

You can search for documents using the SearchRequest and SearchSourceBuilder classes:

SearchRequest searchRequest = new SearchRequest("my_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

Closing the Connection

Always remember to close the RestHighLevelClient when you’re finished:

client.close();

Conclusion

Interacting with Amazon Elasticsearch Service using the Java SDK is a straightforward process once you understand the basic steps involved. The Java High-Level REST client makes it easy to manage your instances and data. By integrating these tools into your data management processes, you can leverage the powerful search and analytics capabilities of Elasticsearch in a manageable, scalable way.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.