简体   繁体   中英

How do I tell the status of a Kinesis shard?

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.

You can use Amazon Web Services Java SDK: https://github.com/aws/aws-sdk-java

There are a lot of useful methods for accessing your resources.

Edit: Sorry, I misunderstood the question. You cannot access a shard's status directly (yet). But there is a trick: A Closed shard always has an "Ending Sequence Number" defined. You can hack this way.

Excerpt from Javadoc;

public String getEndingSequenceNumber ()

The ending sequence number for the range. Shards that are in the OPEN state have an ending sequence number of null.

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

-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM