tangwei / hyperf-clickhouse
Hyperf 的 Clickhouse 数据库
v1.0.1
2024-07-12 09:52 UTC
Requires
- php: ^8.1
- hyperf/database: ~3.1.0
- hyperf/paginator: ~3.1.0
- hyperf/pool: ~3.1.0
- tangwei/clickhouse-builder: ~1.0.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: >=7.0
This package is auto-updated.
Last update: 2024-09-12 10:05:50 UTC
README
最受欢迎的库的 Hyperf 框架适配器
- https://github.com/tw2066/ClickhouseBuilder - 良好的查询构建器
功能
无依赖
更多:https://github.com/smi2/phpClickHouse#features
先决条件
- PHP 8.1
- Hyperf = 3.1
- Clickhouse 服务器
安装
- 通过 composer 安装
$ composer require tangwei/hyperf-clickhouse
- 将新的连接添加到您的 config/database.php 配置中
'clickhouse' => [ 'driver' => 'clickhouse', 'host' => env('CLICKHOUSE_HOST'), 'port' => env('CLICKHOUSE_PORT','8123'), 'database' => env('CLICKHOUSE_DATABASE','default'), 'username' => env('CLICKHOUSE_USERNAME','default'), 'password' => env('CLICKHOUSE_PASSWORD',''), 'https' => (bool)env('CLICKHOUSE_HTTPS',false), 'settings' => [ // optional // 'max_partitions_per_insert_block' => 300, ], 'pool' => [ 'min_connections' => 1, 'max_connections' => 3, 'connect_timeout' => 10.0, 'wait_timeout' => 3.0, 'heartbeat' => -1, 'max_idle_time' => 60, ], ],
然后修补您的 .env 文件
CLICKHOUSE_HOST=localhost
CLICKHOUSE_PORT=8123
CLICKHOUSE_DATABASE=default
CLICKHOUSE_USERNAME=default
CLICKHOUSE_PASSWORD=
# only if you use https connection
CLICKHOUSE_HTTPS=true
- 使用
您可以直接使用 smi2/phpClickHouse 功能
$client = \Tang\HyperfClickhouse\DB::query()->getClient(); $statement = $client->select('SELECT * FROM summing_url_views LIMIT 2');
更多关于 $db 的信息请参阅此处: https://github.com/smi2/phpClickHouse/blob/master/README.md
或使用 Eloquent ORM 的 dawnings(将完全实现)
- 添加模型
<?php namespace App\Models\Clickhouse; use Tang\HyperfClickhouse\Model; class MyTable extends Model { // Not necessary. Can be obtained from class name MyTable => my_table protected string $table = 'my_table'; }
- 添加迁移
<?php class CreateMyTable extends \Tang\HyperfClickhouse\Migration { /** * Run the migrations. * * @return void */ public function up() { static::write(' CREATE TABLE my_table ( id UInt32, created_at DateTime, field_one String, field_two Int32 ) ENGINE = MergeTree() ORDER BY (id) '); } /** * Reverse the migrations. * * @return void */ public function down() { static::write('DROP TABLE my_table'); } }
- 然后您可以插入数据
单行
$model = MyTable::create(['field_one' => 'model 1', 'field_two' => 1]); # or $model = MyTable::make(['field_one' => 'model 2']); $model->field_two = 2; $model->save(); # or $model = new MyTable(); $model->fill(['field_one' => 'model 3', 'field_two' => 3])->save();
或批量插入
MyTable::query()->insert(['field_one' => 'model 11','field_two' => 11]);
- 现在检查查询构建器
$rows = MyTable::query()->select(['field_one', \Tinderbox\ClickhouseBuilder\raw('sum(field_two)', 'field_two_sum')]) ->where('created_at', '>', '2020-09-14 12:47:29') ->groupBy('field_one') ->getCollect();
处理大量行
您可以像 Laravel 一样分块结果
// Split the result into chunks of 30 rows $rows = MyTable::query()->select(['field_one', 'field_two']) ->chunk(30, function ($rows) { foreach ($rows as $row) { echo $row['field_two'] . "\n"; } });
插入查询的缓冲引擎。参见 https://clickhouse.ac.cn/docs/en/engines/table-engines/special/buffer/
<?php namespace App\Models\Clickhouse; use Tang\HyperfClickhouse\Model; class MyTable extends Model { // Not necessary. Can be obtained from class name MyTable => my_table protected $table = 'my_table'; // All inserts will be in the table $tableForInserts // But all selects will be from $table protected $tableForInserts = 'my_table_buffer'; }
如果您还想从您的缓冲表中读取,请将表名放入 $table
<?php namespace App\Models\Clickhouse; use Tang\HyperfClickhouse\Model; class MyTable extends Model { protected $table = 'my_table_buffer'; }
OPTIMIZE 语句。参见 https://clickhouse.ac.cn/docs/ru/sql-reference/statements/optimize/
MyTable::query()->optimize($final = false, $partition = null);
删除
参见 https://clickhouse.ac.cn/docs/en/sql-reference/statements/alter/delete/
MyTable::query()->where('field_one', 123)->delete();
使用缓冲引擎并执行 OPTIMIZE 或 ALTER TABLE DELETE
<?php namespace App\Models\Clickhouse; use Tang\HyperfClickhouse\Model; class MyTable extends Model { // All SELECT's and INSERT's on $table protected $table = 'my_table_buffer'; // OPTIMIZE and DELETE on $tableSources protected $tableSources = 'my_table'; }