ClickHouse hosting integration
Цей контент ще не доступний вашою мовою.
The ClickHouse hosting integration models a ClickHouse server and database as the ClickHouseServerResource and ClickHouseDatabaseResource types. To access these types and APIs, add the 📦 Aspire.Hosting.ClickHouse NuGet package in your AppHost project:
aspire add clickhouseAspire CLI інтерактивний; оберіть відповідний результат пошуку:
Select an integration to add:
> clickhouse (Aspire.Hosting.ClickHouse)> Other results listed as selectable options...#:package Aspire.Hosting.ClickHouse@*<PackageReference Include="Aspire.Hosting.ClickHouse" Version="*" />For an introduction to the ClickHouse integration, see Get started with the ClickHouse integrations.
Installation
Section titled “Installation”The Aspire ClickHouse hosting integration models the ClickHouse database server as the following types:
ClickHouseServerResourceClickHouseDatabaseResource
To access these types and APIs for expressing them as resources in your AppHost project, install the 📦 Aspire.Hosting.ClickHouse NuGet package:
aspire add clickhouseAspire CLI інтерактивний; оберіть відповідний результат пошуку:
Select an integration to add:
> clickhouse (Aspire.Hosting.ClickHouse)> Other results listed as selectable options...#:package Aspire.Hosting.ClickHouse@*<PackageReference Include="Aspire.Hosting.ClickHouse" Version="*" />Add ClickHouse server and database resources
Section titled “Add ClickHouse server and database resources”In the AppHost project, call AddClickHouse to add and return a ClickHouse server resource builder. Chain a call to the returned resource builder to AddDatabase, to add a ClickHouse database to the server resource:
var builder = DistributedApplication.CreateBuilder(args);
var clickhouse = builder.AddClickHouse("clickhouse");
var clickhousedb = clickhouse.AddDatabase("clickhousedb");
builder.AddProject<Projects.ExampleProject>() .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-server image, it creates a new ClickHouse server on your local machine. A reference to your ClickHouse resource builder (the clickhouse variable) is used to add a database. The database is named clickhousedb and then added to the ExampleProject. The database is automatically created using CREATE DATABASE IF NOT EXISTS when the server resource becomes ready.
The ClickHouse server resource includes default credentials:
CLICKHOUSE_USER: A value ofdefaultCLICKHOUSE_PASSWORD: Random password generated using the default password parameter
The password is stored in the AppHost’s secret store in the Parameters section:
{ "Parameters:clickhouse-password": "<THE_GENERATED_PASSWORD>"}The WithReference method configures a connection in the ExampleProject named "clickhousedb".
Add ClickHouse resource with data volume
Section titled “Add ClickHouse resource with data volume”To add a data volume to the ClickHouse resource, call the WithDataVolume method on the ClickHouse resource:
var builder = DistributedApplication.CreateBuilder(args);
var clickhouse = builder.AddClickHouse("clickhouse") .WithDataVolume();
var clickhousedb = clickhouse.AddDatabase("clickhousedb");
builder.AddProject<Projects.ExampleProject>() .WithReference(clickhousedb) .WaitFor(clickhousedb);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”To add a data bind mount to the ClickHouse resource, call the WithDataBindMount method:
var builder = DistributedApplication.CreateBuilder(args);
var clickhouse = builder.AddClickHouse("clickhouse") .WithDataBindMount( source: @"C:\ClickHouse\Data", isReadOnly: false);
var clickhousedb = clickhouse.AddDatabase("clickhousedb");
builder.AddProject<Projects.ExampleProject>() .WithReference(clickhousedb) .WaitFor(clickhousedb);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");var password = builder.AddParameter("password", secret: true);
var clickhouse = builder.AddClickHouse("clickhouse", userName: username, password: password);var clickhousedb = clickhouse.AddDatabase("clickhousedb");
builder.AddProject<Projects.ExampleProject>() .WithReference(clickhousedb) .WaitFor(clickhousedb);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”When you use the WithReference method to pass a ClickHouse server or database resource from the AppHost project to a consuming client project, several properties are available to use in the consuming project.
ClickHouse server
Section titled “ClickHouse server”The ClickHouse server resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Host | The hostname or IP address of the ClickHouse server |
Port | The port number the ClickHouse server is listening on (default: 8123) |
Username | The username for authentication (default: default) |
Password | The password for authentication |
Example connection string:
Host=localhost;Port=8123;Username=default;Password=p%40ssw0rd1ClickHouse database
Section titled “ClickHouse database”The ClickHouse database resource inherits all properties from its parent ClickHouseServerResource and adds:
| Property Name | Description |
|---|---|
DatabaseName | The ClickHouse database name |
Example connection string:
Host=localhost;Port=8123;Username=default;Password=p@ssw0rd1;Database=clickhousedbHosting 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.
Using with non-.NET applications
Section titled “Using with non-.NET applications”When you use the WithReference method to pass a ClickHouse database resource to a non-.NET application (such as Python or JavaScript), Aspire automatically injects environment variables that describe the connection information.
For example, if you reference a ClickHouse database resource named clickhousedb:
var builder = DistributedApplication.CreateBuilder(args);
var clickhouse = builder.AddClickHouse("clickhouse");var clickhousedb = clickhouse.AddDatabase("clickhousedb");
var pythonApp = builder.AddPythonApp("python-app", "./python_app", "main.py") .WithReference(clickhousedb);The following environment variables are available in the consuming application:
CLICKHOUSEDB_HOST- The hostname of the ClickHouse serverCLICKHOUSEDB_PORT- The port numberCLICKHOUSEDB_USERNAME- The username for authenticationCLICKHOUSEDB_PASSWORD- The password for authenticationCLICKHOUSEDB_DATABASENAME- The database name
You can access these environment variables in your application code:
import clickhouse_connectimport os
# Get connection propertieshost = os.getenv("CLICKHOUSEDB_HOST")port = int(os.getenv("CLICKHOUSEDB_PORT", "8123"))username = os.getenv("CLICKHOUSEDB_USERNAME", "default")password = os.getenv("CLICKHOUSEDB_PASSWORD", "")database = os.getenv("CLICKHOUSEDB_DATABASENAME")
# Create ClickHouse clientclient = clickhouse_connect.get_client( host=host, port=port, username=username, password=password, database=database)
# Use the clientresult = client.command("SELECT 1")print(f"ClickHouse connection status: {result}")import { createClient } from '@clickhouse/client';
// Get connection propertiesconst host = process.env.CLICKHOUSEDB_HOST;const port = process.env.CLICKHOUSEDB_PORT;const username = process.env.CLICKHOUSEDB_USERNAME;const password = process.env.CLICKHOUSEDB_PASSWORD;const database = process.env.CLICKHOUSEDB_DATABASENAME;
// Create ClickHouse clientconst client = createClient({ url: `http://${host}:${port}`, username: username, password: password, database: database,});
// Use the clientconst result = await client.ping();console.log('ClickHouse connection status:', result.success);