如何判断Kinesis碎片的状态?

时间:2014-10-11 18:45:58

标签: amazon-web-services amazon-kinesis

有没有办法告诉碎片的状态是什么,无论是开放,关闭还是过期?我能够确定此信息的唯一方法似乎是尝试在分片上进行操作。

1 个答案:

答案 0 :(得分:6)

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

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

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

摘自Javadoc;

  

public String 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();
        }
    }
}

-