Set up ClickHouse in the AppHost
Bu içerik henüz dilinizde mevcut değil.
This article is the reference for the Aspire ClickHouse Hosting integration. It enumerates the AppHost APIs that you use to model ClickHouse server and database resources in your AppHost project.
If you’re new to the ClickHouse integration, start with the Get started with ClickHouse integrations guide. For how consuming apps read the connection information this page exposes, see Connect to ClickHouse.
Installation
Section titled “Installation”To start building an Aspire app that uses ClickHouse, install the 📦 Aspire.Hosting.ClickHouse NuGet package:
aspire add clickhouseLearn more about aspire add in the command reference.
Or, choose a manual installation approach:
#:package Aspire.Hosting.ClickHouse@*<PackageReference Include="Aspire.Hosting.ClickHouse" Version="*" />Add ClickHouse server resource
Section titled “Add ClickHouse server resource”Once you’ve installed the hosting integration in your AppHost project, you can add a ClickHouse server resource and then add a database resource as shown in the following example:
var builder = DistributedApplication.CreateBuilder(args);
var clickhouse = builder.AddClickHouse("clickhouse");var clickhousedb = clickhouse.AddDatabase("clickhousedb");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(clickhousedb) .WaitFor(clickhousedb);
// After adding all resources, run the app...-
When Aspire adds a container image to the AppHost, as shown in the preceding example with the
clickhouse/clickhouse-serverimage, it creates a new ClickHouse server instance on your local machine. A reference to theclickhousedbdatabase resource is then used to add a dependency to the consuming project. -
The database is automatically created using
CREATE DATABASE IF NOT EXISTSwhen the server resource becomes ready. -
The ClickHouse server resource includes default credentials with a
CLICKHOUSE_USERofdefaultand a randomly generatedCLICKHOUSE_PASSWORD. -
The password is stored in the AppHost’s secret store in the
Parameterssection:JSON — secrets.json {"Parameters:clickhouse-password": "<THE_GENERATED_PASSWORD>"} -
The
WithReferencecall configures a connection in the consuming project named after the referenced database resource, such asclickhousedbin the preceding example.
Add ClickHouse resource with data volume
Section titled “Add ClickHouse resource with data volume”Add a data volume to the ClickHouse resource as shown in the following example:
var builder = DistributedApplication.CreateBuilder(args);
var clickhouse = builder.AddClickHouse("clickhouse") .WithDataVolume(isReadOnly: false);
var clickhousedb = clickhouse.AddDatabase("clickhousedb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(clickhousedb) .WaitFor(clickhousedb);
// After adding all resources, run the app...The data volume is used to persist the ClickHouse data outside the lifecycle of its container. The data volume is mounted at the /var/lib/clickhouse path in the ClickHouse container and when a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.
Add ClickHouse resource with data bind mount
Section titled “Add ClickHouse resource with data bind mount”Add a data bind mount to the ClickHouse resource as shown in the following example:
var builder = DistributedApplication.CreateBuilder(args);
var clickhouse = builder.AddClickHouse("clickhouse") .WithDataBindMount( source: "/ClickHouse/Data", isReadOnly: false);
var clickhousedb = clickhouse.AddDatabase("clickhousedb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(clickhousedb) .WaitFor(clickhousedb);
// After adding all resources, run the app...Data bind mounts rely on the host machine’s filesystem to persist the ClickHouse data across container restarts. The data bind mount is mounted at the C:\ClickHouse\Data on Windows (or /ClickHouse/Data on Unix) path on the host machine in the ClickHouse container. For more information on data bind mounts, see Docker docs: Bind mounts.
Add ClickHouse resource with parameters
Section titled “Add ClickHouse resource with parameters”When you want to explicitly provide the username and password used by the container image, you can provide these credentials as parameters:
var builder = DistributedApplication.CreateBuilder(args);
var username = builder.AddParameter("username", secret: true);var password = builder.AddParameter("password", secret: true);
var clickhouse = builder.AddClickHouse("clickhouse", userName: username, password: password);var clickhousedb = clickhouse.AddDatabase("clickhousedb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(clickhousedb) .WaitFor(clickhousedb);
// After adding all resources, run the app...The username and password parameters are usually specified as user secrets:
{ "Parameters": { "username": "default", "password": "your-secure-password" }}For more information, see External parameters.
You can also specify a custom host port:
var clickhouse = builder.AddClickHouse("clickhouse", port: 18123);Connection properties
Section titled “Connection properties”For the full reference of ClickHouse connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to ClickHouse.
Hosting integration health checks
Section titled “Hosting integration health checks”The ClickHouse hosting integration automatically adds a health check for the ClickHouse resource. The health check sends an HTTP GET request to the /ping endpoint on the ClickHouse server and verifies that the instance is running and responsive.