Connect to OpenSearch with Java
There are a few ways to write Java client for OpenSearch. 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 10 |
<dependency> <groupId>org.opensearch.client</groupId> <artifactId>opensearch-rest-high-level-client</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.opensearch.client</groupId> <artifactId>opensearch-rest-client-sniffer</artifactId> <version>1.2.0</version> </dependency> |
1. Authentication to OpenSearch Cluster
1 2 3 |
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("icopensearch", "<Password>")); |
2. Connect to cluster
1 2 3 4 5 6 7 8 9 10 11 |
final SSLContext sslContext = sslBuilder.build(); RestClientBuilder builder = RestClient.builder( new HttpHost("xxx.xxx.xxx.xxx", 9200, "https"), new HttpHost("xxx.xxx.xxx.xxx", 9200, "https"), new HttpHost("xxx.xxx.xxx.xxx", 9200, "https")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient( final HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext); } }); |
3. Indexing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
createIndexRequest.settings(Settings.builder() .put("index.number_of_shards",4) .put("index.number_of_replicas",3) ); HashMap<String, String> typeMapping=new HashMap<String, String>(); typeMapping.put("type","integer"); HashMap<String, Object> ageMapping=new HashMap<String, Object>(); ageMapping.put("age",typeMapping); HashMap<String, Object> mapping=new HashMap<String, Object>(); mapping.put("properties",ageMapping); createIndexRequest.mapping(mapping); CreateIndexResponse createIndexResponse=client.indices().create(createIndexRequest,RequestOptions.DEFAULT); System.out.println("\nCreating index:"); System.out.println("Is client acknowledged?"+((createIndexResponse.isAcknowledged())?" Yes":" No")); |
4. Getting a document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
IndexRequest request = new IndexRequest(index); request.id("1"); HashMap<String, String> stringMapping = new HashMap<String, String>(); stringMapping.put("message:", "Testing Java REST client"); request.source(stringMapping); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); System.out.println("\nAdding document:"); System.out.println(indexResponse); GetRequest getRequest = new GetRequest(index, "1"); GetResponse response = client.get(getRequest, RequestOptions.DEFAULT); System.out.println("\nSearch results:"); System.out.println(response.getSourceAsString()); |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.SSLContexts; import org.opensearch.action.admin.indices.delete.DeleteIndexRequest; import org.opensearch.action.delete.DeleteRequest; import org.opensearch.action.delete.DeleteResponse; import org.opensearch.action.get.GetRequest; import org.opensearch.action.get.GetResponse; import org.opensearch.action.index.IndexRequest; import org.opensearch.action.index.IndexResponse; import org.opensearch.action.support.master.AcknowledgedResponse; import org.opensearch.client.RequestOptions; import org.opensearch.client.RestClient; import org.opensearch.client.RestClientBuilder; import org.opensearch.client.RestHighLevelClient; import org.opensearch.client.indices.CreateIndexRequest; import org.opensearch.client.indices.CreateIndexResponse; import org.opensearch.common.settings.Settings; import org.opensearch.client.sniff.SniffOnFailureListener; import org.opensearch.client.sniff.OpenSearchNodesSniffer; import org.opensearch.client.sniff.NodesSniffer; import org.opensearch.client.sniff.Sniffer; import javax.net.ssl.SSLContext; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.util.HashMap; import java.util.concurrent.TimeUnit; public class RESTClientSample { public static void main(String[] args) throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("icopensearch", "<Password>")); RestClientBuilder builder = RestClient.builder( new HttpHost("xxx.xxx.xxx.xxx", 9200, "https"), new HttpHost("xxx.xxx.xxx.xxx", 9200, "https"), new HttpHost("xxx.xxx.xxx.xxx", 9200, "https")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient( final HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); RestHighLevelClient client = new RestHighLevelClient(builder); NodesSniffer nodesniffer = new OpenSearchNodesSniffer ( client.getLowLevelClient(), TimeUnit.SECONDS.toMillis(5), OpenSearchNodesSniffer.Scheme.HTTPS); Sniffer sniffer = Sniffer.builder(client.getLowLevelClient()) .setNodesSniffer(nodesniffer) .build(); SniffOnFailureListener listener = new SniffOnFailureListener(); listener.setSniffer(sniffer); String index = "java-test-index"; CreateIndexRequest createIndexRequest = new CreateIndexRequest(index); createIndexRequest.settings(Settings.builder() .put("index.number_of_shards", 4) .put("index.number_of_replicas", 3) ); HashMap<String, String> typeMapping = new HashMap<String,String>(); typeMapping.put("type", "integer"); HashMap<String, Object> ageMapping = new HashMap<String, Object>(); ageMapping.put("age", typeMapping); HashMap<String, Object> mapping = new HashMap<String, Object>(); mapping.put("properties", ageMapping); createIndexRequest.mapping(mapping); CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); System.out.println("\nCreating index:"); System.out.println("Is client acknowledged?" + ((createIndexResponse.isAcknowledged())? " Yes" : " No")); IndexRequest request = new IndexRequest(index); request.id("1"); HashMap<String, String> stringMapping = new HashMap<String, String>(); stringMapping.put("message:", "Testing Java REST client"); request.source(stringMapping); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); System.out.println("\nAdding document:"); System.out.println(indexResponse); GetRequest getRequest = new GetRequest(index, "1"); GetResponse response = client.get(getRequest, RequestOptions.DEFAULT); System.out.println("\nSearch results:"); System.out.println(response.getSourceAsString()); DeleteRequest deleteDocumentRequest = new DeleteRequest(index, "1"); DeleteResponse deleteResponse = client.delete(deleteDocumentRequest, RequestOptions.DEFAULT); System.out.println("\nDeleting document:"); System.out.println(deleteResponse); DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index); AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); System.out.println("\nDeleting index:"); System.out.println("Is client acknowledged?" + ((deleteIndexResponse.isAcknowledged())? " Yes" : " No")); client.close(); } } |
By Instaclustr Support
Need Support?
Experiencing difficulties on the website or console?
Already have an account?
Need help with your cluster?
Contact Support
Why sign up?