ksqlDb.RestApi.Client 6.5.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package ksqlDb.RestApi.Client --version 6.5.2                
NuGet\Install-Package ksqlDb.RestApi.Client -Version 6.5.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ksqlDb.RestApi.Client" Version="6.5.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ksqlDb.RestApi.Client --version 6.5.2                
#r "nuget: ksqlDb.RestApi.Client, 6.5.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ksqlDb.RestApi.Client as a Cake Addin
#addin nuget:?package=ksqlDb.RestApi.Client&version=6.5.2

// Install ksqlDb.RestApi.Client as a Cake Tool
#tool nuget:?package=ksqlDb.RestApi.Client&version=6.5.2                

This package enables seamless integration of KSQL push and pull queries with LINQ queries in your .NET C# applications. It allows you to perform server-side operations such as filtering, projection, limiting, and more directly on push notifications using ksqlDB push queries. This facilitates continuous processing of computations over unbounded, potentially never-ending, streams of data.

In addition, the package supports executing SQL statements via the REST API. You can use it to insert records into streams, create tables and types, and perform administrative tasks such as listing available streams.

ksqlDB.RestApi.Client is a contribution to Confluent ksqldb-clients

main

Install with NuGet package manager:

Install-Package ksqlDb.RestApi.Client

or with .NET CLI

dotnet add package ksqlDb.RestApi.Client

This adds a <PackageReference> to your csproj file, similar to the following:

<PackageReference Include="ksqlDb.RestApi.Client" Version="6.0.0" />

Alternative option is to use Protobuf content type:

dotnet add package ksqlDb.RestApi.Client.ProtoBuf

Feel free to experiment with the following example in a .NET interactive Notebook:

using ksqlDB.RestApi.Client.KSql.Linq;
using ksqlDB.RestApi.Client.KSql.Query;
using ksqlDB.RestApi.Client.KSql.Query.Context;
using ksqlDB.RestApi.Client.KSql.Query.Options;

var ksqlDbUrl = @"http://localhost:8088";

var contextOptions = new KSqlDBContextOptions(ksqlDbUrl)
{
  ShouldPluralizeFromItemName = true
};

await using var context = new KSqlDBContext(contextOptions);

using var subscription = context.CreatePushQuery<Tweet>()
  .WithOffsetResetPolicy(AutoOffsetReset.Latest)
  .Where(p => p.Message != "Hello world" || p.Id == 1)
  .Select(l => new { l.Message, l.Id })
  .Take(2)
  .Subscribe(tweetMessage =>
  {
    Console.WriteLine($"{nameof(Tweet)}: {tweetMessage.Id} - {tweetMessage.Message}");
  }, error => { Console.WriteLine($"Exception: {error.Message}"); }, () => Console.WriteLine("Completed"));

Console.WriteLine("Press any key to stop the subscription");

Console.ReadKey();
public class Tweet : Record
{
  public int Id { get; set; }

  public string Message { get; set; }
}

An entity class in ksqlDB.RestApi.Client represents the structure of a table or stream. An instance of the class represents a record in that stream or table while properties are mapped to columns respectively.

LINQ code written in C# from the sample is equivalent to this KSQL query:

SELECT Message, Id
  FROM Tweets
 WHERE Message != 'Hello world' OR Id = 1
  EMIT CHANGES
 LIMIT 2;

In the provided C# code snippet, most of the code executes on the server side except for the IQbservable<TEntity>.Subscribe extension method. This method is responsible for subscribing to your ksqlDB stream, which is created using the following approach:

using ksqlDB.RestApi.Client.KSql.RestApi.Http;
using ksqlDB.RestApi.Client.KSql.RestApi.Statements;
using ksqlDB.RestApi.Client.KSql.RestApi;
using ksqlDB.Api.Client.Samples.Models;

EntityCreationMetadata metadata = new(kafkaTopic: nameof(Tweet))
{
  Partitions = 3,
  Replicas = 3
};

var httpClient = new HttpClient()
{
  BaseAddress = new Uri(@"http://localhost:8088")
};

var httpClientFactory = new HttpClientFactory(httpClient);
var restApiClient = new KSqlDbRestApiClient(httpClientFactory);

var httpResponseMessage = await restApiClient.CreateOrReplaceStreamAsync<Tweet>(metadata);

CreateOrReplaceStreamAsync executes the following statement:

CREATE OR REPLACE STREAM Tweets (
  Id INT,
  Message VARCHAR
) WITH ( KAFKA_TOPIC='Tweet', VALUE_FORMAT='Json', PARTITIONS='3', REPLICAS='3' );

Execute the following insert statements to publish messages using your ksqldb-cli

docker exec -it $(docker ps -q -f name=ksqldb-cli) ksql http://ksqldb-server:8088
INSERT INTO tweets (id, message) VALUES (1, 'Hello world');
INSERT INTO tweets (id, message) VALUES (2, 'ksqlDB rulez!');

or insert a record from C#:

var responseMessage = await new KSqlDbRestApiClient(httpClientFactory)
  .InsertIntoAsync(new Tweet { Id = 2, Message = "ksqlDB rulez!" });

or with KSqlDbContext:

await using var context = new KSqlDBContext(ksqlDbUrl);

context.Add(new Tweet { Id = 1, Message = "Hello world" });
context.Add(new Tweet { Id = 2, Message = "ksqlDB rulez!" });

var saveChangesResponse = await context.SaveChangesAsync();

Sample projects can be found under Samples solution folder in ksqlDB.RestApi.Client.sln

External dependencies:

Clone the repository

git clone https://github.com/tomasfabian/ksqlDB.RestApi.Client-DotNet.git

CD to Samples

CD Samples\ksqlDB.RestApi.Client.Sample\

run in command line:

docker compose up -d

AspNet Blazor server side sample:

In Blazor, the application logic and UI rendering occur on the server. The client's web browser receives updates and UI changes through a SignalR connection. This ensures smooth integration with the ksqlDB.RestApi.Client library, allowing the Apache Kafka broker and ksqlDB to remain hidden from direct exposure to clients. The server-side Blazor application communicates with ksqlDB using the ksqlDB.RestApi.Client. Whenever an event in ksqlDB occurs, the server-side Blazor app responds and signals the UI in the client's browser to update. This setup allows a smooth and continuous update flow, creating a real-time, reactive user interface.

  • set docker-compose.csproj as startup project in InsideOut.sln for embedded Kafka connect integration and stream processing examples.

IQbservable<T> extension methods

As depicted below IObservable<T> is the dual of IEnumerable<T> and IQbservable<T> is the dual of IQueryable<T>. In all four cases LINQ providers are using deferred execution. While the first two are executed locally the latter two are executed server side. The server side execution is possible thanks to traversing ASTs (Abstract Syntax Trees) with visitors. The KSqlDbProvider will create the KSQL syntax for you from expression trees and pass it along to ksqlDB.

Both IObservable<T> and IQbservable<T> represent push-based sequences of asynchronous and potentially infinite events, while IEnumerable<T> and IQueryable<T> represent collections or pull-based sequences of items that can be iterated or queried, respectively.

<img src="https://www.codeproject.com/KB/cs/646361/WhatHowWhere.jpg" />

List of supported push query extension methods:

Register the KSqlDbContext

IKSqlDBContext and IKSqlDbRestApiClient can be provided with dependency injection. These services can be registered during app startup and components that require these services, are provided with these services via constructor parameters.

To register KSqlDbContext as a service, open Program.cs, and add the lines to the ConfigureServices method shown below or see some more details in the workshop:

using ksqlDB.RestApi.Client.Sensors;
using ksqlDB.RestApi.Client.KSql.Query.Options;
using ksqlDb.RestApi.Client.DependencyInjection;
using ksqlDB.RestApi.Client.Sensors.KSqlDb;

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
      var ksqlDbUrl = @"http://localhost:8088";

      services.AddDbContext<ISensorsKSqlDbContext, SensorsKSqlDbContext>(
        options =>
        {
          var setupParameters = options.UseKSqlDb(ksqlDbUrl);

          setupParameters.Options.ShouldPluralizeFromItemName = false;
          setupParameters.SetAutoOffsetReset(AutoOffsetReset.Earliest);

        }, ServiceLifetime.Transient, restApiLifetime: ServiceLifetime.Transient);
    })
    .Build();

await host.RunAsync();

KSqlDbContextOptions builder

To modify parameters or introduce new ones, utilize the following approach:

var contextOptions = new KSqlDbContextOptionsBuilder()
  .UseKSqlDb("http://localhost:8088)
  .SetBasicAuthCredentials("fred", "flinstone")
  .SetJsonSerializerOptions(jsonOptions =>
  {
    jsonOptions.IgnoreReadOnlyFields = true;
  })
  .SetAutoOffsetReset(AutoOffsetReset.Latest)
  .SetProcessingGuarantee(ProcessingGuarantee.ExactlyOnce)
  .SetIdentifierEscaping(IdentifierEscaping.Keywords)
  .SetupPushQuery(options =>
  {
    options.Properties["ksql.query.push.v2.enabled"] = "true";
  })
  .Options;

This code initializes a KSqlDbContextOptionsBuilder to configure settings for a ksqlDB context. Here's a breakdown of the configurations:

  • UseKSqlDb("http://localhost:8088"): Specifies the URL of the ksqlDB server.
  • SetBasicAuthCredentials("fred", "flinstone"): Sets the basic authentication credentials (username and password) for accessing the ksqlDB server.
  • SetJsonSerializerOptions(jsonOptions => { ... }): Configures JSON serialization options, such as ignoring read-only fields.
  • SetAutoOffsetReset(AutoOffsetReset.Latest): Sets the offset reset behavior to start consuming messages from the latest available when no committed offset is found. By default, 'auto.offset.reset' is configured to 'earliest'.
  • SetProcessingGuarantee(ProcessingGuarantee.ExactlyOnce): Specifies the processing guarantee as exactly-once semantics.
  • SetIdentifierEscaping(IdentifierEscaping.Keywords): Escapes identifiers such as table and column names that are SQL keywords.
  • SetupPushQuery(options => { ... }): Configures push query options, specifically enabling KSQL query push version 2.

Finally, .Options returns the configured options for the ksqlDB context.

Overriding stream names

Stream names are generated based on the generic record types. They are pluralized with Pluralize.NET package.

By default the generated from item names such as stream and table names are pluralized. This behavior could be switched off with the following ShouldPluralizeStreamName configuration.

context.CreatePushQuery<Person>();
FROM People

This can be disabled:

var contextOptions = new KSqlDBContextOptions(@"http://localhost:8088")
{
  ShouldPluralizeFromItemName = false
};

new KSqlDBContext(contextOptions).CreatePushQuery<Person>();
FROM Person

Setting an arbitrary stream name (from_item name):

context.CreatePushQuery<Tweet>("custom_topic_name");
FROM custom_topic_name

KSqlDbRestApiClient

The KSqlDbRestApiClient supports various operations such as executing KSQL statements, inserting data into streams asynchronously, creating, listing or dropping entities, and managing KSQL connectors.

using ksqlDB.RestApi.Client.KSql.RestApi;
using ksqlDB.RestApi.Client.KSql.RestApi.Enums;
using ksqlDB.RestApi.Client.KSql.RestApi.Extensions;
using ksqlDB.RestApi.Client.KSql.RestApi.Http;
using ksqlDB.RestApi.Client.KSql.RestApi.Serialization;
using ksqlDB.RestApi.Client.KSql.RestApi.Statements;
using ksqlDB.RestApi.Client.KSql.RestApi.Statements.Properties;
using ksqlDB.RestApi.Client.Samples.Models.Movies;

public static async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
  var httpClient = new HttpClient()
  {
    BaseAddress = new Uri("http://localhost:8088")
  };
  var httpClientFactory = new HttpClientFactory(httpClient);
  var kSqlDbRestApiClient = new KSqlDbRestApiClient(httpClientFactory);

  EntityCreationMetadata entityCreationMetadata = new(kafkaTopic: "companyname.movies")
  {
    Partitions = 3,
    Replicas = 3,
    ValueFormat = SerializationFormats.Json,
    IdentifierEscaping = IdentifierEscaping.Keywords
  };

  var httpResponseMessage = await kSqlDbRestApiClient.CreateOrReplaceTableAsync<Movie>(entityCreationMetadata, cancellationToken);
  var responses = await httpResponseMessage.ToStatementResponsesAsync();
  Console.WriteLine($"Create or replace table response: {responses[0].CommandStatus!.Message}");

  Console.WriteLine($"{Environment.NewLine}Available tables:");
  var tablesResponses = await kSqlDbRestApiClient.GetTablesAsync(cancellationToken);
  Console.WriteLine(string.Join(', ', tablesResponses[0].Tables!.Select(c => c.Name)));

  var dropProperties = new DropFromItemProperties
  {
    UseIfExistsClause = true,
    DeleteTopic = true,
    IdentifierEscaping = IdentifierEscaping.Keywords
  };
  httpResponseMessage = await kSqlDbRestApiClient.DropTableAsync<Movie>(dropProperties, cancellationToken: cancellationToken);
  tablesResponses = await kSqlDbRestApiClient.GetTablesAsync(cancellationToken);
}
using ksqlDB.RestApi.Client.KSql.Query;
using ksqlDB.RestApi.Client.KSql.RestApi.Statements.Annotations;

public class Movie : Record
{
  [Key]
  public int Id { get; set; }
  public string Title { get; set; } = null!;
}

Model builder

By leveraging the ksqlDb.RestApi.Client fluent API model builder, you can streamline the configuration process, improve code readability, and mitigate issues related to code regeneration by keeping configuration logic separate from generated POCOs.

using ksqlDb.RestApi.Client.FluentAPI.Builders;
using ksqlDb.RestApi.Client.FluentAPI.Builders.Configuration;

ModelBuilder modelBuilder = new();

var decimalTypeConvention = new DecimalTypeConvention(14, 14);

modelBuilder.AddConvention(decimalTypeConvention);

modelBuilder.Entity<Payment>()
  .Property(b => b.Amount)
  .Decimal(precision: 10, scale: 2);

modelBuilder.Entity<Payment>()
  .Property(b => b.Description)
  .HasColumnName("Desc");

modelBuilder.Entity<Account>()
  .HasKey(c => c.Id);

modelBuilder.Entity<Account>()
  .Property(b => b.Secret)
  .Ignore();

C# entity definitions:

record Payment
{
  public string Id { get; set; } = null!;
  public decimal Amount { get; set; }
  public string Description { get; set; } = null!;
}

record Account
{
  public string Id { get; set; } = null!;
  public decimal Balance { get; set; }
  public string Secret { get; set; }
}

Usage with ksqlDB REST API Client:

var kSqlDbRestApiClient = new KSqlDbRestApiClient(httpClientFactory, modelBuilder);
await kSqlDbRestApiClient.CreateTypeAsync<Payment>(cancellationToken);

var entityCreationMetadata = new EntityCreationMetadata(kafkaTopic: nameof(Account), partitions: 3)
{
  Replicas = 3
};
responseMessage = await restApiProvider.CreateTableAsync<Account>(entityCreationMetadata, true, cancellationToken);

Generated KSQL:

CREATE TYPE Payment AS STRUCT<Id VARCHAR, Amount DECIMAL(10,2), Desc VARCHAR>;

CREATE TABLE IF NOT EXISTS Accounts (
	Id VARCHAR PRIMARY KEY,
	Balance DECIMAL(14,14)
) WITH ( KAFKA_TOPIC='Account', VALUE_FORMAT='Json', PARTITIONS='3', REPLICAS='3' );

The Description property within the Payment type has been customized to override the resulting column name as "Desc". Additionally, the Id property within the Account table has been designated as the primary key, while the Secret property is disregarded during code generation.

Aggregation functions

List of supported ksqldb aggregation functions:

Rest API reference

List of supported data types:

List of supported Joins:

List of supported pull query extension methods:

List of supported ksqlDB SQL statements:

KSqlDbContext

Config

Operators

Data definitions

Miscelenaous

Functions

LinqPad samples

Push Query

Pull Query

Nuget

https://www.nuget.org/packages/ksqlDB.RestApi.Client/

Scalar functions

Aggregation functions

Push query

Acknowledgements:

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on ksqlDb.RestApi.Client:

Package Downloads
SqlServer.Connector

SqlServer.Connector is a C# / .NET client API for consuming row-level table changes (CDC - Change Data Capture) from a SQL Server database with the Debezium connector streaming platform. With Kafka Connect and Debezium connectors you can stream data to and from Kafka and use it as your integral component of the ETL pipeline or create materialized views (caches) where it is needed, precompute the results of a query and store them for fast read access. See also https://www.nuget.org/packages/ksqlDb.RestApi.Client/ Targets .NET 5, .NET Core 3.1 and .NET Standard 2.0. Documentation for the library can be found at https://github.com/tomasfabian/ksqlDb.RestApi.Client-DotNet/blob/main/SqlServer.Connector/Wiki.md.

ksqlDb.RestApi.Client.ProtoBuf

ksqlDB.RestApi.Client.ProtoBuf adds support for Protobuf content type. ksqlDB.RestApi.Client is a C# LINQ-enabled client API for issuing and consuming ksqlDB push queries. Targets .NET 8, and .NET 9. Documentation for the library can be found at https://github.com/tomasfabian/ksqlDB.RestApi.Client-DotNet/blob/main/README.md.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.0.0 194 12/13/2024
6.5.2 1,612 12/4/2024
6.5.1 200 12/3/2024
6.5.0 7,265 10/27/2024
6.4.0 1,886 10/15/2024
6.3.0 567 10/13/2024
6.3.0-rc.1 61 10/11/2024
6.2.1 4,952 9/9/2024
6.2.0 4,608 8/3/2024
6.1.0 15,658 6/7/2024
6.1.0-rc.2 66 6/1/2024
6.1.0-rc.1 67 5/31/2024
6.0.2 1,379 5/29/2024
6.0.1 3,391 5/10/2024
6.0.0 1,897 4/30/2024
6.0.0-rc.2 74 4/26/2024
6.0.0-rc.1 53 4/24/2024
5.1.0 791 4/24/2024
5.0.0 2,405 4/15/2024
5.0.0-rc.3 66 4/11/2024
5.0.0-rc.2 70 4/6/2024
5.0.0-rc.1 72 4/5/2024
4.0.2 8,444 4/9/2024
4.0.1 2,591 3/22/2024
4.0.0 1,715 3/19/2024
4.0.0-rc.4 71 3/13/2024
4.0.0-rc.3 108 3/10/2024
4.0.0-rc.2 63 3/10/2024
4.0.0-rc.1 70 3/9/2024
3.6.2 1,720 3/9/2024
3.6.1 7,483 3/4/2024
3.6.0 380 3/1/2024
3.6.0-rc.2 165 2/21/2024
3.5.0 5,091 2/1/2024
3.4.0 5,785 12/6/2023
3.3.0 4,906 11/15/2023
3.2.2 6,349 9/12/2023
3.2.1 153,240 8/25/2023
3.2.0 4,688 7/14/2023
3.1.0 4,269 6/17/2023
3.0.1 7,624 3/30/2023
3.0.0 6,398 2/25/2023
3.0.0-rc.2 425 2/13/2023
3.0.0-rc.1 125 2/11/2023
2.7.0 24,189 1/14/2023
2.7.0-rc.2 383 1/10/2023
2.7.0-rc.1 392 1/2/2023
2.6.0 3,869 12/23/2022
2.5.2 2,489 12/8/2022
2.5.1 2,475 11/17/2022
2.5.0 3,003 11/3/2022
2.5.0-rc1 866 10/27/2022
2.4.0 6,727 9/26/2022
2.4.0-rc.3 128 9/26/2022
2.4.0-rc.2 137 9/25/2022
2.4.0-rc.1 117 9/21/2022
2.3.2 2,601 9/8/2022
2.3.1 9,421 8/17/2022
2.3.1-rc.1 256 8/5/2022
2.3.0 2,251 8/5/2022
2.3.0-rc.3 123 8/2/2022
2.3.0-rc.2 150 7/29/2022
2.2.1 2,331 7/27/2022
2.2.0 1,828 7/24/2022
2.2.0-rc.1 173 7/23/2022
2.1.4 2,305 7/16/2022
2.1.3 3,049 6/28/2022
2.1.1 1,523 6/23/2022
2.1.0 1,610 6/16/2022
2.1.0-rc.1 184 6/10/2022
2.0.1 2,074 5/27/2022
2.0.1-rc.1 197 5/26/2022
2.0.0 1,290 5/22/2022
2.0.0-rc.1 144 5/21/2022
1.7.0-rc.1 169 5/11/2022
1.6.0 6,284 3/25/2022
1.6.0-rc.1 188 3/20/2022
1.5.0 7,294 12/13/2021
1.5.0-rc.1 229 12/11/2021
1.4.0 1,845 12/1/2021
1.4.0-rc.1 174 11/27/2021
1.3.1 1,161 11/22/2021
1.3.0 2,119 11/20/2021
1.3.0-rc.1 233 11/15/2021
1.2.0 1,608 11/8/2021
1.2.0-rc.1 208 11/5/2021
1.1.0 1,087 11/2/2021
1.1.0-rc.1 200 10/27/2021
1.0.0 4,966 10/19/2021
1.0.0-rc.1 493 10/19/2021