如何通过循环获取亚马逊中的所有日志事件?

时间:2017-11-13 12:16:14

标签: java amazon-web-services amazon-dynamodb amazon-cloudwatchlogs

每个亚马逊API都有自己的令牌,您必须将其设置为下一个请求。但是使用aws log api我得到了无限循环:

public class Some {
    public static void main (String[] args) {
        final GetLogEventsRequest request = new GetLogEventsRequest()
                .withLogGroupName("myGroup")
                .withLogStreamName("myStrean");
        final AWSLogs awsLogs = AWSLogsClientBuilder.defaultClient();
        Collection<OutputLogEvent> result = new ArrayList<>();

        GetLogEventsResult response = null;
        do {
            response = awsLogs.getLogEvents(request);
            result.addAll(response.getEvents());
            request.withNextToken(response.getNextBackwardToken());
        } while (response.getNextBackwardToken() != null);
    }
}

来自文档:

  

nextBackwardToken

     

向后方向的下一组项目的标记。令牌在24小时后到期。此标记永远不会为空。如果您已到达流的末尾,它将返回您传入的相同令牌。

所以当你LastEvaluatedKey dynamodb时它不能像scan那样为空:

Map<String, AttributeValue> lastKeyEvaluated = null;
do {
    ScanRequest scanRequest = new ScanRequest()
        .withTableName("ProductCatalog")
        .withLimit(10)
        .withExclusiveStartKey(lastKeyEvaluated);

    ScanResult result = client.scan(scanRequest);
    for (Map<String, AttributeValue> item : result.getItems()){
        printItem(item);
    }
    lastKeyEvaluated = result.getLastEvaluatedKey();
} while (lastKeyEvaluated != null);

那么如果我们谈论log api,我应该传递给request.withNextToken ???如果nextBackwardToken(和nextForwardToken也不能为空 - 如何检测我收到亚马逊的最后一个回复???

1 个答案:

答案 0 :(得分:0)

您引用的文档非常简单。我想你需要这样的东西:

    final AWSLogs awsLogs = AWSLogsClientBuilder.defaultClient();
    Collection<OutputLogEvent> result = new ArrayList<>();

    String nextToken = null;
    GetLogEventsResult response;
    do {
        GetLogEventsRequest request = new GetLogEventsRequest()
                .withLogGroupName("myGroup")
                .withLogStreamName("myStrean");
        if (nextToken != null) request = request.withNextToken(nextToken);

        response = awsLogs.getLogEvents(request);
        result.addAll(response.getEvents());

        // check if token is the same
        if (response.getNextForwardToken().equals(nextToken)) break;
        // save new token
        nextToken = response.getNextForwardToken();
    } while (true);

因此,您只需要每次使用新令牌创建请求,直到它变得等于旧令牌。