one23/laravel-clickhouse

Laravel Clickhouse Eloquent

dev-master 2024-06-28 12:30 UTC

This package is auto-updated.

Last update: 2024-09-28 12:58:49 UTC


README

安装

composer require one23/laravel-clickhouse

将提供者添加到 bootstrap/providers.php

return [
    //...
    One23\LaravelClickhouse\ClickhouseServiceProvider::class,
    //...
];

将连接添加到 config/database.php

return [
    'connections' => [
        //...
        'clickhouse' => [
            'driver' => 'clickhouse',
            'host' => env('CLICKHOUSE_HOST', 'localhost'),
            'port' => env('CLICKHOUSE_PORT', 8123),
            'database' => env('CLICKHOUSE_DATABASE'),
            'username' => env('CLICKHOUSE_USERNAME'),
            'password' => env('CLICKHOUSE_PASSWORD'),
            'options' => [
                'timeout' => 15,
                'protocol' => 'http',
            ],
        ],
        //...
    ]
];

创建模型。示例

use One23\LaravelClickhouse\Database\Eloquent\Model;

class StatisticDaily extends Model
{
    protected $table = 'mv_statistic_daily';

    protected $casts = [
        'date' => 'date',
        'link_id' => 'int',
        'cnt' => 'int',
        'uniq_ip' => 'int',
    ];

    protected $primaryKey = null;

    //
}

使用模型。示例

Clickhouse\StatisticDaily::query()
    ->where('date', '=', $dt)
    ->delete();

Clickhouse\StatisticDaily::query()
    ->where('link_id', '=', $OLink->getId())
    ->whereBetween('date', [
        $from->startOfDay()->toDateString(),
        $to->endOfDay()->toDateString(),
    ])
    ->groupBy('date')
    ->selectRaw(implode(', ', [
        '`date`',
        'SUM(`cnt`) AS `cnt`',
        'SUM(`cnt_uniq_ip`) AS `cnt_uniq_ip`',

        'SUM(`cnt_uniq`) AS `cnt_uniq`',
        'SUM(`cnt_mobile`) AS `cnt_mobile`',
        'AVG(`avg_latency`) AS `avg_latency`',
        'MIN(NULLIF(`min_latency`, 0)) AS `min_latency`',
        'MAX(`max_latency`) AS `max_latency`',
        'AVG(`quantile_latency`) AS `quantile_latency`',
    ]))
    ->get();

Clickhouse\StatisticDaily::query()
    ->whereIn('link_id', $linkIds)
    ->whereBetween('date', [
        $this->from
            ->toDateString(),
        $this->to
            ->toDateString(),
    ])
    ->selectRaw(implode(', ', [
        'SUM(`statistic_daily`.`cnt`) as `cnt`',
        'SUM(`statistic_daily`.`cnt_uniq_ip`) as `cnt_uniq_ip`',
    ]))
    ->first();

待办事项

  • 测试
  • 深入 WHERE 子句
  • 重写语法
  • ...

安全性

如果您发现任何安全相关的问题,请通过电子邮件 eugene@krivoruchko.info 向我们报告,而不是使用问题跟踪器。

许可证

MIT