software.amazon.awssdk.services.kinesis.model.DescribeStreamRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.kinesis.model.DescribeStreamRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
protected static void createKinesisStream() {
    kinesis = KinesisClient.builder().credentialsProvider(CREDENTIALS_PROVIDER_CHAIN).region(Region.US_WEST_2).build();

    kinesis.createStream(CreateStreamRequest.builder().streamName(KINESIS_STREAM_NAME).shardCount(1).build());

    StreamDescription description = kinesis.describeStream(DescribeStreamRequest.builder().streamName(KINESIS_STREAM_NAME).build())
            .streamDescription();
    streamArn = description.streamARN();

    // Wait till stream is active (less than a minute)
    Instant start = Instant.now();
    while (StreamStatus.ACTIVE != description.streamStatus()) {
        if (Duration.between(start, Instant.now()).toMillis() > MAX_WAIT_TIME.toMillis()) {
            throw new RuntimeException("Timed out waiting for stream to become active");
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ignored) {
            // Ignored or expected.
        }

        description = kinesis.describeStream(DescribeStreamRequest.builder().streamName(KINESIS_STREAM_NAME).build())
                .streamDescription();
    }
}
 
Example #2
Source File: StockTradesWriter.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void validateStream(KinesisClient kinesisClient, String streamName) {
    try {
        DescribeStreamRequest describeStreamRequest = DescribeStreamRequest.builder()
                .streamName(streamName)
                .build();

        DescribeStreamResponse describeStreamResponse = kinesisClient.describeStream(describeStreamRequest);

        if(!describeStreamResponse.streamDescription().streamStatus().toString().equals("ACTIVE")) {
            System.err.println("Stream " + streamName + " is not active. Please wait a few moments and try again.");
            System.exit(1);
        }
    }catch (KinesisException e) {
        System.err.println("Error found while describing the stream " + streamName);
        System.err.println(e);
        System.exit(1);
    }
    // snippet-end:[kinesis.java2.putrecord.main]
}
 
Example #3
Source File: AwsKinesisScanner.java    From clouditor with Apache License 2.0 5 votes vote down vote up
@Override
protected List<StreamDescription> list() {
  return this.api.listStreams().streamNames().stream()
      .map(
          streamName ->
              this.api
                  .describeStream(DescribeStreamRequest.builder().streamName(streamName).build())
                  .streamDescription())
      .collect(Collectors.toList());
}
 
Example #4
Source File: AwsKinesisScannerTest.java    From clouditor with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void setUpOnce() throws IOException {
  discoverAssets(
      KinesisClient.class,
      AwsKinesisScanner::new,
      api -> {
        when(api.listStreams())
            .thenReturn(
                ListStreamsResponse.builder()
                    .streamNames("stream-encrypted", "stream-not-encrypted")
                    .build());

        when(api.describeStream(
                DescribeStreamRequest.builder().streamName("stream-encrypted").build()))
            .thenReturn(
                DescribeStreamResponse.builder()
                    .streamDescription(
                        StreamDescription.builder()
                            .streamARN("arn:aws:kinesis:us-east-1:111122223333:encrypted")
                            .encryptionType(EncryptionType.KMS)
                            .build())
                    .build());

        when(api.describeStream(
                DescribeStreamRequest.builder().streamName("stream-not-encrypted").build()))
            .thenReturn(
                DescribeStreamResponse.builder()
                    .streamDescription(
                        StreamDescription.builder()
                            .streamARN("arn:aws:kinesis:us-east-1:111122223333:unencrypted")
                            .encryptionType(EncryptionType.NONE)
                            .build())
                    .build());
      });
}
 
Example #5
Source File: KinesisIntegrationTests.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescribeBogusStream() {
    try {
        client.describeStream(DescribeStreamRequest.builder().streamName("bogus-stream-name").build());
        Assert.fail("Expected ResourceNotFoundException");
    } catch (ResourceNotFoundException exception) {
        // Ignored or expected.
    }
}
 
Example #6
Source File: KinesisIntegrationTests.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private List<Shard> waitForStream(final String streamName)
        throws InterruptedException {

    while (true) {
        DescribeStreamResponse result = client.describeStream(DescribeStreamRequest.builder().streamName(streamName).build());
        Assert.assertNotNull(result);

        StreamDescription description = result.streamDescription();
        Assert.assertNotNull(description);

        Assert.assertEquals(streamName, description.streamName());
        Assert.assertNotNull(description.streamARN());
        Assert.assertFalse(description.hasMoreShards());

        StreamStatus status = description.streamStatus();
        Assert.assertNotNull(status);

        if (status == StreamStatus.ACTIVE) {
            List<Shard> shards = description.shards();
            validateShards(shards);

            return shards;
        }

        if (!(status == StreamStatus.CREATING
              || status == StreamStatus.UPDATING)) {

            Assert.fail("Unexpected status '" + status + "'");
        }

        Thread.sleep(1000);
    }
}
 
Example #7
Source File: KinesisStreamInfoProvider.java    From synapse with Apache License 2.0 5 votes vote down vote up
/**
 * Returns stream information for the given Kinesis stream.
 *
 * @param channelName the name of the stream
 * @return KinesisStreamInfo
 * @throws IllegalArgumentException if the stream does not exist
 */
public KinesisStreamInfo getStreamInfo(final String channelName) {

    try {

        final DescribeStreamRequest request = DescribeStreamRequest.builder().streamName(channelName).build();

        DescribeStreamResponse response = kinesisAsyncClient.describeStream(request).join();

        final KinesisStreamInfo.Builder streamInfoBuilder = builder()
                .withChannelName(channelName)
                .withArn(response.streamDescription().streamARN());

        String lastSeenShardId = addShardInfoFromResponse(response, streamInfoBuilder);

        while (response.streamDescription().hasMoreShards()) {
            response = kinesisAsyncClient.describeStream(DescribeStreamRequest.builder()
                    .streamName(channelName)
                    .exclusiveStartShardId(lastSeenShardId)
                    .build())
                    .join();
            lastSeenShardId = addShardInfoFromResponse(response, streamInfoBuilder);
        }

        return streamInfoBuilder.build();
    } catch (final ResourceNotFoundException e) {
        throw new IllegalArgumentException(format("Kinesis channel %s does not exist: %s", channelName, e.getMessage()));
    }
}
 
Example #8
Source File: GetRecords.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void getStockTrades(KinesisClient kinesisClient, String streamName) {

            String shardIterator;
            String lastShardId = null;

            // Retrieve the shards from a data stream
            DescribeStreamRequest describeStreamRequest = DescribeStreamRequest.builder()
                    .streamName(streamName)
                    .build();
            List<Shard> shards = new ArrayList<>();

            DescribeStreamResponse streamRes;
            do {
                streamRes = kinesisClient.describeStream(describeStreamRequest);
                shards.addAll(streamRes.streamDescription().shards());

                if (shards.size() > 0) {
                    lastShardId = shards.get(shards.size() - 1).shardId();
                }
            } while (streamRes.streamDescription().hasMoreShards());

            GetShardIteratorRequest itReq = GetShardIteratorRequest.builder()
                    .streamName(streamName)
                    .shardIteratorType("TRIM_HORIZON")
                    .shardId(shards.get(0).shardId())
                    .build();

            GetShardIteratorResponse shardIteratorResult = kinesisClient.getShardIterator(itReq);
            shardIterator = shardIteratorResult.shardIterator();

            // Continuously read data records from a shard
            List<Record> records;

            // Create a GetRecordsRequest with the existing shardIterator,
            // and set maximum records to return to 1000
            GetRecordsRequest recordsRequest = GetRecordsRequest.builder()
                     .shardIterator(shardIterator)
                     .limit(1000)
                     .build();

           GetRecordsResponse result = kinesisClient.getRecords(recordsRequest);

           // Put result into a record list, result might be empty
           records = result.records();

            // Print records
            for (Record record : records) {
                SdkBytes byteBuffer = record.data();
                System.out.println(String.format("Seq No: %s - %s", record.sequenceNumber(),
                 new String(byteBuffer.asByteArray())));
             }

             }