How do I report the status of a Kinesis Shard?
1 answer
You can use Java SDK for Amazon Web Services: https://github.com/aws/aws-sdk-java
There are many useful methods for accessing your resources.
Edit: Sorry, I misunderstood the question. You cannot access shard status directly (yet). But there is a trick: a closed shard always has an "Ending Sequence Number". You can hack this way.
Excerpt from the Javadoc;
public String getEndingSequenceNumber ()
The ending sequence number for the range. Shards in the OPEN state have a final sequence number of zero.
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();
}
}
}
-
+6
source to share