亚马逊竞价历史价格 - Java 代码

4
可以获取过去90天的AWS Spot价格历史记录。使用Java SDK时,可以创建一个查询来获取部分历史记录,但由于此列表非常长,因此会将其拆分。使用令牌,您应该能够获取列表的下一部分,并重复此操作,直到接收整个列表为止。
问题在于,使用给定的令牌,我尚未能够检索超出列表第一部分的内容。在搜索互联网时,清楚地表明了对该令牌的理解是正确的。
    // Create the AmazonEC2Client object so we can call various APIs.
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);

    // Get the spot price history
    DescribeSpotPriceHistoryResult result = ec2.describeSpotPriceHistory();

    // Print first part of list
    for (int i = 0; i < result.getSpotPriceHistory().size(); i++) {
        System.out.println(result.getSpotPriceHistory().get(i));
    }

    result = result.withNextToken(result.getNextToken());

    // Print second part of list
    for (int i = 0; i < result.getSpotPriceHistory().size(); i++) {
            System.out.println(result.getSpotPriceHistory().get(i));
    }

查询结果中的“nextToken”没有发生变化。我做错了什么?SDK有问题吗?我是通过Eclipse安装的。

提前致谢!

1个回答

2
您确实没有按预期使用API-您需要使用从DescribeSpotPriceHistoryResult检索到的nextToken重新提交DescribeSpotPriceHistoryRequest(诚然,可以在后者上设置nextToken有点令人困惑,猜测它理想情况下应该只是一个内部方法),例如:
// Create the AmazonEC2Client object so we can call various APIs.
AmazonEC2 ec2 = new AmazonEC2Client(credentials);

// Get the spot price history
String nextToken = "";
do {
    // Prepare request (include nextToken if available from previous result)
    DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest()
            .withNextToken(nextToken);

    // Perform request
    DescribeSpotPriceHistoryResult result = ec2
            .describeSpotPriceHistory(request);
    for (int i = 0; i < result.getSpotPriceHistory().size(); i++) {
        System.out.println(result.getSpotPriceHistory().get(i));
    }

    // 'nextToken' is the string marking the next set of results returned (if any), 
    // it will be empty if there are no more results to be returned.            
    nextToken = result.getNextToken();

} while (!nextToken.isEmpty());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接