Java 高级 REST 客户端
OpenSearch Java 高级 REST 客户端已弃用。未来版本将移除对其支持。我们建议改用 Java 客户端。
OpenSearch Java 高级 REST 客户端允许您通过 Java 方法和数据结构而非 HTTP 方法和 JSON 与您的 OpenSearch 集群和索引进行交互。
设置
要开始使用 OpenSearch Java 高级 REST 客户端,请确保您的项目 pom.xml
文件中包含以下依赖项
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-rest-high-level-client</artifactId>
<version>3.1.0</version>
</dependency>
您现在可以启动 OpenSearch 集群。OpenSearch 1.x 高级 REST 客户端适用于 OpenSearch 的 1.x 版本。
安全
在您的 Java 应用程序中使用 REST 客户端之前,您必须配置应用程序的信任库以连接到安全插件。如果您正在使用自签名证书或演示配置,您可以使用以下命令创建自定义信任库并添加根权限证书。
如果您使用的是来自受信任证书颁发机构 (CA) 的证书,则无需配置信任库。
keytool -import <path-to-cert> -alias <alias-to-call-cert> -keystore <truststore-name>
您现在可以将 Java 客户端指向信任库,并设置可访问安全集群的基本身份验证凭据(请参阅下面的示例代码以了解如何操作)。
如果您在配置安全方面遇到问题,请参阅 常见问题 和 排除 TLS 故障。
示例程序
此代码示例使用 OpenSearch 默认配置附带的基本凭据。如果您将 OpenSearch Java 高级 REST 客户端与您自己的 OpenSearch 集群一起使用,请务必更改代码以使用您自己的凭据。
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.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 java.io.IOException;
import java.util.HashMap;
public class RESTClientSample {
public static void main(String[] args) throws IOException {
//Point to keystore with appropriate certificates for security.
System.setProperty("javax.net.ssl.trustStore", "/full/path/to/keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "password-to-keystore");
//Establish credentials to use basic authentication.
//Only for demo purposes. Don't specify your credentials in code.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("admin", "admin"));
//Create a client.
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "https"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
//Create a non-default index with custom settings and mappings.
CreateIndexRequest createIndexRequest = new CreateIndexRequest("custom-index");
createIndexRequest.settings(Settings.builder() //Specify in the settings how many shards you want in the index.
.put("index.number_of_shards", 4)
.put("index.number_of_replicas", 3)
);
//Create a set of maps for the index's mappings.
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);
//Adding data to the index.
IndexRequest request = new IndexRequest("custom-index"); //Add a document to the custom-index we created.
request.id("1"); //Assign an ID to the document.
HashMap<String, String> stringMapping = new HashMap<String, String>();
stringMapping.put("message:", "Testing Java REST client");
request.source(stringMapping); //Place your content into the index's source.
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
//Getting back the document
GetRequest getRequest = new GetRequest("custom-index", "1");
GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
//Delete the document
DeleteRequest deleteDocumentRequest = new DeleteRequest("custom-index", "1"); //Index name followed by the ID.
DeleteResponse deleteResponse = client.delete(deleteDocumentRequest, RequestOptions.DEFAULT);
//Delete the index
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("custom-index"); //Index name.
AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
client.close();
}
}
Elasticsearch OSS Java 高级 REST 客户端
我们建议使用 OpenSearch 客户端连接到 OpenSearch 集群,但如果您必须使用 Elasticsearch OSS Java 高级 REST 客户端,则 Elasticsearch OSS 客户端的 7.10.2 版本也适用于 OpenSearch 的 1.x 版本。
迁移到 OpenSearch Java 高级 REST 客户端
从 Elasticsearch OSS 客户端迁移到 OpenSearch 高级 REST 客户端很简单,只需将您的 Maven 依赖项更改为引用 OpenSearch 的依赖项 即可。
之后,将所有 org.elasticsearch
的引用更改为 org.opensearch
,即可开始向您的 OpenSearch 集群提交请求。