Search in sources :

Example 1 with Record

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

the class KinesisIOReadTest method testRecords.

private List<List<Record>> testRecords(int shards, int events) {
    final Instant now = DateTime.now().toInstant();
    Function<Integer, List<Record>> dataStream = shard -> range(0, events).mapToObj(off -> record(now, shard, off)).collect(toList());
    return range(0, shards).boxed().map(dataStream).collect(toList());
}
Also used : PipelineExecutionException(org.apache.beam.sdk.Pipeline.PipelineExecutionException) IntStream.range(java.util.stream.IntStream.range) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) Shard(software.amazon.awssdk.services.kinesis.model.Shard) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Duration.standardSeconds(org.joda.time.Duration.standardSeconds) BiFunction(java.util.function.BiFunction) CloudWatchClient(software.amazon.awssdk.services.cloudwatch.CloudWatchClient) SdkBytes(software.amazon.awssdk.core.SdkBytes) ArgumentMatcher(org.mockito.ArgumentMatcher) GetRecordsResponse(software.amazon.awssdk.services.kinesis.model.GetRecordsResponse) URI(java.net.URI) LimitExceededException(software.amazon.awssdk.services.kinesis.model.LimitExceededException) DefaultCredentialsProvider(software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider) List(java.util.List) ParDo(org.apache.beam.sdk.transforms.ParDo) Function.identity(java.util.function.Function.identity) Read(org.apache.beam.sdk.io.aws2.kinesis.KinesisIO.Read) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GetShardIteratorRequest(software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest) Mock(org.mockito.Mock) TRIM_HORIZON(software.amazon.kinesis.common.InitialPositionInStream.TRIM_HORIZON) RunWith(org.junit.runner.RunWith) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) Function(java.util.function.Function) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) GetShardIteratorResponse(software.amazon.awssdk.services.kinesis.model.GetShardIteratorResponse) Region(software.amazon.awssdk.regions.Region) MockClientBuilderFactory(org.apache.beam.sdk.io.aws2.MockClientBuilderFactory) IntFunction(java.util.function.IntFunction) Before(org.junit.Before) DoFn(org.apache.beam.sdk.transforms.DoFn) KinesisClient(software.amazon.awssdk.services.kinesis.KinesisClient) PAssert(org.apache.beam.sdk.testing.PAssert) UTF_8(java.nio.charset.StandardCharsets.UTF_8) DateTime(org.joda.time.DateTime) KinesisClientBuilder(software.amazon.awssdk.services.kinesis.KinesisClientBuilder) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) PCollection(org.apache.beam.sdk.values.PCollection) Iterables.concat(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables.concat) GetRecordsRequest(software.amazon.awssdk.services.kinesis.model.GetRecordsRequest) Collectors.toList(java.util.stream.Collectors.toList) Rule(org.junit.Rule) StaticSupplier(org.apache.beam.sdk.io.aws2.StaticSupplier) CloudWatchClientBuilder(software.amazon.awssdk.services.cloudwatch.CloudWatchClientBuilder) Instant(org.joda.time.Instant) ListShardsRequest(software.amazon.awssdk.services.kinesis.model.ListShardsRequest) ListShardsResponse(software.amazon.awssdk.services.kinesis.model.ListShardsResponse) ClientConfiguration(org.apache.beam.sdk.io.aws2.common.ClientConfiguration) Record(software.amazon.awssdk.services.kinesis.model.Record) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Instant(org.joda.time.Instant) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList)

Example 2 with Record

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

the class KinesisIOReadTest method readFromShards.

private void readFromShards(Function<Read, Read> fn, Iterable<Record> expected) {
    Read read = KinesisIO.read().withStreamName("stream").withInitialPositionInStream(TRIM_HORIZON).withArrivalTimeWatermarkPolicy().withMaxNumRecords(SHARDS * SHARD_EVENTS);
    PCollection<Record> result = p.apply(fn.apply(read)).apply(ParDo.of(new ToRecord()));
    PAssert.that(result).containsInAnyOrder(expected);
    p.run();
}
Also used : Read(org.apache.beam.sdk.io.aws2.kinesis.KinesisIO.Read) Record(software.amazon.awssdk.services.kinesis.model.Record)

Example 3 with Record

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

the class SimplifiedKinesisClientTest method generateRecords.

private List<Record> generateRecords(int num) {
    List<Record> records = new ArrayList<>();
    for (int i = 0; i < num; i++) {
        byte[] value = new byte[1024];
        Arrays.fill(value, (byte) i);
        records.add(Record.builder().sequenceNumber(String.valueOf(i)).partitionKey("key").data(SdkBytes.fromByteBuffer(ByteBuffer.wrap(value))).build());
    }
    return records;
}
Also used : ArrayList(java.util.ArrayList) Record(software.amazon.awssdk.services.kinesis.model.Record) Datapoint(software.amazon.awssdk.services.cloudwatch.model.Datapoint)

Example 4 with Record

use of software.amazon.awssdk.services.kinesis.model.Record in project flink by apache.

the class FanOutRecordPublisherTest method testToSdkV1Records.

@Test
public void testToSdkV1Records() throws Exception {
    Date now = new Date();
    byte[] data = new byte[] { 0, 1, 2, 3 };
    Record record = Record.builder().approximateArrivalTimestamp(now.toInstant()).partitionKey("pk").sequenceNumber("sn").data(SdkBytes.fromByteArray(data)).build();
    KinesisProxyV2Interface kinesis = singletonShard(createSubscribeToShardEvent(record));
    RecordPublisher publisher = createRecordPublisher(kinesis, latest());
    TestConsumer consumer = new TestConsumer();
    publisher.run(consumer);
    UserRecord actual = consumer.getRecordBatches().get(0).getDeaggregatedRecords().get(0);
    assertFalse(actual.isAggregated());
    assertEquals(now, actual.getApproximateArrivalTimestamp());
    assertEquals("sn", actual.getSequenceNumber());
    assertEquals("pk", actual.getPartitionKey());
    assertThat(toByteArray(actual.getData()), Matchers.equalTo(data));
}
Also used : RecordPublisher(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher) UserRecord(com.amazonaws.services.kinesis.clientlibrary.types.UserRecord) UserRecord(com.amazonaws.services.kinesis.clientlibrary.types.UserRecord) Record(software.amazon.awssdk.services.kinesis.model.Record) KinesisProxyV2Interface(org.apache.flink.streaming.connectors.kinesis.proxy.KinesisProxyV2Interface) Date(java.util.Date) TestConsumer(org.apache.flink.streaming.connectors.kinesis.testutils.TestUtils.TestConsumer) Test(org.junit.Test)

Example 5 with Record

use of software.amazon.awssdk.services.kinesis.model.Record in project flink by apache.

the class FanOutRecordPublisher method run.

@Override
public RecordPublisherRunResult run(final RecordBatchConsumer recordConsumer) throws InterruptedException {
    LOG.info("Running fan out record publisher on {}::{} from {} - {}", subscribedShard.getStreamName(), subscribedShard.getShard().getShardId(), nextStartingPosition.getShardIteratorType(), nextStartingPosition.getStartingMarker());
    Consumer<SubscribeToShardEvent> eventConsumer = event -> {
        RecordBatch recordBatch = new RecordBatch(toSdkV1Records(event.records()), subscribedShard, event.millisBehindLatest());
        SequenceNumber sequenceNumber = recordConsumer.accept(recordBatch);
        nextStartingPosition = StartingPosition.continueFromSequenceNumber(sequenceNumber);
    };
    RecordPublisherRunResult result = runWithBackoff(eventConsumer);
    LOG.info("Subscription expired {}::{}, with status {}", subscribedShard.getStreamName(), subscribedShard.getShard().getShardId(), result);
    return result;
}
Also used : COMPLETE(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher.RecordPublisherRunResult.COMPLETE) Date(java.util.Date) FullJitterBackoff(org.apache.flink.streaming.connectors.kinesis.proxy.FullJitterBackoff) LoggerFactory(org.slf4j.LoggerFactory) RecordPublisher(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher) StartingPosition(org.apache.flink.streaming.connectors.kinesis.model.StartingPosition) FanOutSubscriberInterruptedException(org.apache.flink.streaming.connectors.kinesis.internals.publisher.fanout.FanOutShardSubscriber.FanOutSubscriberInterruptedException) ArrayList(java.util.ArrayList) FanOutSubscriberException(org.apache.flink.streaming.connectors.kinesis.internals.publisher.fanout.FanOutShardSubscriber.FanOutSubscriberException) SubscribeToShardEvent(software.amazon.awssdk.services.kinesis.model.SubscribeToShardEvent) SequenceNumber(org.apache.flink.streaming.connectors.kinesis.model.SequenceNumber) Nonnull(javax.annotation.Nonnull) StartingPosition.builder(software.amazon.awssdk.services.kinesis.model.StartingPosition.builder) KinesisProxyV2Interface(org.apache.flink.streaming.connectors.kinesis.proxy.KinesisProxyV2Interface) INCOMPLETE(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher.RecordPublisherRunResult.INCOMPLETE) ResourceNotFoundException(software.amazon.awssdk.services.kinesis.model.ResourceNotFoundException) Logger(org.slf4j.Logger) StreamShardHandle(org.apache.flink.streaming.connectors.kinesis.model.StreamShardHandle) RecoverableFanOutSubscriberException(org.apache.flink.streaming.connectors.kinesis.internals.publisher.fanout.FanOutShardSubscriber.RecoverableFanOutSubscriberException) Preconditions(org.apache.flink.util.Preconditions) Consumer(java.util.function.Consumer) RecordBatch(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordBatch) List(java.util.List) EncryptionType(software.amazon.awssdk.services.kinesis.model.EncryptionType) Record(software.amazon.awssdk.services.kinesis.model.Record) CANCELLED(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher.RecordPublisherRunResult.CANCELLED) Internal(org.apache.flink.annotation.Internal) RecordBatch(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordBatch) SequenceNumber(org.apache.flink.streaming.connectors.kinesis.model.SequenceNumber) SubscribeToShardEvent(software.amazon.awssdk.services.kinesis.model.SubscribeToShardEvent)

Aggregations

Record (software.amazon.awssdk.services.kinesis.model.Record)8 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 List (java.util.List)2 Read (org.apache.beam.sdk.io.aws2.kinesis.KinesisIO.Read)2 RecordPublisher (org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher)2 KinesisProxyV2Interface (org.apache.flink.streaming.connectors.kinesis.proxy.KinesisProxyV2Interface)2 Test (org.junit.Test)2 EncryptionType (software.amazon.awssdk.services.kinesis.model.EncryptionType)2 GetRecordsResponse (software.amazon.awssdk.services.kinesis.model.GetRecordsResponse)2 UserRecord (com.amazonaws.services.kinesis.clientlibrary.types.UserRecord)1 URI (java.net.URI)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 BiFunction (java.util.function.BiFunction)1 Consumer (java.util.function.Consumer)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