Signed-off-by: keithhc2 <keithhc2@users.noreply.github.com>
This commit is contained in:
keithhc2 2022-02-15 13:12:01 -08:00
parent accc360c4a
commit a3ed129d15
1 changed files with 10 additions and 24 deletions

View File

@ -123,6 +123,7 @@ import java.io.IOException;
public class OpenSearchClientExample { public class OpenSearchClientExample {
public static void main(String[] args) { public static void main(String[] args) {
RestClient restClient = null;
try{ try{
System.setProperty("javax.net.ssl.trustStore", "/full/path/to/keystore"); System.setProperty("javax.net.ssl.trustStore", "/full/path/to/keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "password-to-keystore"); System.setProperty("javax.net.ssl.trustStorePassword", "password-to-keystore");
@ -133,7 +134,7 @@ public class OpenSearchClientExample {
new UsernamePasswordCredentials("admin", "admin")); new UsernamePasswordCredentials("admin", "admin"));
//Initialize the client with SSL and TLS enabled //Initialize the client with SSL and TLS enabled
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "https")). restClient = RestClient.builder(new HttpHost("localhost", 9200, "https")).
setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override @Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
@ -214,28 +215,11 @@ restClient.close();
## Complete code sample ## Complete code sample
```java ```java
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.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.clients.base.RestClientTransport;
import org.opensearch.clients.base.Transport;
import org.opensearch.clients.json.jackson.JacksonJsonpMapper;
import org.opensearch.clients.opensearch.OpenSearchClient;
import org.opensearch.clients.opensearch._global.IndexRequest;
import org.opensearch.clients.opensearch._global.IndexResponse;
import org.opensearch.clients.opensearch._global.SearchResponse;
import org.opensearch.clients.opensearch.indices.*;
import org.opensearch.clients.opensearch.indices.put_settings.IndexSettingsBody;
import java.io.IOException;
public class OpenSearchClientExample { public class OpenSearchClientExample {
public static void main(String[] args) { public static void main(String[] args) {
RestClient restClient = null;
try{ try{
System.setProperty("javax.net.ssl.trustStore", "/full/path/to/keystore"); System.setProperty("javax.net.ssl.trustStore", "/full/path/to/keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "password-to-keystore"); System.setProperty("javax.net.ssl.trustStorePassword", "password-to-keystore");
@ -246,7 +230,7 @@ public class OpenSearchClientExample {
new UsernamePasswordCredentials("admin", "admin")); new UsernamePasswordCredentials("admin", "admin"));
//Initialize the client with SSL and TLS enabled //Initialize the client with SSL and TLS enabled
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "https")). restClient = RestClient.builder(new HttpHost("localhost", 9200, "https")).
setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override @Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
@ -290,8 +274,8 @@ public class OpenSearchClientExample {
System.out.println(e.toString()); System.out.println(e.toString());
} finally { } finally {
try { try {
if (client != null) { if (restClient != null) {
client.close(); restClient.close();
} }
} catch (IOException e) { } catch (IOException e) {
System.out.println(e.toString()); System.out.println(e.toString());
@ -299,4 +283,6 @@ public class OpenSearchClientExample {
} }
} }
} }
``` ```