Connecting to Elasticsearch with Java
- There are few ways to write JAVA client for Elasticsearch. Here we are using Java High-Level REST Client. Make sure you have Java 1.8 or higher version. In order to use Java High-Level REST Client, you need to add the following dependencies to the project.
1 2 3 4 5 6 7 8 9 | <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.5.1</version> </dependency> |
- Authentication to Elasticsearch Cluster
1 2 3 4 5 | final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("icelasticsearch", "password")); |
- Connect to cluster
1 2 3 4 5 6 7 8 9 10 11 12 13 | final SSLContext sslContext = sslBuilder.build(); RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("xxx.xxx.xxx.xxx", 9201, "https"), new HttpHost("xxx.xxx.xxx.xxx", 9201, "https"), new HttpHost("xxx.xxx.xxx.xxx", 9201, "https")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { |
- Indexing
1 2 3 4 5 6 7 8 9 | Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("description", "this is a test"); jsonMap.put("timestamp", new Date()); IndexRequest indexRequest = new IndexRequest("testing") .id("1").source(jsonMap); |
- Getting a document
1 2 3 4 5 6 7 8 9 | client.index(indexRequest, RequestOptions.DEFAULT); System.out.printf("Indexed document to index \"%s\" with id %s%n", indexRequest.index(), indexRequest.id()); GetRequest getRequest = new GetRequest("testing", "1"); |
Full sample code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("icelasticsearch", "password")); Path trustStorePath = Paths.get("path/to/trustsore/file.jks"); KeyStore truststore = KeyStore.getInstance("jks"); try (InputStream is = Files.newInputStream(trustStorePath)) { truststore.load(is, "instaclustr".toCharArray()); } SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null); final SSLContext sslContext = sslBuilder.build(); RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("xxx.xxx.xxx.xxx", 9201, "https"), new HttpHost("xxx.xxx.xxx.xxx", 9201, "https"), new HttpHost("xxx.xxx.xxx.xxx", 9201, "https")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient( final HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext); } }) ); Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("description", "this is a test"); jsonMap.put("timestamp", new Date()); IndexRequest indexRequest = new IndexRequest("testing") .id("1").source(jsonMap); client.index(indexRequest, RequestOptions.DEFAULT); System.out.printf("Indexed document to index \"%s\" with id %s%n", indexRequest.index(), indexRequest.id()); GetRequest getRequest = new GetRequest("testing", "1"); GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); System.out.printf("Retrieved document: {Id: %s, Description: %s, Test Time: %s}%n", getResponse.getId(), getResponse.getSource().get("description"), getResponse.getSource().get("timestamp")); client.close(); |
Submit Comment