Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added High Level Rest Client for SDK #306

Merged
merged 4 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dependencies {
implementation("org.opensearch:opensearch:${opensearchVersion}")
implementation("org.apache.logging.log4j:log4j-api:${log4jVersion}")
implementation("org.apache.logging.log4j:log4j-core:${log4jVersion}")
implementation ("org.opensearch.client:opensearch-rest-high-level-client:${opensearchVersion}")
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
implementation("org.opensearch.client:opensearch-rest-client:${opensearchVersion}")
implementation("org.opensearch.client:opensearch-java:${opensearchVersion}")
implementation("org.opensearch.plugin:transport-netty4-client:${opensearchVersion}")
Expand Down
47 changes: 39 additions & 8 deletions src/main/java/org/opensearch/sdk/SDKClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.transport.OpenSearchTransport;
Expand All @@ -41,14 +42,9 @@
public class SDKClient {
private OpenSearchClient javaClient;
private RestClient restClient = null;
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
private RestHighLevelClient highLevelClient;

/**
* Creates OpenSearchClient for SDK. It also creates a restClient as a wrapper around Java OpenSearchClient
* @param hostAddress The address of OpenSearch cluster, client can connect to
* @param port The port of OpenSearch cluster
* @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client
*/
public OpenSearchClient initializeClient(String hostAddress, int port) {
private RestClientBuilder builder(String hostAddress, int port) {
RestClientBuilder builder = RestClient.builder(new HttpHost(hostAddress, port));
builder.setStrictDeprecationMode(true);
builder.setHttpClientConfigCallback(httpClientBuilder -> {
Expand All @@ -74,6 +70,17 @@ public TlsDetails create(final SSLEngine sslEngine) {
throw new RuntimeException(e);
}
});
return builder;
}

/**
* Creates OpenSearchClient for SDK. It also creates a restClient as a wrapper around Java OpenSearchClient
* @param hostAddress The address of OpenSearch cluster, client can connect to
* @param port The port of OpenSearch cluster
* @return SDKClient which is internally an OpenSearchClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
*/
public OpenSearchClient initializeJavaClient(String hostAddress, int port) {
RestClientBuilder builder = builder(hostAddress, port);

restClient = builder.build();

Expand All @@ -91,7 +98,20 @@ public TlsDetails create(final SSLEngine sslEngine) {
}

/**
* Close this client.
* Creates High Level Rest Client for SDK.
* @param hostAddress The address of OpenSearch cluster, client can connect to
* @param port The port of OpenSearch cluster
* @return SDKClient which is internally an RestHighLevelClient. The user is responsible for calling {@link #doCloseRestClient()} when finished with the client
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
*/
public RestHighLevelClient initializeRestClient(String hostAddress, int port) {
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
RestClientBuilder builder = builder(hostAddress, port);

highLevelClient = new RestHighLevelClient(builder);
return highLevelClient;
}

/**
* Close java client.
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
*
* @throws IOException if closing the restClient fails
*/
Expand All @@ -100,4 +120,15 @@ public void doCloseRestClient() throws IOException {
restClient.close();
}
}

/**
* Close high level rest client.
*
* @throws IOException if closing the highLevelClient fails
*/
public void doCloseHighLevelClient() throws IOException {
if (highLevelClient != null) {
highLevelClient.close();
}
}
}
21 changes: 19 additions & 2 deletions src/test/java/org/opensearch/sdk/TestSDKClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package org.opensearch.sdk;

import org.junit.jupiter.api.Test;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.indices.Alias;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
Expand All @@ -23,9 +25,9 @@ public class TestSDKClient extends OpenSearchTestCase {
SDKClient sdkClient = new SDKClient();

@Test
public void testCreateClient() throws Exception {
public void testCreateJavaClient() throws Exception {

OpenSearchClient testClient = sdkClient.initializeClient("localhost", 9200);
OpenSearchClient testClient = sdkClient.initializeJavaClient("localhost", 9200);
assertInstanceOf(OpenSearchClient.class, testClient);

assertThrows(
Expand All @@ -41,4 +43,19 @@ public void testCreateClient() throws Exception {
sdkClient.doCloseRestClient();
}

@Test
public void testCreateHighLevelRestClient() throws Exception {
RestHighLevelClient testClient = sdkClient.initializeRestClient("localhost", 9200);

// Using the package name here as Java uses package name if the filename from different packages are same
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
org.opensearch.client.indices.CreateIndexRequest createIndexRequest = new org.opensearch.client.indices.CreateIndexRequest(
"my-index"
);

assertThrows(ConnectException.class, () -> testClient.indices().create(createIndexRequest, RequestOptions.DEFAULT));

sdkClient.doCloseHighLevelClient();

}

}