定制 hyvor/clickhouse-php 二次开发

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

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

hyvor/clickhouse-php

Composer 安装命令:

composer require hyvor/clickhouse-php

包简介

ClickhouseDB Client for PHP

README 文档

README

This is a minimal wrapper around the Clickhouse HTTP Interface for PHP. It supports sessions, select and inserts, and queries with parameters.

Installation

composer require hyvor/clickhouse-php

Connecting

<?php
use Hyvor\Clickhouse\Clickhouse;

$clickhouse = new Clickhouse(
    host: 'localhost',
    port: 8123,
    https: false,
    user: 'default',
    password: '',
    database: 'default',
);

HTTP Client

Starting from version 2.0, this library is HTTP client agnostic. By default, it uses php-http Discovery to find an HTTP client from the installed packages. You can also pass your own PSR-18 HTTP client.

$clickhouse = new Clickhouse(
    httpClient: new MyCustomPsr18HttpClient(),
    
    // and also
    httpRequestFactory: new MyCustomPsr17HttpRequestFactory(),
    httpStreamFactory: new MyCustomPsr17StreamFactory(),
);

Select

Selecting multiple rows

$results = $clickhouse->select(
    'SELECT * FROM users WHERE id < {id: UInt32}',
     ['id' => 10]
);

// get rows as arrays
$results->all(); // [[1, 'John'], [2, 'Jane']]

// get the first row
$results->first(); // [1, 'John']

// get the first column of the first row
// useful for aggregations like COUNT(*)
$results->value(); // 2

// loop through the rows
foreach ($results as $row) {
    // $row is an array
}

// properties
$results->rows; // int (same as $results->count())
$results->rowsBeforeLimitAtLeast; // null | int
$results->elapsedTimeSeconds; // float
$results->rowsRead; // int
$results->bytesRead; // int

Insert

Insert a single row

Use the insert method to insert a new row.

Arguments:

Argument 1: The table name Argument 2: Key-value pairs for the columns and values types Argument 3...: Rows to insert

$clickhouse->insert(
    'users',
    [
        'id' => 'UInt64',
        'name' => 'String',
        'age' => 'UInt8',
    ],
    [
        'id' => 1, 
        'name' => 'John', 
        'age' => 42
    ]
)

In SQL, this would be:

INSERT INTO users (id, name, age) VALUES ({id: Int64}, {name: String}, {age: Int64})

Insert multiple rows

To insert multiple rows, pass multiple arguments (arrays) at the end:

$clickhouse->insert(
    'users',
    [
        'id' => 'UInt64',
        'name' => 'String',
        'age' => 'UInt8',
    ],
    ['id' => 1, 'name' => 'John', 'age' => 42],
    ['id' => 2, 'name' => 'Jane', 'age' => 37],
    ['id' => 3, 'name' => 'Bob', 'age' => 21],
)

Insert Raw

Clickhouse is notably slow when inserting a large number of rows with parameters. In such cases, you can use the insertRaw method, which inserts rows without parameters. Be careful with SQL injection.

$clickhouse->insertRaw(
    'users',
    ['id', 'created_at', 'name', 'age'],
    [
        [1, '2021-01-01 00:00:00', 'John', 30],
        [2, '2021-01-02 00:00:00', 'Jane', 25],
        [3, '2021-01-03 00:00:00', 'Doe', 35],
    ],
    asyncInsert: true,
    waitForAsyncInsert: true,
)

This method sets async_insert and wait_for_async_insert settings true by default. Read more about these settings.

Other Queries

You can run any other query with query(). The response is returned as JSON in Clickhouse's JSONCompact format.

$clickhouse->query('DROP TABLE users');
// with params
$clickhouse->query('QUERY', ['param' => 1]);

Session

Each Hyvor\Clickhouse\Clickhouse object creates a new session ID. You can use this to share the session between multiple requests.

$clickhouse = new Clickhouse();

// example:
// by default, Clickhouse update mutations are async
// here, we set mutations to sync
$clickhouse->query('SET mutations_sync = 1');

// all queries in this session (using the same $clickhouse object) will be sync
$clickhouse->query(
    'ALTER TABLE users UPDATE name = {name: String} WHERE id = 1', 
    ['name' => 'John']
);

hyvor/clickhouse-php 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-25