इसे छोड़कर कंटेंट पर जाएं
Docs Try Aspire
Docs Try

Connect to ClickHouse

यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।

ClickHouse logo

This page describes how consuming apps connect to a ClickHouse resource that’s already modeled in your AppHost. For the AppHost API surface — adding a ClickHouse server, databases, volumes, and more — see ClickHouse Hosting integration.

When you reference a ClickHouse resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire ClickHouse client integration for automatic dependency injection, health checks, and telemetry.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Host property of a resource called clickhousedb becomes CLICKHOUSEDB_HOST.

The ClickHouse server resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the ClickHouse server
PortThe port number the ClickHouse server is listening on (default: 8123)
UsernameThe username for authentication (default: default)
PasswordThe password for authentication

Example connection string:

Host=localhost;Port=8123;Username=default;Password=p%40ssw0rd1

The ClickHouse database resource inherits all properties from its parent server resource and adds:

Property NameDescription
DatabaseNameThe name of the database

Example connection string:

Host=localhost;Port=8123;Username=default;Password=p@ssw0rd1;Database=clickhousedb

Pick the language your consuming app is written in. Each example assumes your AppHost adds a ClickHouse database resource named clickhousedb and references it from the consuming app.

For C# apps, the recommended approach is the Aspire ClickHouse client integration. It registers an IClickHouseClient and a ClickHouseDataSource through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.

Install the 📦 Aspire.ClickHouse.Driver NuGet package in the client-consuming project:

.NET CLI — Add Aspire.ClickHouse.Driver package
dotnet add package Aspire.ClickHouse.Driver

In Program.cs, call AddClickHouseDataSource on your IHostApplicationBuilder to register both IClickHouseClient and ClickHouseDataSource:

C# — Program.cs
builder.AddClickHouseDataSource(connectionName: "clickhousedb");

Use IClickHouseClient for a simple query and insert API:

C# — ExampleService.cs
public class ExampleService(IClickHouseClient client)
{
// Use client...
}

Or use ClickHouseDataSource when you need ADO.NET access (connections, commands, transactions):

C# — ExampleService.cs
public class ExampleService(ClickHouseDataSource dataSource)
{
// Use data source...
}

To register multiple ClickHouseDataSource instances with different connection names, use AddKeyedClickHouseDataSource:

C# — Program.cs
builder.AddKeyedClickHouseDataSource(name: "analytics");
builder.AddKeyedClickHouseDataSource(name: "logging");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("analytics")] IClickHouseClient analyticsClient,
[FromKeyedServices("logging")] IClickHouseClient loggingClient)
{
// Use clients...
}

For more information on keyed services, see .NET dependency injection: Keyed services.

The Aspire ClickHouse client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddClickHouseDataSource:

C# — Program.cs
builder.AddClickHouseDataSource("clickhousedb");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"clickhousedb": "Host=myserver;Port=8123;Username=default;Database=clickhousedb"
}
}

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads ClickHouseClientSettings from appsettings.json (or any other configuration source) by using the Aspire:ClickHouse:Driver key:

JSON — appsettings.json
{
"Aspire": {
"ClickHouse": {
"Driver": {
"ConnectionString": "Host=myserver;Port=8123;Username=default;Database=clickhousedb",
"DisableHealthChecks": false,
"DisableTracing": false,
"DisableMetrics": true,
"HealthCheckTimeout": "00:00:03"
}
}
}
}

Inline delegates. Pass an Action<ClickHouseClientSettings> to configure settings inline, for example to disable health checks:

C# — Program.cs
builder.AddClickHouseDataSource(
"clickhousedb",
static settings => settings.DisableHealthChecks = true);

Aspire client integrations enable health checks by default. The ClickHouse client integration adds:

  • A health check that calls PingAsync on the configured ClickHouseDataSource. If the ping succeeds, the health check is considered healthy.
  • Integration with the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire ClickHouse client integration automatically configures logging, tracing, and metrics through OpenTelemetry.

Logging categories:

  • ClickHouse.Driver
  • ClickHouse.Driver.BulkCopy
  • ClickHouse.Driver.Client
  • ClickHouse.Driver.Command
  • ClickHouse.Driver.Connection
  • ClickHouse.Driver.Transport

Tracing activities:

  • ClickHouse.Driver

Metrics: The ClickHouse client integration doesn’t currently expose OpenTelemetry metrics. The DisableMetrics setting defaults to true.

Any logging or tracing features can be disabled through the configuration options above.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection string from the environment and use it with the ClickHouse.Driver package directly:

C# — Program.cs
using ClickHouse.Driver;
var connectionString = Environment.GetEnvironmentVariable(
"ConnectionStrings__clickhousedb");
var dataSource = new ClickHouseDataSource(connectionString!);
// Use dataSource to query ClickHouse...