public async Task <IEnumerable <T> > ExecuteQuery <T>(KsqlQuery query)
        {
            await using var queryStream = await _ksqlRestClient.ExecuteQueryAsync(query);

            using var streamReader = new StreamReader(queryStream);
            var results = new List <T>();

            while (!streamReader.EndOfStream)
            {
                var line = await streamReader.ReadLineAsync();

                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                var streamResponse = JsonConvert.DeserializeObject <StreamResponse>(line);

                if (streamResponse.Row?.Columns is null || !streamResponse.Row.Columns.Any())
                {
                    break;
                }

                var values  = streamResponse.Row.Columns.Skip(2).SelectMany(FlattenNestedValues).ToArray();
                var payload = _mapper.Map <T>(values);
                results.Add(payload);
            }

            return(results);
        }
Пример #2
0
        public async Task <Stream> ExecuteQueryAsync(KsqlQuery query, CancellationToken token = default)
        {
            var request  = JsonConvert.SerializeObject(query);
            var response = await _client.SendAsync(new HttpRequestMessage(HttpMethod.Post, "")
            {
                Content = new StringContent(request, Encoding.UTF8, "application/vnd.ksql.v1+json"),
                Headers = { { "accept", MediaTypeWithQualityHeaderValue.Parse("application/vnd.ksql.v1+json").ToString() } }
            },
                                                   HttpCompletionOption.ResponseHeadersRead,
                                                   token);

            return(await response.Content.ReadAsStreamAsync());
        }
Пример #3
0
 public static IServiceCollection AddKsqlConsumer <TRow>(this IServiceCollection services, KsqlQuery query) =>
 services.AddTransient(provider =>
                       new KafkaKsqlConsumer <TRow>(
                           provider.GetService <KsqlClient>(),
                           query,
                           provider.GetService <TableMapper>()
                           )
                       );
 public KafkaKsqlConsumer(KsqlClient ksqlRestClient, KsqlQuery query, TableMapper mapper)
 {
     _ksqlRestClient = ksqlRestClient;
     _query          = query;
     _mapper         = mapper;
 }