This topic describes how to use developer tools, such as Alibaba Cloud CLI, OpenAPI
Explorer, and Alibaba Cloud SDKs, to call an Elasticsearch API operation. The ListSearchLog
API operation is used in the examples in this topic.
Background information
Before you call an API operation, you must understand related instructions in API documentation and obtain the values of the required request parameters. If an error code is returned
after you send a request, you can obtain the description of the error code in the
API documentation.
Methods to make API requests
Use Alibaba Cloud CLI to call an Elasticsearch API operation
- Obtain the ID of the cluster that you want to access.
You can use one of the following methods to obtain the ID:
- Call the ListSearchLog operation to query the system logs of the cluster.
aliyun elasticsearch ListSearchLog --type INSTANCELOG --query level:warn --beginTime 1593332477000 --endTime 1593418877000 --page 1 --size 20 --InstanceId es-cn-n6w1o1x0w00******
Use OpenAPI Explorer to call an Elasticsearch API operation
- Call the ListInstance operation to obtain the ID of the cluster that you want to access.
- Call the ListSearchLog operation to query the system logs of the cluster.
If the query is successful, OpenAPI Explorer displays related logs.
Use SDK for Java to call an Elasticsearch API operation
- Create a Maven project.
- Add Project Object Model (POM) dependencies.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-elasticsearch</artifactId>
<version>3.0.16</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.6</version>
</dependency>
Notice SDK packages are frequently updated. We recommend that you visit the
Alibaba Cloud SDK Center to obtain dependencies of the latest version.
- Create a Java program and call the ListSearchLog operation to query the system logs of the cluster that you want to access.
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
public class ListSearchLog {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("<RegionId>", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.GET);
request.setDomain("elasticsearch.<RegionId>.aliyuncs.com");
request.setVersion("2017-06-13");
request.setUriPattern("/openapi/instances/<instanceid>/search-log");
request.putQueryParameter("type", "INSTANCELOG");
request.putQueryParameter("query", "level:warn");
request.putQueryParameter("beginTime", "1593332477000");
request.putQueryParameter("endTime", "1593418877000");
request.putQueryParameter("page", "1");
request.putQueryParameter("size", "20");
request.putHeadParameter("Content-Type", "application/json");
String requestBody = "" +
"{}";
request.setHttpContent(requestBody.getBytes(), "utf-8", FormatType.JSON);
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
Parameter |
Description |
<RegionId> |
The ID of the region where the cluster resides. For more information about how to
obtain the ID, see Parameters.
|
<accessKeyId> |
The AccessKey ID of your Alibaba Cloud account. For more information about how to
obtain the AccessKey ID, see Obtain an AccessKey pair.
|
<accessSecret> |
The AccessKey secret of your Alibaba Cloud account. For more information about how
to obtain the AccessKey secret, see Obtain an AccessKey pair.
|
<instanceId> |
The ID of the cluster. For more information about how to obtain the ID, see View the basic information of a cluster and ListInstance.
|
Note For more information about request parameters, see
ListSearchLog.