Exemple #1
0
    public async Task ReceiveArrayWithComplexElements()
    {
        //Arrange
        var httpResponseMessage = await restApiClient.ExecuteStatementAsync(new KSqlDbStatement(@"
Drop type EventCategory;
Drop table Events;
"));

        //Act
        httpResponseMessage = await restApiClient.CreateTypeAsync <EventCategory>();

        httpResponseMessage = await restApiClient.CreateTableAsync <Event>(new EntityCreationMetadata()
        {
            KafkaTopic = "Events", Partitions = 1
        });

        var eventCategory = new EventCategory()
        {
            Name = "xyz"
        };

        var testEvent = new Event
        {
            Id         = 1,
            Places     = new[] { "Place1", "Place2" },
            Categories = new[] { eventCategory, new EventCategory {
                                     Name = "puk"
                                 } }
        };

        var semaphoreSlim = new SemaphoreSlim(0, 1);

        var receivedValues = new List <Event>();
        var subscription   = Context.CreateQueryStream <Event>().Take(1)
                             .Subscribe(value =>
        {
            receivedValues.Add(value);
        }, error =>
        {
            semaphoreSlim.Release();
        },
                                        () =>
        {
            semaphoreSlim.Release();
        });

        httpResponseMessage = await restApiClient.InsertIntoAsync(testEvent);

        string responseContent = await httpResponseMessage.Content.ReadAsStringAsync();

        //Assert
        await semaphoreSlim.WaitAsync(TimeSpan.FromSeconds(5));

        receivedValues.Count.Should().Be(1);
        receivedValues[0].Places.Length.Should().Be(2);
        receivedValues[0].Categories.ToArray()[0].Name.Should().Be("xyz");
        receivedValues[0].Categories.ToArray()[1].Name.Should().Be("puk");

        using (subscription) { }
    }
    public async Task SubscribeAsync()
    {
        var entityCreationMetadata = new EntityCreationMetadata
        {
            KafkaTopic = nameof(Order) + "-Join",
            Partitions = 1
        };

        var response = await restApiClient.CreateStreamAsync <Order>(entityCreationMetadata, ifNotExists : true);

        var r = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

        response = await restApiClient.CreateTableAsync <Payment>(entityCreationMetadata with {
            KafkaTopic = nameof(Payment) + "-Join"
        }, ifNotExists : true);

        response = await restApiClient.CreateTableAsync <Shipment>(entityCreationMetadata with {
            KafkaTopic = nameof(Shipment) + "-Join"
        }, ifNotExists : true);

        var value = new Foo {
            Prop = 42
        };

        var query = (from o in context.CreateQueryStream <Order>()
                     join p1 in Source.Of <Payment>() on o.PaymentId equals p1.Id
                     join s1 in Source.Of <Shipment>() on o.ShipmentId equals s1.Id into gj
                     from sa in gj.DefaultIfEmpty()
                     select new
        {
            value,
            orderId = o.OrderId,
            shipmentId = sa.Id,
            paymentId = p1.Id,
        })
                    .Take(5);

        string ksql = query.ToQueryString();

        using var subscription = query
                                 .Subscribe(c =>
        {
            Console.WriteLine($"{nameof(Order.OrderId)}: {c.orderId}");

            Console.WriteLine($"{nameof(Order.PaymentId)}: {c.paymentId}");

            if (c.shipmentId.HasValue)
            {
                Console.WriteLine($"{nameof(Order.ShipmentId)}: {c.shipmentId}");
            }
        }, error =>
        {
            Console.WriteLine(error.Message);
        });

        var order = new Order {
            OrderId = 1, PaymentId = 1, ShipmentId = 1
        };
        var payment = new Payment {
            Id = 1
        };
        var shipment = new Shipment {
            Id = 1
        };

        response = await restApiClient.InsertIntoAsync(order);

        r = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

        response = await restApiClient.InsertIntoAsync(payment);

        await Task.Delay(TimeSpan.FromMilliseconds(250));

        response = await restApiClient.InsertIntoAsync(shipment);
    }