本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
适用于 AWS 运行状况 API 的 Java 代码示例
以下 Java 代码示例将演示如何初始化 AWS 运行状况 客户端,并检索有关事件和实体的信息。
步骤 1: 初始化凭证
需要有效凭证,才可以与 AWS 运行状况 API 进行通信。您可以使用与 AWS 账户关联的任何 IAM 用户的密钥对。
AWSCredentials credentials = null;
try {
credentials = new ProfileCredentialsProvider("default").getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. "
+ "Please make sure that your credentials file is at the correct "
+ "location (/home/username/.aws/credentials), and is in valid format.", e);
}
步骤 2: 初始化 AWS 运行状况 API 客户端
使用上一步中的初始化凭证对象来创建 AWS 运行状况 客户端:
import com.amazonaws.services.health.AWSHealthClient;
AWSHealth awsHealthClient = new AWSHealthClient(credentials);
步骤 3: 使用 AWS 运行状况 API 操作获取事件信息
DescribeEvents
import com.amazonaws.services.health.model.DescribeEventsRequest;
import com.amazonaws.services.health.model.DescribeEventsResult;
import com.amazonaws.services.health.model.Event;
import com.amazonaws.services.health.model.EventFilter;
DescribeEventsRequest request = new DescribeEventsRequest();
EventFilter filter = new EventFilter();
// Filter on any field from the supported AWS Health EventFilter model.
// Here is an example for Region us-east-1 events from the EC2 service.
filter.setServices(singletonList("EC2"));
filter.setRegions(singletonList("us-east-1"));
request.setFilter(filter);
DescribeEventsResult response = awsHealthClient.describeEvents(request);
List resultEvents = response.getEvents();
Event currentEvent = null;
for (Event event : resultEvents) {
// Display result event data; here is a subset.
System.out.println(event.getArn());
System.out.println(event.getService());
System.out.println(event.getRegion());
System.out.println(event.getAvailabilityZone());
System.out.println(event.getStartTime());
System.out.println(event.getEndTime());
}
DescribeEventAggregates
import com.amazonaws.services.health.model.DescribeEventAggregatesRequest;
import com.amazonaws.services.health.model.DescribeEventAggregatesResult;
import com.amazonaws.services.health.model.EventAggregate;
import com.amazonaws.services.health.model.EventFilter;
DescribeEventAggregatesRequest request = new DescribeEventAggregatesRequest();
// set the aggregation field
request.setAggregateField("eventTypeCategory");
// filter more on result if needed
EventFilter filter = new EventFilter();
filter.setRegions(singleton("us-east-1"));
request.setFilter(filter);
DescribeEventAggregatesResult response = awsHealthClient.describeEventAggregates(request);
// print event count for each eventTypeCategory
for (EventAggregate aggregate: response.getEventAggregates()) {
System.out.println("Event Category:" + aggregate.getAggregateValue());
System.out.println("Event Count:" + aggregate.getCount());
}
DescribeEventDetails
import com.amazonaws.services.health.model.DescribeEventDetailsRequest;
import com.amazonaws.services.health.model.DescribeEventDetailsResult;
import com.amazonaws.services.health.model.Event;
import com.amazonaws.services.health.model.EventDetails;
DescribeEventDetailsRequest describeEventDetailsRequest = new DescribeEventDetailsRequest();
// set event ARN and local value
describeEventDetailsRequest.setEventArns(singletonList("arn:aws:health:us-east-1::event/service/eventTypeCode/eventId"));
describeEventDetailsRequest.setLocale("en-US");
filter.setEventArns
DescribeEventDetailsResult describeEventDetailsResult = awsHealthClient.describeEventDetails(request);
EventDetails eventDetail = describeEventDetailsResult.getSuccessfulSet().get(0);
// check event-related fields
Event event = eventDetail.getEvent();
System.out.println(event.getService());
System.out.println(event.getRegion());
System.out.println(event.getAvailabilityZone());
System.out.println(event.getStartTime());
System.out.println(event.getEndTime());
// print out event description
System.out.println(eventDetail.getEventDescription().getLatestDescription());
DescribeAffectedEntities
import com.amazonaws.services.health.model.AffectedEntity;
import com.amazonaws.services.health.model.DateTimeRange;
import com.amazonaws.services.health.model.DescribeAffectedEntitiesRequest;
import com.amdescribeEventDetailsRequestazonaws.services.health.model.DescribeAffectedEntitiesResult;
DescribeAffectedEntitiesRequest request = new DescribeAffectedEntitiesRequest();
EntityFilter filter = new EntityFilter();
filter.setEventArns(singletonList("arn:aws:health:us-east-1::event/service/eventTypeCode/eventId"));
DescribeAffectedEntitiesResult response = awsHealthClient.describeAffectedEntities(request);
for (AffectedEntity affectedEntity: response.getEntities()) {
System.out.println(affectedEntity.getEntityValue());
System.out.println(affectedEntity.getAwsAccountId());
System.out.println(affectedEntity.getEntityArn());
}
DescribeEntityAggregates
import com.amazonaws.services.health.model.DescribeEntityAggregatesRequest;
import com.amazonaws.services.health.model.DescribeEntityAggregatesResult;
import com.amazonaws.services.health.model.EntityAggregate;
DescribeEntityAggregatesRequest request = new DescribeEntityAggregatesRequest();
request.setEventArns(singletonList("arn:aws:health:us-east-1::event/service/eventTypeCode/eventId"));
DescribeEntityAggregatesResult response = awsHealthClient.describeEntityAggregates(request);
for (EntityAggregate entityAggregate : response.getEntityAggregates()) {
System.out.println(entityAggregate.getEventArn());
System.out.println(entityAggregate.getCount());
}