kafkiansky/phpclick

ClickHouse async php client.

0.1.0 2024-06-30 15:09 UTC

This package is auto-updated.

Last update: 2024-10-30 16:35:45 UTC


README

This package can be installed as a Composer dependency.

composer require kafkiansky/phpclick

This package requires PHP 8.2 or later.

Usage

From Memory

This client is designed to be inserted into clickhouse in large batches. RowBinary mode is used for insertion. This means that you must know the types of table columns and their order.

For example, let's look at the following table DDL:

CREATE TABLE IF NOT EXISTS logs (
    `LogName` String CODEC(ZSTD(1)),
    `LogTime` DateTime64(9) CODEC(Delta(8), ZSTD(1)),
    `LogAttributes` Map(LowCardinality(String), String) CODEC(ZSTD(1)),
    `LogUserId` Nullable(Int64)
)   ENGINE = MergeTree()
    ORDER BY (LogName, toUnixTimestamp(LogTime))
;

You can insert directly from memory using Row:

<?php

declare(strict_types=1);

require_once __DIR__.'/vendor/autoload.php';

use Kafkiansky\PHPClick;

$client = new PHPClick\Client('http://127.0.0.1:8124/');

$client->insert(
    PHPClick\Query::string('INSERT INTO test.logs (LogName, LogTime, LogAttributes, LogUserId)'),
    PHPClick\Batch::fromRows(
        PHPClick\Row::columns(
            PHPClick\Column::string('users'),
            PHPClick\Column::dateTime64(new \DateTimeImmutable()),
            PHPClick\Column::map(
                [PHPClick\Column::string('x'), PHPClick\Column::string('y')],
            ),
            PHPClick\Column::nullable(
                PHPClick\Column::int64(13),
            ),
        ),
        PHPClick\Row::columns(
            PHPClick\Column::string('users'),
            PHPClick\Column::dateTime64(new \DateTimeImmutable()),
            PHPClick\Column::map(
                [PHPClick\Column::string('x'), PHPClick\Column::string('y')],
            ),
            PHPClick\Column::nullable(),
        ),
    ),
);

From resource

Or accumulate data in a file and then paste data from it:

<?php

declare(strict_types=1);

require_once __DIR__.'/vendor/autoload.php';

use Kafkiansky\PHPClick;

PHPClick\rowsToFile(
    __DIR__.'/click.log', // or resource
    PHPClick\Row::columns(
        PHPClick\Column::string('users'),
        PHPClick\Column::dateTime64(new \DateTimeImmutable()),
        PHPClick\Column::map(
            [PHPClick\Column::string('x'), PHPClick\Column::string('y')],
        ),
        PHPClick\Column::nullable(
            PHPClick\Column::int64(13),
        ),
    ),
    PHPClick\Row::columns(
        PHPClick\Column::string('users'),
        PHPClick\Column::dateTime64(new \DateTimeImmutable()),
        PHPClick\Column::map(
            [PHPClick\Column::string('x'), PHPClick\Column::string('y')],
        ),
        PHPClick\Column::nullable(),
    ),
);

$client = new PHPClick\Client('http://127.0.0.1:8124/');

$logHandle = fopen(__DIR__.'/click.log', 'r');

try {
    $client->insert(
        PHPClick\Query::string('INSERT INTO test.logs (LogName, LogTime, LogAttributes, LogUserId)'),
        PHPClick\Batch::fromStream($logHandle),
    );
} finally {
    if (\is_resource($logHandle)) {
        \fclose($logHandle);
    }
}

Query builder

You can use a simple query builder (insert-only) instead of a query string:

<?php

declare(strict_types=1);

require_once __DIR__.'/vendor/autoload.php';

use Kafkiansky\PHPClick;

$client = new PHPClick\Client('http://127.0.0.1:8124/');

$client->insert(
    PHPClick\Query::builder('test.logs')->columns(
        'LogName',
        'LogTime',
        'LogAttributes',
        'LogUserId',
    ),
    PHPClick\Batch::fromRows(/* rows here */),
);

Types

All available clickhouse types are listed below:

Testing

$ composer test

License

The MIT License (MIT). See License File for more information.