PHP code example of rasuvaeff / clickhouse-toolkit
1. Go to this page and download the library: Download rasuvaeff/clickhouse-toolkit library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Rasuvaeff\ClickHouseToolkit\ClickHouseClientFactory;
use Rasuvaeff\ClickHouseToolkit\ClickHouseConfig;
use Rasuvaeff\ClickHouseToolkit\ClickHouseDataType as T;
use Rasuvaeff\ClickHouseToolkit\ClickHouseQueryBuilder;
use SimPod\ClickHouseClient\Format\JsonEachRow;
use Yiisoft\Data\Reader\Filter\In;
use Yiisoft\Data\Reader\Sort;
// 1. Build a client.
$client = (new ClickHouseClientFactory(new ClickHouseConfig(
host: 'clickhouse',
port: 8123,
database: 'app',
username: 'default',
password: '',
)))->create();
// 2. Build a safe, parameterized query from user-supplied filters.
$qb = new ClickHouseQueryBuilder(
allowedFields: ['id', 'status', 'created_at'],
fieldTypes: ['id' => T::UInt64, 'created_at' => T::DateTime],
defaultSort: 'id DESC',
);
$where = $qb->buildWhere(new In('status', ['active', 'pending']));
$orderBy = $qb->buildOrderBy(Sort::only(['created_at'])->withOrder(['created_at' => 'desc']));
$sql = $qb->buildSelect(table: 'events', columns: ['id', 'status'], where: $where->sql, orderBy: $orderBy, limit: 20);
// 3. Execute.
$output = $where->isEmpty()
? $client->select($sql, new JsonEachRow())
: $client->selectWithParams($sql, $where->params, new JsonEachRow());
foreach ($output->data as $row) {
// ...
}
final readonly class ClickHouseConfig
{
public function __construct(
public string $host = '127.0.0.1',
public int $port = 8123,
public string $database = 'default',
public string $username = 'default',
public string $password = '',
public bool $secure = false, // true -> https://
) {}
public function baseUri(): string; // e.g. "http://127.0.0.1:8123"
}
use Rasuvaeff\ClickHouseToolkit\ClickHouseClientFactory;
use Rasuvaeff\ClickHouseToolkit\ClickHouseConfig;
// Auto-discovers an installed PSR-18 client + PSR-17 factories:
$client = (new ClickHouseClientFactory(new ClickHouseConfig(
host: 'ch.internal',
secure: true, // https
)))->create();
$client->executeQuery('SELECT 1');
use GuzzleHttp\Client;
$factory = new ClickHouseClientFactory(
config: new ClickHouseConfig(host: 'ch.internal', secure: true),
httpClient: new Client(['timeout' => 10.0]),
// requestFactory / streamFactory / uriFactory are optional (auto-discovered when null)
);
public function __construct(
private array $allowedFields, // list<string>
private array $fieldTypes = [], // field => ClickHouse type, default "String" (use ClickHouseDataType constants)
private string $defaultSort = '', // no ORDER BY by default; pass e.g. 'id DESC' for stable pagination
private ?FilterInterface $mandatoryFilter = null,
private ?string $serverTimezone = null, // IANA timezone; DateTime values are converted before formatting
) {}
$qb = new ClickHouseQueryBuilder(
allowedFields: ['created_at'],
fieldTypes: ['created_at' => T::DateTime],
serverTimezone: 'UTC',
);
// A DateTimeImmutable in Europe/Moscow (+03:00) will be formatted as UTC.
$where = $qb->buildWhere(new Equals('created_at', new \DateTimeImmutable('2024-06-15 15:00:00+03:00')));
// params: ['p0' => '2024-06-15 12:00:00']
use Rasuvaeff\ClickHouseToolkit\ClickHouseClientFactory;
use Rasuvaeff\ClickHouseToolkit\ClickHouseConfig;
use Rasuvaeff\ClickHouseToolkit\ClickHouseMigrationRunner;
use Rasuvaeff\ClickHouseToolkit\ClickHouseMigrationRunnerInterface;
use SimPod\ClickHouseClient\Client\ClickHouseClient;
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
return [
ClickHouseConfig::class => static fn (): ClickHouseConfig => new ClickHouseConfig(
host: $_ENV['CLICKHOUSE_HOST'] ?? 'clickhouse',
port: (int) ($_ENV['CLICKHOUSE_PORT'] ?? 8123),
database: $_ENV['CLICKHOUSE_DB'] ?? 'app',
username: $_ENV['CLICKHOUSE_USER'] ?? 'default',
password: $_ENV['CLICKHOUSE_PASSWORD'] ?? '',
),
PsrClickHouseClient::class => static fn (ClickHouseClientFactory $f): PsrClickHouseClient => $f->create(),
ClickHouseClient::class => PsrClickHouseClient::class, // toolkit classes type-hint the interface
ClickHouseMigrationRunnerInterface::class => static fn (ClickHouseClient $client): ClickHouseMigrationRunner => new ClickHouseMigrationRunner(
client: $client,
migrationsPath: dirname(__DIR__) . '/resources/clickhouse-migrations',
),
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.