To send events to Elasticsearch from a Java application, you can use the Elasticsearch client library. One of the preferred client libraries is the Elasticsearch Java REST Client, which allows you to interact with your Elasticsearch cluster.
Here's a step-by-step example of how to send an event to Elasticsearch using its REST client. Before you begin, you'll need to make sure that you have the Elasticsearch Java REST Client library added to your project. You can add it using Maven by including the following dependencies in your pom.xml file:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.17.10</version> <!-- Make sure the version matches your Elasticsearch version -->
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.5</version>
</dependency>
Now, you can write the Java code to send an event to Elasticsearch:
import org.apache.http.HttpHost;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ElasticsearchEventSender {
private static final String ELASTICSEARCH_IP = "your_elasticsearch_ip"; // e.g., "127.0.0.1"
private static final int ELASTICSEARCH_PORT = 9200; // default port is usually 9200
public static void main(String[] args) {
// Initialize the REST client
RestClientBuilder restClientBuilder = RestClient.builder(
new HttpHost(ELASTICSEARCH_IP, ELASTICSEARCH_PORT, "http"));
try (RestClient restClient = restClientBuilder.build()) {
// Prepare index name with current date
String currentDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String indexName = "my-index-" + currentDate;
// Prepare JSON document
String jsonDocument = "{ \"event\": \"sample event\", \"timestamp\": \"" + System.currentTimeMillis() + "\"}";
// Create a request to send the JSON document as an "index" request
Request request = new Request("POST", "/" + indexName + "/_doc");
request.setJsonEntity(jsonDocument);
// Send the request
Response response = restClient.performRequest(request);
System.out.println("Response code: " + response.getStatusLine().getStatusCode());
System.out.println(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Key Points:
- Dependencies: Ensure you have the necessary Maven dependencies included for the Elasticsearch REST client.
- ElasticSearch IP/Port: Replace
your_elasticsearch_ip with your Elasticsearch server's IP address, and ensure the port is correct.
- Index Name: The index name is dynamically generated with the current date using
LocalDate.now() and DateTimeFormatter.
- JSON Document: The document to be indexed is represented as a JSON string (
String jsonDocument).
- HttpRequest: The
Request object is set with the method type POST to create new documents and includes the JSON entity.
This Java code creates and sends a simple event document to an Elasticsearch cluster, creating a new index if it doesn't exist. Be sure to handle exceptions and errors appropriately, especially for production applications.