alex-qiu / hyperf-clickhouse
Hyperf 的 Clickhouse 数据库
Requires
- the-tinderbox/clickhouse-builder: dev-master
This package is auto-updated.
Last update: 2024-09-29 06:14:48 UTC
README
最流行的库的 Hyperf 框架适配器
特性
无依赖
更多: https://github.com/smi2/phpClickHouse#features
先决条件
- PHP 7.1
- Hyperf PHP
- Clickhouse 服务器
安装
- 通过 composer 安装
$ composer require xtwoend/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',''), 'timeout_connect' => env('CLICKHOUSE_TIMEOUT_CONNECT',2), 'timeout_query' => env('CLICKHOUSE_TIMEOUT_QUERY',2), 'https' => (bool)env('CLICKHOUSE_HTTPS', null), 'retries' => env('CLICKHOUSE_RETRIES', 0), 'settings' => [ // optional 'max_partitions_per_insert_block' => 300, ], ],
然后修补您的 .env 文件
CLICKHOUSE_HOST=localhost
CLICKHOUSE_PORT=8123
CLICKHOUSE_DATABASE=default
CLICKHOUSE_USERNAME=default
CLICKHOUSE_PASSWORD=
CLICKHOUSE_TIMEOUT_CONNECT=2
CLICKHOUSE_TIMEOUT_QUERY=2
# only if you use https connection
CLICKHOUSE_HTTPS=true
- 使用
您可以直接使用 smi2/phpClickHouse 功能
/** @var \ClickHouseDB\Client $db */ $db = Clickhouse::connection('clickhouse')->getClient(); $statement = $db->select('SELECT * FROM summing_url_views LIMIT 2');
更多关于 $db 的信息请见此处: https://github.com/smi2/phpClickHouse/blob/master/README.md
或者使用 Eloquent ORM 的黎明(将完全实现)
- 添加模型
<?php namespace App\Models\Clickhouse; use Xtwoend\HyperfClickhouse\Model; class MyTable extends Model { // Not necessary. Can be obtained from class name MyTable => my_table protected $table = 'my_table'; }
- 添加迁移
<?php class CreateMyTable extends \Xtwoend\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(['model_name' => 'model 1', 'some_param' => 1]); # or $model = MyTable::make(['model_name' => 'model 1']); $model->some_param = 1; $model->save(); # or $model = new MyTable(); $model->fill(['model_name' => 'model 1', 'some_param' => 1])->save();
或批量插入
非关联方式
MyTable::insertBulk([['model 1', 1], ['model 2', 2]], ['model_name', 'some_param']);
关联方式
MyTable::insertAssoc([['model_name' => 'model 1', 'some_param' => 1], ['some_param' => 2, 'model_name' => 'model 2']]);
- 现在查看查询构建器
$rows = MyTable::select(['field_one', new RawColumn('sum(field_two)', 'field_two_sum')]) ->where('created_at', '>', '2020-09-14 12:47:29') ->groupBy('field_one') ->getRows();
高级用法
重试 您可以启用在收到非 200 响应时重试请求的功能,可能是由于网络连接问题。
修补您的 .env 文件
CLICKHOUSE_RETRIES=2
retries 是可选的,默认值为 0。0 表示只尝试一次。1 表示一次尝试加一次错误重试(总共两次尝试)。
处理大量行
您可以像在 Laravel 中一样分块结果
// Split the result into chunks of 30 rows $rows = MyTable::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 Xtwoend\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 Xtwoend\HyperfClickhouse\Model; class MyTable extends Model { protected $table = 'my_table_buffer'; }
OPTIMIZE 语句 请参阅 https://clickhouse.ac.cn/docs/ru/sql-reference/statements/optimize/
MyTable::optimize($final = false, $partition = null);
删除
请参阅 https://clickhouse.ac.cn/docs/en/sql-reference/statements/alter/delete/
MyTable::where('field_one', 123)->delete();
使用缓冲引擎并执行 OPTIMIZE 或 ALTER TABLE DELETE
<?php namespace App\Models\Clickhouse; use Xtwoend\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'; }