This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
getShardIterator
KinesisClient.getShardIterator(streamName, shardId, shardIteratorType, [options]) gets a shard iterator for reading records from a Kinesis stream shard.
Parameters
Shard Iterator Types
Options
Returns
Returns object
Example
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';
// First, get the shards for the stream
const shards = await kinesis.listShards(streamName);
if (shards.shards.length > 0) {
const shardId = shards.shards[0].shardId;
// Get a shard iterator starting from the beginning
const trimHorizonIterator = await kinesis.getShardIterator(streamName, shardId, 'TRIM_HORIZON');
console.log('Trim horizon iterator:', trimHorizonIterator.shardIterator);
// Get a shard iterator starting from the latest records
const latestIterator = await kinesis.getShardIterator(streamName, shardId, 'LATEST');
console.log('Latest iterator:', latestIterator.shardIterator);
// Get a shard iterator starting from a specific timestamp
const timestampIterator = await kinesis.getShardIterator(streamName, shardId, 'AT_TIMESTAMP', {
timestamp: new Date(Date.now() - 3600000), // 1 hour ago
});
console.log('Timestamp iterator:', timestampIterator.shardIterator);
// Use the iterator to read records
const records = await kinesis.getRecords(trimHorizonIterator.shardIterator);
console.log('Records retrieved:', records.records.length);
// If we have records, we can get an iterator starting after a specific sequence number
if (records.records.length > 0) {
const sequenceNumber = records.records[0].sequenceNumber;
const afterSequenceIterator = await kinesis.getShardIterator(
streamName,
shardId,
'AFTER_SEQUENCE_NUMBER',
{
startingSequenceNumber: sequenceNumber,
}
);
console.log('After sequence iterator:', afterSequenceIterator.shardIterator);
}
}
}

