本章节以ListSearchLog为例,为您演示如何通过阿里云CLI、OpenAPI Explorer和阿里云SDK等开发者工具调用阿里云Elasticsearch
API。
背景信息
调用API时,您可以根据API文档了解使用说明,并查询必选的请求参数。发送请求后报错时,您可以在相应API文档中获取错误码说明。
阿里云CLI示例
- 获取实例ID。
- 使用ListSearchLog获取实例的系统日志。
aliyun elasticsearch ListSearchLog --type INSTANCELOG --query level:warn --beginTime 1593332477000 --endTime 1593418877000 --page 1 --size 20 --InstanceId es-cn-n6w1o1x0w00******
Java SDK示例
- 创建Maven工程。
- 配置pom依赖。
<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>
- 创建Java程序,调用ListSearchLog查询实例的系统日志。
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();
}
}
}