Kinesis data streams CLI

Whats is Kinesis? You can use Amazon Kinesis Data Streams to collect and process large streams of data records in real time. You can create data-processing applications, known as Kinesis Data Streams applications. A typical Kinesis Data Streams application reads data from a data stream as data records. These applications can use the Kinesis Client Library, and they can run on Amazon EC2 instances. You can send the processed records to dashboards, use them to generate alerts, dynamically change pricing and advertising strategies, or send data to a variety of other AWS services.

Step 1: create the stream

Your first step is to create a stream and verify that it was successfully created. Use the following command to create a stream named "cn7030":

In [ ]:
aws kinesis create-stream --stream-name cn7030 --shard-count 1
In [1]:
from IPython.display import display, Image, Math, YouTubeVideo #required to import images
In [ ]:
aws kinesis describe-stream --stream-name cn7030
In [6]:
i = Image(filename='Pictures/describe-stream.png')
i
Out[6]:
In [ ]:
aws kinesis list-streams

Step 2: Put a Record

Now that you have an active stream, you are ready to put some data. For this tutorial, you will use the simplest possible command, put-record, which puts a single data record containing the text "testdata" into the stream:

In [ ]:
aws kinesis put-record --stream-name cn7030 --partition-key 123 --data testdata

Step 3: Get the Record

Before you can get data from the stream you need to obtain the shard iterator for the shard you are interested in. A shard iterator represents the position of the stream and shard from which the consumer (get-record command in this case) will read. You'll use the get-shard-iterator command, as follows:

In [ ]:
aws kinesis get-shard-iterator --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON --stream-name cn7030

"ShardIterator": "AAAAAAAAAAHinH2hdJkH7GPEtULpeyo/I0L+l/6dj9L88reER24d3jiPgcfiqdc4up+0wexlzAJDSzpJBAjdBIoyafsuR1p++IxOz5uZdCrh5BiK4otUeq7ycRV7HcQtn6NoYe1/ZrM+4DrvqfdrBGCGbg14HuzCBvAV2nT2xa1AdGN9dBMqtFK/Z8oYWglNM7O0paAzsPKYmZDo5vZ7oDTtiSWghfkH"

The long string of seemingly random characters is the shard iterator (yours will be different). You will need to copy/paste the shard iterator into the get command, shown next. Shard iterators have a valid lifetime of 300 seconds, which should be enough time for you to copy/paste the shard iterator into the next command. Notice you will need to remove any newlines from your shard iterator before pasting to the next command. If you get an error message that the shard iterator is no longer valid, simply execute the get-shard-iterator command again.

The get-records command gets data from the stream, and it resolves to a call to GetRecords in the Kinesis Data Streams API. The shard iterator specifies the position in the shard from which you want to start reading data records sequentially. If there are no records available in the portion of the shard that the iterator points to, GetRecords returns an empty list. Note that it might take multiple calls to get to a portion of the shard that contains records.

In [ ]:
aws kinesis get-records --shard-iterator AAAAAAAAAAHinH2hdJkH7GPEtULpeyo/I0L+l/6dj9L88reER24d3jiPgcfiqdc4up+0wexlzAJDSzpJBAjdBIoyafsuR1p++IxOz5uZdCrh5BiK4otUeq7ycRV7HcQtn6NoYe1/ZrM+4DrvqfdrBGCGbg14HuzCBvAV2nT2xa1AdGN9dBMqtFK/Z8oYWglNM7O0paAzsPKYmZDo5vZ7oDTtiSWghfkH

aws kinesis get-records --shard-iterator AAAAAAAAAAHMn1s9IOGHpUCtL7pJewQVKpGkCGcoKPe0MNEIcgQkMUAZWu33LPXE7q9S/ITDCzS0598FotIsyfHCPmQoz4DIPhfJwGJ/sadQEHBWy5jB7pSSeoUwFG+rwa+QcabvkuJDrf/n0pVjA9KixwHIfhsoyrXjzXZba0NBGpYuFAuNuzM2uGQYLGlrxLOJGhjOZtp/Td6CuSLIIbM7LBbnoLX3

In [3]:
i = Image(filename='Pictures/get-records.png')
i
Out[3]:

Note that get-records is described above as a request, which means you may receive zero or more records even if there are records in your stream, and any records returned may not represent all the records currently in your stream. This is perfectly normal, and production code will simply poll the stream for records at appropriate intervals (this polling speed will vary depending on your specific application design requirements).

The first thing you'll likely notice about your record in this part of the tutorial is that the data appears to be garbage –; it's not the clear text testdata we sent. This is due to the way put-record uses Base64 encoding to allow you to send binary data. However, the Kinesis Data Streams support in the AWS CLI does not provide Base64 decoding because Base64 decoding to raw binary content printed to stdout can lead to undesired behavior and potential security issues on certain platforms and terminals. If you use a Base64 decoder (for example, https://www.base64decode.org/) to manually decode dGVzdGRhdGE= you will see that it is, in fact, testdata. This is sufficient for the sake of this tutorial because, in practice, the AWS CLI is rarely used to consume data, but more often to monitor the state of the stream and obtain information, as shown previously (describe-stream and list-streams). Future tutorials will show you how to build production-quality consumer applications using the Kinesis Client Library (KCL), where Base64 is taken care of for you.

In [11]:
Image(url='http://richardfrancis.info/images/decode.png')
Out[11]:

It's not always the case that get-records will return all records in the stream/shard specified. When that happens, use the NextShardIterator from the last result to get the next set of records. So if more data were being put into the stream (the normal situation in production applications), you could keep polling for data using get-records each time. However, if you do not call get-records using the next shard iterator within the 300 second shard iterator lifetime, you will get an error message, and you will need to use the get-shard-iterator command to get a fresh shard iterator.

Also provided in this output is MillisBehindLatest, which is the number of milliseconds the GetRecords operation's response is from the tip of the stream, indicating how far behind current time the consumer is. A value of zero indicates record processing is caught up, and there are no new records to process at this moment. In the case of this tutorial, you may see a number that's quite large if you've been taking time to read along as you go. That's not a problem, data records will stay in a stream for 24 hours waiting for you to retrieve them. This time frame is called the retention period and it is configurable up to 168 hours (7 days).

Note that a successful get-records result will always have a NextShardIterator even if there are no more records currently in the stream. This is a polling model that assumes a producer is potentially putting more records into the stream at any given time. Although you can write your own polling routines, if you use the previously mentioned KCL for developing consumer applications, this polling is taken care of for you.

If you call get-records until there are no more records in the stream and shard you are pulling from, you will see output with empty records similar to the following example (scroll horizontally to see the entire output):

Step 4: Clean Up

In [ ]:
aws kinesis delete-stream --stream-name cn7030
In [ ]:
aws kinesis describe-stream --stream-name cn7030