Menu
Open source

listShards

KinesisClient.listShards(streamName, [options]) lists the shards in a Kinesis stream.

Parameters

ParameterTypeDescription
streamNamestringThe name of the Kinesis stream.
optionsobjectOptional configuration for the list shards operation.

Options

ParameterTypeDescription
nextTokenstringThe token to use for pagination.
exclusiveStartShardIdstringThe shard ID to start listing from (exclusive).
maxResultsnumberThe maximum number of shards to return.

Returns

TypeDescription
Promise<Object>A Promise that fulfills with the list of shards.

Returns object

PropertyTypeDescription
shardsArrayAn array of shard objects.
nextTokenstringThe token to use for the next page of results.

Shard object

PropertyTypeDescription
shardIdstringThe unique identifier of the shard.
parentShardIdstring (optional)The shard ID of the parent shard (if any).
adjacentParentShardIdstring (optional)The shard ID of the adjacent parent shard (if any).
hashKeyRangeObjectThe hash key range for the shard.
sequenceNumberRangeObjectThe sequence number range for the shard.

Example

JavaScript
import {
  AWSConfig,
  KinesisClient,
} from 'https://jslib.k6.io/aws/0.14.0/kinesis.js';

const awsConfig = new AWSConfig({
  region: __ENV.AWS_REGION,
  accessKeyId: __ENV.AWS_ACCESS_KEY_ID,
  secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY,
});

const kinesis = new KinesisClient(awsConfig);

export default async function () {
  const streamName = 'my-test-stream';

  // List all shards in the stream
  const shardsResponse = await kinesis.listShards(streamName);

  console.log('Number of shards:', shardsResponse.shards.length);

  // Display information about each shard
  shardsResponse.shards.forEach((shard, index) => {
    console.log(`Shard ${index}:`);
    console.log('  Shard ID:', shard.shardId);
    console.log('  Parent Shard ID:', shard.parentShardId || 'None');
    console.log('  Adjacent Parent Shard ID:', shard.adjacentParentShardId || 'None');
    console.log('  Hash Key Range:', shard.hashKeyRange);
    console.log('  Sequence Number Range:', shard.sequenceNumberRange);
  });

  // List shards with pagination
  const limitedShards = await kinesis.listShards(streamName, { maxResults: 2 });
  console.log('Limited shards:', limitedShards.shards.length);

  // Continue pagination if there are more shards
  if (limitedShards.nextToken) {
    const nextPage = await kinesis.listShards(streamName, {
      nextToken: limitedShards.nextToken,
    });
    console.log('Next page shards:', nextPage.shards.length);
  }
}