Tablestore provides the ListStream operation to query all streams of a table, the DescribeStream operation to query information about a stream, the GetShardIterator operation to obtain the start iterator in a stream for reading a shard, and the GetStreamRecord operation to obtain the update records of a shard in a stream.
ListStream
You can call the ListStream operation to query all streams of a table in an instance.
The following sample code provides an example on how to query all streams of a table:
private static void listStream(SyncClient client, String tableName) {
ListStreamRequest listStreamRequest = new ListStreamRequest(tableName);
ListStreamResponse result = client.listStream(listStreamRequest);
System.out.println(result.getStreams());
}
DescribeStream
You can call the DescribeStream operation to query information about a stream, such as the time when the stream was created, the validity period of the stream, the status of the stream, the list of shards, and the ID of the next shard to return.
You can query information about all shards of a stream or information about the shards of a stream that meet specific conditions.
Query information about all shards of a stream
The following sample code provides an example on how to query information about all shards of a stream:
private static void describeStream(SyncClient client, String streamId) {
DescribeStreamRequest desRequest = new DescribeStreamRequest(streamId);
DescribeStreamResponse desStream = client.describeStream(desRequest);
// Display the information about the shards.
System.out.println("Shard Info: "desStream.getShards());
// Display the time when the shards were created in microseconds.
System.out.println("Creation Time: "desStream.getCreationTime());
// Display the validity periods of the shards in hours.
System.out.println("Expiration Time: "desStream.getExpirationTime());
}
Query information about shards of a stream that meet specific conditions
The following sample code provides an example on how to query information about specific shards based on the ID of the start shard (InclusiveStartShardId) and the maximum number of shards that can be returned for each request:
private static void describeStream(SyncClient client, String streamId, String shardId) {
DescribeStreamRequest dsRequest = new DescribeStreamRequest(streamId);
// Specify the ID of the start shard. You can obtain the ID of the start shard by querying information about all shards of a stream.
dsRequest.setInclusiveStartShardId(shardId);
dsRequest.setShardLimit(10);
DescribeStreamResponse dscStream = client.describeStream(dsRequest);
// Display the information about specific shards.
System.out.println(dscStream.getShards());
}
GetShardIterator
You can call the GetShardIterator operation to obtain the start iterator in a stream for reading a shard.
The following sample code provides an example on how to obtain the start iterator in a stream for reading a shard:
private static void getShardIterator(SyncClient client, String streamId, String shardId) {
GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest(streamId, shardId);
GetShardIteratorResponse shardIterator = client.getShardIterator(getShardIteratorRequest);
System.out.println(shardIterator.getShardIterator());
}
GetStreamRecord
You can call the GetStreamRecord operation to obtain the update records of a shard.
The following sample code provides an example on how to obtain the first 100 update records of a shard:
private static void getShardIterator(SyncClient client, String shardIterator) {
GetStreamRecordRequest streamRecordRequest = new GetStreamRecordRequest(shardIterator);
streamRecordRequest.setLimit(100);
GetStreamRecordResponse streamRecordResponse = client.getStreamRecord(streamRecordRequest);
List<StreamRecord> records = streamRecordResponse.getRecords();
for(int k=0;k<records.size();k++){
System.out.println("record info:" + records.get(k).toString());
}
System.out.println("next iterator:" + streamRecordResponse.getNextShardIterator());
}