kafkiansky/phpclick
最新稳定版本:0.3.0
Composer 安装命令:
composer require kafkiansky/phpclick
包简介
ClickHouse async php client.
关键字:
README 文档
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:
| ClickHouse Type | API Type |
|---|---|
Bool |
\Kafkiansky\PHPClick\Column::bool |
Int8 |
\Kafkiansky\PHPClick\Column::int8 |
Uint8 |
\Kafkiansky\PHPClick\Column::uint8 |
Int16 |
\Kafkiansky\PHPClick\Column::int16 |
Uint16 |
\Kafkiansky\PHPClick\Column::uint16 |
Int32 |
\Kafkiansky\PHPClick\Column::int32 |
Uint32 |
\Kafkiansky\PHPClick\Column::uint32 |
Int64 |
\Kafkiansky\PHPClick\Column::int64 |
Uint64 |
\Kafkiansky\PHPClick\Column::uint64 |
Float32 |
\Kafkiansky\PHPClick\Column::float32 |
Float64 |
\Kafkiansky\PHPClick\Column::float64 |
String |
\Kafkiansky\PHPClick\Column::string |
DateTime |
\Kafkiansky\PHPClick\Column::dateTime |
DateTime64 |
\Kafkiansky\PHPClick\Column::dateTime64 |
Nullable |
\Kafkiansky\PHPClick\Column::nullable |
Map |
\Kafkiansky\PHPClick\Column::map |
Array |
\Kafkiansky\PHPClick\Column::array |
Testing
$ composer test
License
The MIT License (MIT). See License File for more information.
统计信息
- 总下载量: 21.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-30