xtwoend/hyperf-clickhouse

hyperf 的 Clickhouse 数据库

dev-master / 1.0.x-dev 2022-05-22 02:22 UTC

This package is auto-updated.

Last update: 2024-09-22 07:26:24 UTC


README

最受欢迎的库适配 Hyperf 框架的适配器

特性

无依赖

更多: https://github.com/smi2/phpClickHouse#features

先决条件

  • PHP 7.1
  • Hyperf PHP
  • Clickhouse 服务器

安装

  1. 通过 composer 安装
$ composer require xtwoend/hyperf-clickhouse
  1. 将新连接添加到 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
  1. 使用

您可以直接使用 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 的黎明(将完全实现)

  1. 添加模型
<?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';

}
  1. 添加迁移
<?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');
    }
}
  1. 然后您可以插入数据

单行

$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']]);
  1. 现在查看查询构建器
$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

重试是可选的,默认值为 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';
}

优化语句 请参阅 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';
}