Example usage for com.amazonaws.services.kinesis AmazonKinesisClient createStream

List of usage examples for com.amazonaws.services.kinesis AmazonKinesisClient createStream

Introduction

In this page you can find the example usage for com.amazonaws.services.kinesis AmazonKinesisClient createStream.

Prototype

@Override
public CreateStreamResult createStream(CreateStreamRequest request) 

Source Link

Document

Creates a Kinesis data stream.

Usage

From source file:whgHelper.java

License:Open Source License

public static void setStream(AmazonKinesisClient kinesis, String streamName, int shardCount) {

    try {//w  ww  .  j  a v a2s  . c o  m
        // Describe the stream and check if it exists
        DescribeStreamRequest describeStreamRequest = new DescribeStreamRequest().withStreamName(streamName);
        StreamDescription streamDescription = kinesis.describeStream(describeStreamRequest)
                .getStreamDescription();
        System.out.printf("Stream %s has a status of %s.\n", streamName, streamDescription.getStreamStatus());

        if ("DELETING".equals(streamDescription.getStreamStatus())) {
            System.out.println("Stream is being deleted. This sample will now exit.");
            System.exit(0);
        }

        // Wait for the stream to become active if it is not yet ACTIVE.
        if (!"ACTIVE".equals(streamDescription.getStreamStatus())) {
            try {
                waitForStreamToBecomeAvailable(kinesis, streamName);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } catch (ResourceNotFoundException ex) {

        System.out.printf("Stream %s does not exist. Creating it now.\n", streamName);
        // Create a stream. The number of shards determines the provisioned throughput.
        CreateStreamRequest createStreamRequest = new CreateStreamRequest();
        createStreamRequest.setStreamName(streamName);
        createStreamRequest.setShardCount(shardCount);
        kinesis.createStream(createStreamRequest);
        // The stream is now being created. Wait for it to become active.
        try {
            waitForStreamToBecomeAvailable(kinesis, streamName);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.amazon.services.awsrum.utils.KinesisUtils.java

License:Open Source License

/**
 * Creates a Kinesis stream if it does not exist and waits for it to become available
 * /*  w ww . j a  v a 2s .c o m*/
 * @param kinesisClient
 *            The {@link AmazonKinesisClient} with Kinesis read and write privileges
 * @param streamName
 *            The Kinesis stream name to create
 * @param shardCount
 *            The shard count to create the stream with
 * @throws IllegalStateException
 *             Invalid Kinesis stream state
 * @throws IllegalStateException
 *             Stream does not go active before the timeout
 */
public static void createAndWaitForStreamToBecomeAvailable(AmazonKinesisClient kinesisClient, String streamName,
        int shardCount) {
    if (streamExists(kinesisClient, streamName)) {
        String state = streamState(kinesisClient, streamName);
        switch (state) {
        case "DELETING":
            long startTime = System.currentTimeMillis();
            long endTime = startTime + 1000 * 120;
            while (System.currentTimeMillis() < endTime && streamExists(kinesisClient, streamName)) {
                try {
                    LOG.info("...Deleting Stream " + streamName + "...");
                    Thread.sleep(1000 * 10);
                } catch (InterruptedException e) {
                }
            }
            if (streamExists(kinesisClient, streamName)) {
                LOG.error("KinesisUtils timed out waiting for stream " + streamName + " to delete");
                throw new IllegalStateException(
                        "KinesisUtils timed out waiting for stream " + streamName + " to delete");
            }
        case "ACTIVE":
            LOG.info("Stream " + streamName + " is ACTIVE");
            return;
        case "CREATING":
            break;
        case "UPDATING":
            LOG.info("Stream " + streamName + " is UPDATING");
            return;
        default:
            throw new IllegalStateException("Illegal stream state: " + state);
        }
    } else {
        CreateStreamRequest createStreamRequest = new CreateStreamRequest();
        createStreamRequest.setStreamName(streamName);
        createStreamRequest.setShardCount(shardCount);
        kinesisClient.createStream(createStreamRequest);
        LOG.info("Stream " + streamName + " created");
    }
    long startTime = System.currentTimeMillis();
    long endTime = startTime + (10 * 60 * 1000);
    while (System.currentTimeMillis() < endTime) {
        try {
            Thread.sleep(1000 * 10);
        } catch (Exception e) {
        }
        try {
            String streamStatus = streamState(kinesisClient, streamName);
            if (streamStatus.equals("ACTIVE")) {
                LOG.info("Stream " + streamName + " is ACTIVE");
                return;
            }
        } catch (ResourceNotFoundException e) {
            throw new IllegalStateException("Stream " + streamName + " never went active");
        }
    }
}