Example #1
0
    public static KSqlDBContextOptions CreateQueryStreamOptions(string ksqlDbUrl)
    {
        var contextOptions = new KSqlDbContextOptionsBuilder()
                             .UseKSqlDb(ksqlDbUrl)
                             .SetBasicAuthCredentials("fred", "letmein")
                             .SetJsonSerializerOptions(jsonOptions =>
        {
            jsonOptions.IgnoreReadOnlyFields = true;
            jsonOptions.AddContext <SourceGenerationContext>();
        })
                                                                                      //.SetAutoOffsetReset(AutoOffsetReset.Earliest) // global setting
                             .SetProcessingGuarantee(ProcessingGuarantee.ExactlyOnce) // global setting
                             .SetupQueryStream(options =>
        {
            //SetupQueryStream affects only IKSqlDBContext.CreateQueryStream<T>
            options.AutoOffsetReset = AutoOffsetReset.Earliest;
        })
                             .SetupQuery(options =>
        {
            //SetupQuery affects only IKSqlDBContext.CreateQuery<T>
            options.Properties[KSqlDbConfigs.ProcessingGuarantee] = ProcessingGuarantee.ExactlyOnce.ToKSqlValue();
        })
                             .Options;

        contextOptions.DisposeHttpClient = false;

        return(contextOptions);
    }
Example #2
0
        public static KSqlDBContextOptions CreateQueryStreamOptions(string ksqlDbUrl)
        {
            var contextOptions = new KSqlDbContextOptionsBuilder()
                                 .UseKSqlDb(ksqlDbUrl)
                                 .SetupQuery(options =>
            {
                options.Properties[QueryParameters.AutoOffsetResetPropertyName] = AutoOffsetReset.Latest.ToString().ToLower(); // "latest"
            })
                                 .Options;

            return(contextOptions);
        }
    /// <summary>
    /// Registers the given ksqldb context and its dependencies as services in the <see cref="IServiceCollection" />.
    /// </summary>
    /// <typeparam name="TContextService">The type of context to be registered as.</typeparam>
    /// <typeparam name="TContextImplementation">The implementation type of the context to be registered.</typeparam>
    /// <param name="services">The IServiceCollection to add services to.</param>
    /// <param name="ksqlDbUrl">ksqlDb connection.</param>
    /// <param name="setupAction">Optional action to configure the KSqlDbContextOptions for the context.</param>
    /// <param name="contextLifetime">The lifetime with which to register the context service in the container.</param>
    /// <param name="restApiLifetime">The lifetime with which to register the IKSqlDbRestApiClient service in the container.</param>
    /// <returns>The original IServiceCollection.</returns>
    public static IServiceCollection ConfigureKSqlDb <TContextService, TContextImplementation>(this IServiceCollection services,
                                                                                               string ksqlDbUrl,
                                                                                               Action <ISetupParameters> setupAction = null,
                                                                                               ServiceLifetime contextLifetime       = ServiceLifetime.Scoped,
                                                                                               ServiceLifetime restApiLifetime       = ServiceLifetime.Scoped)
        where TContextService : IKSqlDBContext
        where TContextImplementation : IKSqlDBContext
    {
        var builder = new KSqlDbContextOptionsBuilder();

        var setupParameters = builder.UseKSqlDb(ksqlDbUrl);

        setupAction?.Invoke(setupParameters);

        return(services.ConfigureKSqlDb <TContextService, TContextImplementation>(builder, contextLifetime, restApiLifetime));
    }
    /// <summary>
    /// Registers the given ksqldb context as a service in the <see cref="IServiceCollection" />.
    /// </summary>
    /// <typeparam name="TContextService">The type of context to be registered as.</typeparam>
    /// <typeparam name="TContextImplementation">The implementation type of the context to be registered.</typeparam>
    /// <param name="services">The IServiceCollection to add services to.</param>
    /// <param name="optionsAction">Action to configure the KSqlDbContextOptions for the context.</param>
    /// <param name="contextLifetime">The lifetime with which to register the TContext service in the container.</param>
    /// <param name="restApiLifetime">The lifetime with which to register the IKSqlDbRestApiClient service in the container.</param>
    /// <returns>The original IServiceCollection.</returns>
    public static IServiceCollection AddDbContext <TContextService, TContextImplementation>(this IServiceCollection services,
                                                                                            Action <KSqlDbContextOptionsBuilder> optionsAction,
                                                                                            ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
                                                                                            ServiceLifetime restApiLifetime = ServiceLifetime.Scoped)
        where TContextService : IKSqlDBContext
        where TContextImplementation : IKSqlDBContext
    {
        if (optionsAction == null)
        {
            throw new ArgumentNullException(nameof(optionsAction));
        }

        var builder = new KSqlDbContextOptionsBuilder();

        optionsAction(builder);

        services.ConfigureKSqlDb <TContextService, TContextImplementation>(builder, contextLifetime, restApiLifetime);

        return(services);
    }
    public async Task ExecuteAsync()
    {
        string ksqlDbUrl = @"http:\\localhost:8088";

        var contextOptions = new KSqlDbContextOptionsBuilder()
                             .UseKSqlDb(ksqlDbUrl)
                             .SetBasicAuthCredentials("fred", "letmein")
                             .Options;

        contextOptions.DisposeHttpClient = false;

        await using var context = new KSqlDBContext(contextOptions);

        var httpClient = new HttpClient()
        {
            BaseAddress = new Uri(ksqlDbUrl)
        };

        var httpClientFactory = new HttpClientFactory(httpClient);

        restApiClient = new KSqlDbRestApiClient(httpClientFactory)
                        .SetCredentials(new BasicAuthCredentials("fred", "letmein"));

        ((KSqlDbRestApiClient)restApiClient).DisposeHttpClient = false;

        await CreateOrReplaceStreamAsync();

        var statement = context.CreateTableStatement(MaterializedViewName)
                        .As <IoTSensor>("sensor_values")
                        .GroupBy(c => c.SensorId)
                        .WindowedBy(new TimeWindows(Duration.OfSeconds(5)).WithGracePeriod(Duration.OfHours(2)))
                        .Select(c => new { SensorId = c.Key, AvgValue = c.Avg(g => g.Value) });

        var query = statement.ToStatementString();

        var response = await statement.ExecuteStatementAsync();

        response = await InsertAsync(new IoTSensor { SensorId = "sensor-1", Value = 11 });

        await PullSensor(context);
    }
    internal static IServiceCollection ConfigureKSqlDb <TContextService, TContextImplementation>(this IServiceCollection services,
                                                                                                 KSqlDbContextOptionsBuilder builder,
                                                                                                 ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
                                                                                                 ServiceLifetime restApiLifetime = ServiceLifetime.Scoped)
        where TContextService : IKSqlDBContext
        where TContextImplementation : IKSqlDBContext
    {
        var contextOptions = builder.InternalOptions;

        contextOptions.ServiceCollection.AddSingleton(contextOptions);

        var contextServiceDescriptor = new ServiceDescriptor(
            typeof(TContextService),
            typeof(TContextImplementation),
            contextLifetime);

        contextOptions.ServiceCollection.Add(contextServiceDescriptor);

        contextOptions.ServiceCollection.ConfigureHttpClients(contextOptions);

        var restApiServiceDescriptor = new ServiceDescriptor(
            typeof(IKSqlDbRestApiClient),
            typeof(KSqlDbRestApiClient),
            restApiLifetime);

        contextOptions.ServiceCollection.Add(restApiServiceDescriptor);

        contextOptions.Apply(services);

        return(services);
    }