定制 xtwoend/hyperf-clickhouse 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xtwoend/hyperf-clickhouse

Composer 安装命令:

composer require xtwoend/hyperf-clickhouse

包简介

Clickhouse db for hyperf

README 文档

README

Adapter to Hyperf framework of the most popular libraries:

Features

No dependency

More: https://github.com/smi2/phpClickHouse#features

Prerequisites

  • PHP 7.1
  • Hyperf PHP
  • Clickhouse server

Installation

  1. Install via composer
$ composer require xtwoend/hyperf-clickhouse
  1. Add new connection into your 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,
    ],
],

Then patch your .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. Used

You can use smi2/phpClickHouse functionality directly:

/** @var \ClickHouseDB\Client $db */
$db = Clickhouse::connection('clickhouse')->getClient();
$statement = $db->select('SELECT * FROM summing_url_views LIMIT 2');

More about $db see here: https://github.com/smi2/phpClickHouse/blob/master/README.md

Or use dawnings of Eloquent ORM (will be implemented completely)

  1. Add model
<?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. Add migration
<?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. And then you can insert data

One row

$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();

Or bulk insert

Non assoc way

MyTable::insertBulk([['model 1', 1], ['model 2', 2]], ['model_name', 'some_param']);

Assoc way

MyTable::insertAssoc([['model_name' => 'model 1', 'some_param' => 1], ['some_param' => 2, 'model_name' => 'model 2']]);
  1. Now check out the query builder
$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();

Advanced usage

Retries You may enable ability to retry requests while received not 200 response, maybe due network connectivity problems.

Patch your .env:

CLICKHOUSE_RETRIES=2

retries is optional, default value is 0. 0 mean only one attempt. 1 mean one attempt + 1 retry while error (total 2 attempts).

Working with huge rows

You can chunk results like in 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";
        }
    });

Buffer engine for insert queries See https://clickhouse.tech/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';
}

If you also want to read from your buffer table, put its name in $table

<?php

namespace App\Models\Clickhouse;

use Xtwoend\HyperfClickhouse\Model;

class MyTable extends Model
{
    protected $table = 'my_table_buffer';
}

OPTIMIZE Statement See https://clickhouse.com/docs/ru/sql-reference/statements/optimize/

MyTable::optimize($final = false, $partition = null);

Deletions

See https://clickhouse.com/docs/en/sql-reference/statements/alter/delete/

MyTable::where('field_one', 123)->delete();

Using buffer engine and performing OPTIMIZE or 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';
}

xtwoend/hyperf-clickhouse 适用场景与选型建议

xtwoend/hyperf-clickhouse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 448 次下载、GitHub Stars 达 9, 最近一次更新时间为 2022 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 xtwoend/hyperf-clickhouse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xtwoend/hyperf-clickhouse 我们能提供哪些服务?
定制开发 / 二次开发

基于 xtwoend/hyperf-clickhouse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 448
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-20