3

Is there a way to tell what a shard's status is, whether it's OPEN, CLOSED, or EXPIRED? The only way I've been able to determine this information seems to be attempting an operation on the shard.

4

1 回答 1

9

您可以使用 Amazon Web Services Java SDK:https ://github.com/aws/aws-sdk-java

有很多有用的方法可以访问您的资源。

编辑:对不起,我误解了这个问题。您不能直接访问分片的状态(目前)。但是有一个技巧:一个封闭的分片总是定义一个“结束序列号”。你可以这样破解。

摘自 Javadoc;

公共字符串getEndingSequenceNumber ()

范围的结束序列号。处于 OPEN 状态的分片的结束序列号为空。

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/kinesis/model/SequenceNumberRange.html#getEndingSequenceNumber()

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.kinesis.AmazonKinesis;
import com.amazonaws.services.kinesis.AmazonKinesisClient;
import com.amazonaws.services.kinesis.model.DescribeStreamRequest;
import com.amazonaws.services.kinesis.model.DescribeStreamResult;
import com.amazonaws.services.kinesis.model.ListStreamsResult;

public class KinesisSandbox {

    public static void main(String[] args) {
        try {
            String amazonKey = "x";
            String amazonSecret = "y";
            AmazonKinesis client = new AmazonKinesisClient(new BasicAWSCredentials(amazonKey, amazonSecret));

            ListStreamsResult listStreamsResult = client.listStreams();
            System.out.println("\nlistStreamsResult: " + listStreamsResult);

            String streamName = listStreamsResult.getStreamNames().get(0);

            DescribeStreamRequest describeStreamRequest = new DescribeStreamRequest();
            describeStreamRequest.setStreamName(streamName);
            DescribeStreamResult describeStreamResult = client.describeStream(describeStreamRequest);
            System.out.println("\n describeStreamResult.getStreamDescription().getStreamStatus(): "
                    + describeStreamResult.getStreamDescription().getStreamStatus());
                // System.out.println("\ndescribeStreamResult: " + describeStreamResult);

            List<Shard> shards = describeStreamResult.getStreamDescription().getShards();
            for (int i = 0; i < shards.size(); i++) {
                Shard shard = shards.get(i);
                if (shard.getSequenceNumberRange().getEndingSequenceNumber() == null) {
                    System.out.println("shard(" + i + "): " + shard.getShardId() + " is OPEN.");
                } else {
                    System.out.println("shard(" + i + "): " + shard.getShardId() + " is CLOSED.");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

-

于 2014-10-14T13:41:05.157 回答