Search in sources :

Example 6 with Shard

use of software.amazon.awssdk.services.kinesis.model.Shard in project beam by apache.

the class SimplifiedKinesisClientTest method shouldListAllShardsForExclusiveStartShardId.

@Test
public void shouldListAllShardsForExclusiveStartShardId() throws Exception {
    Shard shard1 = Shard.builder().shardId(SHARD_1).build();
    Shard shard2 = Shard.builder().shardId(SHARD_2).build();
    Shard shard3 = Shard.builder().shardId(SHARD_3).build();
    String exclusiveStartShardId = "exclusiveStartShardId";
    when(kinesis.listShards(ListShardsRequest.builder().streamName(STREAM).maxResults(1_000).shardFilter(ShardFilter.builder().type(ShardFilterType.AFTER_SHARD_ID).shardId(exclusiveStartShardId).build()).build())).thenReturn(ListShardsResponse.builder().shards(shard1, shard2, shard3).nextToken(null).build());
    List<Shard> shards = underTest.listShardsFollowingClosedShard(STREAM, exclusiveStartShardId);
    assertThat(shards).containsOnly(shard1, shard2, shard3);
}
Also used : Shard(software.amazon.awssdk.services.kinesis.model.Shard) Test(org.junit.Test)

Example 7 with Shard

use of software.amazon.awssdk.services.kinesis.model.Shard in project beam by apache.

the class SimplifiedKinesisClientTest method shouldListAllShardsForTimestampBeforeStreamCreationTimestamp.

@Test
public void shouldListAllShardsForTimestampBeforeStreamCreationTimestamp() throws Exception {
    Shard shard1 = Shard.builder().shardId(SHARD_1).build();
    Shard shard2 = Shard.builder().shardId(SHARD_2).build();
    Shard shard3 = Shard.builder().shardId(SHARD_3).build();
    Instant startingPointTimestamp = Instant.parse("2000-01-01T15:00:00.000Z");
    Instant streamCreationTimestamp = startingPointTimestamp.plus(Duration.standardHours(1));
    DescribeStreamSummaryRequest describeStreamRequest = DescribeStreamSummaryRequest.builder().streamName(STREAM).build();
    when(kinesis.describeStreamSummary(describeStreamRequest)).thenReturn(DescribeStreamSummaryResponse.builder().streamDescriptionSummary(StreamDescriptionSummary.builder().streamCreationTimestamp(TimeUtil.toJava(streamCreationTimestamp)).build()).build());
    ShardFilter shardFilter = ShardFilter.builder().type(ShardFilterType.AT_TRIM_HORIZON).build();
    when(kinesis.listShards(ListShardsRequest.builder().streamName(STREAM).shardFilter(shardFilter).maxResults(1_000).build())).thenReturn(ListShardsResponse.builder().shards(shard1, shard2, shard3).nextToken(null).build());
    List<Shard> shards = underTest.listShardsAtPoint(STREAM, new StartingPoint(startingPointTimestamp));
    assertThat(shards).containsOnly(shard1, shard2, shard3);
}
Also used : Instant(org.joda.time.Instant) DescribeStreamSummaryRequest(software.amazon.awssdk.services.kinesis.model.DescribeStreamSummaryRequest) ShardFilter(software.amazon.awssdk.services.kinesis.model.ShardFilter) Shard(software.amazon.awssdk.services.kinesis.model.Shard) Test(org.junit.Test)

Example 8 with Shard

use of software.amazon.awssdk.services.kinesis.model.Shard in project aws-doc-sdk-examples by awsdocs.

the class GetRecords method getStockTrades.

// snippet-start:[kinesis.java2.getrecord.main]
public static void getStockTrades(KinesisClient kinesisClient, String streamName) {
    String shardIterator;
    String lastShardId = null;
    // Retrieve the Shards from a 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(lastShardId).build();
    GetShardIteratorResponse shardIteratorResult = kinesisClient.getShardIterator(itReq);
    shardIterator = shardIteratorResult.shardIterator();
    // Continuously read data records from shard.
    List<Record> records;
    // Create new GetRecordsRequest with existing shardIterator.
    // Set maximum records to return to 1000.
    GetRecordsRequest recordsRequest = GetRecordsRequest.builder().shardIterator(shardIterator).limit(1000).build();
    GetRecordsResponse result = kinesisClient.getRecords(recordsRequest);
    // Put result into record list. Result may 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())));
    }
}
Also used : ArrayList(java.util.ArrayList) GetShardIteratorRequest(software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest) GetRecordsResponse(software.amazon.awssdk.services.kinesis.model.GetRecordsResponse) DescribeStreamResponse(software.amazon.awssdk.services.kinesis.model.DescribeStreamResponse) DescribeStreamRequest(software.amazon.awssdk.services.kinesis.model.DescribeStreamRequest) GetRecordsRequest(software.amazon.awssdk.services.kinesis.model.GetRecordsRequest) SdkBytes(software.amazon.awssdk.core.SdkBytes) GetShardIteratorResponse(software.amazon.awssdk.services.kinesis.model.GetShardIteratorResponse) Record(software.amazon.awssdk.services.kinesis.model.Record) Shard(software.amazon.awssdk.services.kinesis.model.Shard)

Example 9 with Shard

use of software.amazon.awssdk.services.kinesis.model.Shard in project beam by apache.

the class SimplifiedKinesisClientTest method shouldListAllShardsForTimestampWithinStreamRetentionAfterStreamCreationTimestamp.

@Test
public void shouldListAllShardsForTimestampWithinStreamRetentionAfterStreamCreationTimestamp() throws Exception {
    Shard shard1 = Shard.builder().shardId(SHARD_1).build();
    Shard shard2 = Shard.builder().shardId(SHARD_2).build();
    Shard shard3 = Shard.builder().shardId(SHARD_3).build();
    int hoursDifference = 1;
    int retentionPeriodHours = hoursDifference * 3;
    Instant streamCreationTimestamp = CURRENT_TIMESTAMP.minus(Duration.standardHours(retentionPeriodHours));
    Instant startingPointTimestamp = streamCreationTimestamp.plus(Duration.standardHours(hoursDifference));
    when(currentInstantSupplier.get()).thenReturn(CURRENT_TIMESTAMP);
    when(kinesis.describeStreamSummary(DescribeStreamSummaryRequest.builder().streamName(STREAM).build())).thenReturn(DescribeStreamSummaryResponse.builder().streamDescriptionSummary(StreamDescriptionSummary.builder().retentionPeriodHours(retentionPeriodHours).streamCreationTimestamp(TimeUtil.toJava(streamCreationTimestamp)).build()).build());
    ShardFilter shardFilter = ShardFilter.builder().type(ShardFilterType.AT_TIMESTAMP).timestamp(TimeUtil.toJava(startingPointTimestamp)).build();
    when(kinesis.listShards(ListShardsRequest.builder().streamName(STREAM).shardFilter(shardFilter).maxResults(1_000).build())).thenReturn(ListShardsResponse.builder().shards(shard1, shard2, shard3).nextToken(null).build());
    List<Shard> shards = underTest.listShardsAtPoint(STREAM, new StartingPoint(startingPointTimestamp));
    assertThat(shards).containsOnly(shard1, shard2, shard3);
}
Also used : Instant(org.joda.time.Instant) ShardFilter(software.amazon.awssdk.services.kinesis.model.ShardFilter) Shard(software.amazon.awssdk.services.kinesis.model.Shard) Datapoint(software.amazon.awssdk.services.cloudwatch.model.Datapoint) Test(org.junit.Test)

Example 10 with Shard

use of software.amazon.awssdk.services.kinesis.model.Shard in project beam by apache.

the class SimplifiedKinesisClientTest method shouldListAllShardsForTrimHorizon.

@Test
public void shouldListAllShardsForTrimHorizon() throws Exception {
    Shard shard1 = Shard.builder().shardId(SHARD_1).build();
    Shard shard2 = Shard.builder().shardId(SHARD_2).build();
    Shard shard3 = Shard.builder().shardId(SHARD_3).build();
    ShardFilter shardFilter = ShardFilter.builder().type(ShardFilterType.AT_TRIM_HORIZON).build();
    when(kinesis.listShards(ListShardsRequest.builder().streamName(STREAM).shardFilter(shardFilter).maxResults(1_000).build())).thenReturn(ListShardsResponse.builder().shards(shard1, shard2, shard3).nextToken(null).build());
    List<Shard> shards = underTest.listShardsAtPoint(STREAM, new StartingPoint(InitialPositionInStream.TRIM_HORIZON));
    assertThat(shards).containsOnly(shard1, shard2, shard3);
}
Also used : ShardFilter(software.amazon.awssdk.services.kinesis.model.ShardFilter) Shard(software.amazon.awssdk.services.kinesis.model.Shard) Test(org.junit.Test)

Aggregations

Shard (software.amazon.awssdk.services.kinesis.model.Shard)12 Test (org.junit.Test)9 ShardFilter (software.amazon.awssdk.services.kinesis.model.ShardFilter)6 Instant (org.joda.time.Instant)5 ArrayList (java.util.ArrayList)2 ImmutableList (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList)2 Datapoint (software.amazon.awssdk.services.cloudwatch.model.Datapoint)2 DescribeStreamSummaryRequest (software.amazon.awssdk.services.kinesis.model.DescribeStreamSummaryRequest)2 ListShardsRequest (software.amazon.awssdk.services.kinesis.model.ListShardsRequest)2 ListShardsResponse (software.amazon.awssdk.services.kinesis.model.ListShardsResponse)2 URI (java.net.URI)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 List (java.util.List)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 Function.identity (java.util.function.Function.identity)1 IntFunction (java.util.function.IntFunction)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream.range (java.util.stream.IntStream.range)1 PipelineExecutionException (org.apache.beam.sdk.Pipeline.PipelineExecutionException)1