Menu
Amazon Kinesis Streams
Developer Guide

Creating a Stream

Use the following steps to create your Amazon Kinesis stream.

Create the Streams Client

Before you can work with Amazon Kinesis streams you must instantiate a client object. The following Java code creates the Streams client and sets the endpoint information for the client. This overload of setEndpoint also includes the service name and region. Set serviceName to kinesis.

Copy
client = new AmazonKinesisClient(); client.setEndpoint(endpoint, serviceName, regionId);

For more information, see Streams Regions and Endpoints in the AWS General Reference.

Create the Stream

Now that you have created your Streams client, you can create a stream to work with, which you can accomplish with the Streams console, or programmatically. To create a stream programmatically, instantiate a CreateStreamRequest object and specify a name for the stream and the number of shards for the stream to use.

Copy
CreateStreamRequest createStreamRequest = new CreateStreamRequest(); createStreamRequest.setStreamName( myStreamName ); createStreamRequest.setShardCount( myStreamSize );

The stream name identifies the stream. The name is scoped to the AWS account used by the application. It is also scoped by region. That is, two streams in two different AWS accounts can have the same name, and two streams in the same AWS account but in two different regions can have the same name, but not two streams on the same account and in the same region.

The throughput of the stream is a function of the number of shards; more shards are required for greater provisioned throughput. More shards also increase the cost that AWS charges for the stream. For more information, about calculating an appropriate number of shards for your application, see Determining the Initial Size of an Amazon Kinesis Stream.

After the createStreamRequest object is configured, create a stream by calling the createStream method on the client. After calling createStream, wait for the stream to reach the ACTIVE state before performing any operations on the stream. To check the state of the stream, call the describeStream method. However, describeStream throws an exception if the stream does not exist. Therefore, enclose the describeStream call in a try/catch block.

Copy
client.createStream( createStreamRequest ); DescribeStreamRequest describeStreamRequest = new DescribeStreamRequest(); describeStreamRequest.setStreamName( myStreamName ); long startTime = System.currentTimeMillis(); long endTime = startTime + ( 10 * 60 * 1000 ); while ( System.currentTimeMillis() < endTime ) { try { Thread.sleep(20 * 1000); } catch ( Exception e ) {} try { DescribeStreamResult describeStreamResponse = client.describeStream( describeStreamRequest ); String streamStatus = describeStreamResponse.getStreamDescription().getStreamStatus(); if ( streamStatus.equals( "ACTIVE" ) ) { break; } // // sleep for one second // try { Thread.sleep( 1000 ); } catch ( Exception e ) {} } catch ( ResourceNotFoundException e ) {} } if ( System.currentTimeMillis() >= endTime ) { throw new RuntimeException( "Stream " + myStreamName + " never went active" ); }