|
| 1 | +--- |
| 2 | +title: 'KinesisClient' |
| 3 | +description: 'KinesisClient allows interacting with AWS Kinesis streams' |
| 4 | +weight: 00 |
| 5 | +--- |
| 6 | + |
| 7 | +# KinesisClient |
| 8 | + |
| 9 | +{{< docs/shared source="k6" lookup="blocking-aws-blockquote.md" version="<K6_VERSION>" >}} |
| 10 | + |
| 11 | +`KinesisClient` interacts with the AWS Kinesis service. |
| 12 | + |
| 13 | +With it, you can perform operations such as creating streams, putting records, listing streams, and reading records from streams. For a full list of supported operations, see [Methods](#methods). |
| 14 | + |
| 15 | +Both the dedicated `kinesis.js` jslib bundle and the all-encompassing `aws.js` bundle include the `KinesisClient`. |
| 16 | + |
| 17 | +### Methods |
| 18 | + |
| 19 | +| Function | Description | |
| 20 | +| :---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------- | |
| 21 | +| [createStream(streamName, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/kinesisclient/createstream) | Create a new Kinesis stream | |
| 22 | +| [deleteStream(streamName)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/kinesisclient/deletestream) | Delete a Kinesis stream | |
| 23 | +| [listStreams([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/kinesisclient/liststreams) | List available Kinesis streams | |
| 24 | +| [putRecords(streamName, records)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/kinesisclient/putrecords) | Put multiple records into a Kinesis stream | |
| 25 | +| [getRecords(shardIterator, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/kinesisclient/getrecords) | Get records from a Kinesis stream shard | |
| 26 | +| [listShards(streamName, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/kinesisclient/listshards) | List shards in a Kinesis stream | |
| 27 | +| [getShardIterator(streamName, shardId, shardIteratorType, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/aws/kinesisclient/getsharditerator) | Get a shard iterator for reading records from a stream | |
| 28 | + |
| 29 | +### Throws |
| 30 | + |
| 31 | +KinesisClient methods will throw errors in case of failure. |
| 32 | + |
| 33 | +| Error | Condition | |
| 34 | +| :-------------------- | :--------------------------------------------------------- | |
| 35 | +| InvalidSignatureError | When invalid credentials are provided. | |
| 36 | +| KinesisServiceError | When AWS replies to the requested operation with an error. | |
| 37 | + |
| 38 | +### Examples |
| 39 | + |
| 40 | +<!-- md-k6:skip --> |
| 41 | + |
| 42 | +```javascript |
| 43 | +import { check } from 'k6'; |
| 44 | +import exec from 'k6/execution'; |
| 45 | + |
| 46 | +import { |
| 47 | + AWSConfig, |
| 48 | + KinesisClient, |
| 49 | +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/kinesis.js'; |
| 50 | + |
| 51 | +const awsConfig = new AWSConfig({ |
| 52 | + region: __ENV.AWS_REGION, |
| 53 | + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, |
| 54 | + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, |
| 55 | +}); |
| 56 | + |
| 57 | +const kinesis = new KinesisClient(awsConfig); |
| 58 | +const testStreamName = 'test-stream'; |
| 59 | + |
| 60 | +export default async function () { |
| 61 | + // List available streams |
| 62 | + const streams = await kinesis.listStreams(); |
| 63 | + console.log('Available streams:', streams.streamNames); |
| 64 | + |
| 65 | + // Check if our test stream exists |
| 66 | + if (!streams.streamNames.includes(testStreamName)) { |
| 67 | + // Create the stream if it doesn't exist |
| 68 | + await kinesis.createStream(testStreamName, { shardCount: 1 }); |
| 69 | + console.log(`Created stream: ${testStreamName}`); |
| 70 | + } |
| 71 | + |
| 72 | + // Put some records into the stream |
| 73 | + const records = [ |
| 74 | + { |
| 75 | + data: JSON.stringify({ message: 'Hello from k6!', timestamp: Date.now() }), |
| 76 | + partitionKey: 'test-partition-1', |
| 77 | + }, |
| 78 | + { |
| 79 | + data: JSON.stringify({ message: 'Another message', timestamp: Date.now() }), |
| 80 | + partitionKey: 'test-partition-2', |
| 81 | + }, |
| 82 | + ]; |
| 83 | + |
| 84 | + const putResult = await kinesis.putRecords(testStreamName, records); |
| 85 | + console.log('Put records result:', putResult); |
| 86 | + |
| 87 | + // List shards in the stream |
| 88 | + const shards = await kinesis.listShards(testStreamName); |
| 89 | + console.log('Stream shards:', shards.shards); |
| 90 | + |
| 91 | + // Get a shard iterator for reading records |
| 92 | + if (shards.shards.length > 0) { |
| 93 | + const shardId = shards.shards[0].shardId; |
| 94 | + const shardIterator = await kinesis.getShardIterator(testStreamName, shardId, 'TRIM_HORIZON'); |
| 95 | + |
| 96 | + // Get records from the shard |
| 97 | + const getResult = await kinesis.getRecords(shardIterator.shardIterator); |
| 98 | + console.log('Retrieved records:', getResult.records); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +#### Stream management |
| 104 | + |
| 105 | +<!-- md-k6:skip --> |
| 106 | + |
| 107 | +```javascript |
| 108 | +import { |
| 109 | + AWSConfig, |
| 110 | + KinesisClient, |
| 111 | +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/kinesis.js'; |
| 112 | + |
| 113 | +const awsConfig = new AWSConfig({ |
| 114 | + region: __ENV.AWS_REGION, |
| 115 | + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, |
| 116 | + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, |
| 117 | +}); |
| 118 | + |
| 119 | +const kinesis = new KinesisClient(awsConfig); |
| 120 | + |
| 121 | +export default async function () { |
| 122 | + const streamName = 'my-test-stream'; |
| 123 | + |
| 124 | + // Create a stream with on-demand billing |
| 125 | + await kinesis.createStream(streamName, { |
| 126 | + streamModeDetails: { |
| 127 | + streamMode: 'ON_DEMAND', |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + // List all streams |
| 132 | + const streams = await kinesis.listStreams(); |
| 133 | + console.log('All streams:', streams.streamNames); |
| 134 | + |
| 135 | + // Clean up - delete the stream |
| 136 | + await kinesis.deleteStream(streamName); |
| 137 | + console.log(`Deleted stream: ${streamName}`); |
| 138 | +} |
| 139 | +``` |
0 commit comments