bllem/dbal-clickhouse
Composer 安装命令:
composer require bllem/dbal-clickhouse
包简介
Doctrine DBAL driver for ClickHouse
关键字:
README 文档
README
Doctrine DBAL ClickHouse Driver
Doctrine DBAL driver for ClickHouse -- an open-source column-oriented database management system by Yandex (https://clickhouse.yandex/)
Driver is suitable for Symfony or any other framework using Doctrine.
Installation
composer require friendsofdoctrine/dbal-clickhouse
Initialization
Custom PHP script
$connectionParams = [ 'host' => 'localhost', 'port' => 8123, 'user' => 'default', 'password' => '', 'dbname' => 'default', 'driverClass' => 'FOD\DBALClickHouse\Driver', 'wrapperClass' => 'FOD\DBALClickHouse\Connection', 'driverOptions' => [ 'extremes' => false, 'readonly' => true, 'max_execution_time' => 30, 'enable_http_compression' => 0, 'https' => false, ], ]; $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, new \Doctrine\DBAL\Configuration());
driverOptions are special smi2/phpclickhouse client settings
Symfony
configure...
# app/config/config.yml doctrine: dbal: connections: clickhouse: host: localhost port: 8123 user: default password: "" dbname: default driver_class: FOD\DBALClickHouse\Driver wrapper_class: FOD\DBALClickHouse\Connection options: enable_http_compression: 1 max_execution_time: 60 #mysql: # ...
...and get from the service container
$conn = $this->get('doctrine.dbal.clickhouse_connection');
Usage
Create new table
// ***quick start*** $fromSchema = $conn->getSchemaManager()->createSchema(); $toSchema = clone $fromSchema; // create new table object $newTable = $toSchema->createTable('new_table'); // add columns $newTable->addColumn('id', 'integer', ['unsigned' => true]); $newTable->addColumn('payload', 'string', ['notnull' => false]); // *option 'notnull' in false mode allows you to insert NULL into the column; // in this case, the column will be represented in the ClickHouse as Nullable(String) $newTable->addColumn('hash', 'string', ['length' => 32, 'fixed' => true]); // *option 'fixed' sets the fixed length of a string column as specified; // if specified, the type of the column is FixedString //set primary key $newTable->setPrimaryKey(['id']); // execute migration SQLs to create table in ClickHouse $sqlArray = $fromSchema->getMigrateToSql($toSchema, $conn->getDatabasePlatform()); foreach ($sqlArray as $sql) { $conn->exec($sql); }
// ***more options (optional)*** //specify table engine $newTable->addOption('engine', 'MergeTree'); // *if not specified -- default engine 'ReplacingMergeTree' will be used // add Date column for partitioning $newTable->addColumn('event_date', 'date', ['default' => 'toDate(now())']); $newTable->addOption('eventDateColumn', 'event_date'); // *if not specified -- default Date column named EventDate will be added $newTable->addOption('eventDateProviderColumn', 'updated_at'); // *if specified -- event date column will be added with default value toDate(updated_at); // if the type of the provider column is `string`, the valid format of provider column values must be either `YYYY-MM-DD` or `YYYY-MM-DD hh:mm:ss` // if the type of provider column is neither `string`, nor `date`, nor `datetime`, provider column values must contain a valid UNIX Timestamp $newTable->addOption('samplingExpression', 'intHash32(id)'); // samplingExpression -- a tuple that defines the table's primary key, and the index granularity //specify index granularity $newTable->addOption('indexGranularity', 4096); // *if not specified -- default value 8192 will be used
Insert
// 1 $conn->exec("INSERT INTO new_table (id, payload) VALUES (1, 'dummyPayload1')");
// 2 $conn->insert('new_table', ['id' => 2, 'payload' => 'dummyPayload2']); // INSERT INTO new_table (id, payload) VALUES (?, ?) [2, 'dummyPayload2']
// 3 via QueryBuilder $qb = $conn->createQueryBuilder(); $qb ->insert('new_table') ->setValue('id', ':id') ->setValue('payload', ':payload') ->setParameter('id', 3, \PDO::PARAM_INT) // need to explicitly set param type to `integer`, because default type is `string` and ClickHouse doesn't like types mismatchings ->setParameter('payload', 'dummyPayload3'); $qb->execute();
Select
echo $conn->fetchColumn('SELECT SUM(views) FROM articles');
Select via Dynamic Parameters and Prepared Statements
$stmt = $conn->prepare('SELECT authorId, SUM(views) AS total_views FROM articles WHERE category_id = :categoryId AND publish_date = :publishDate GROUP BY authorId'); $stmt->bindValue('categoryId', 123); $stmt->bindValue('publishDate', new \DateTime('2017-02-29'), 'datetime'); $stmt->execute(); while ($row = $stmt->fetch()) { echo $row['authorId'] . ': ' . $row['total_views'] . PHP_EOL; }
Additional types
If you want to use Array(T) type, register additional DBAL types in your code:
// register all custom DBAL Array types ArrayType::registerArrayTypes($conn->getDatabasePlatform()); // register one custom DBAL Array(Int8) type Type::addType('array(int8)', 'FOD\DBALClickHouse\Types\ArrayInt8Type');
or register them in Symfony configuration file:
# app/config/config.yml doctrine: dbal: connections: ... types: array(int8): FOD\DBALClickHouse\Types\ArrayInt8Type array(int16): FOD\DBALClickHouse\Types\ArrayInt16Type array(int32): FOD\DBALClickHouse\Types\ArrayInt32Type array(int64): FOD\DBALClickHouse\Types\ArrayInt64Type array(uint8): FOD\DBALClickHouse\Types\ArrayUInt8Type array(uint16): FOD\DBALClickHouse\Types\ArrayUInt16Type array(uint32): FOD\DBALClickHouse\Types\ArrayUInt32Type array(uint64): FOD\DBALClickHouse\Types\ArrayUInt64Type array(float32): FOD\DBALClickHouse\Types\ArrayFloat32Type array(float64): FOD\DBALClickHouse\Types\ArrayFloat64Type array(string): FOD\DBALClickHouse\Types\ArrayStringType array(datetime): FOD\DBALClickHouse\Types\ArrayDateTimeType array(date): FOD\DBALClickHouse\Types\ArrayDateType
If you want to use numeric types, register additional DBAL types in your Symfony configuration file:
# app/config/config.yml doctrine: dbal: connections: ... types: int8: FOD\DBALClickHouse\Types\Int8Type int16: FOD\DBALClickHouse\Types\Int16Type int32: FOD\DBALClickHouse\Types\Int32Type int64: FOD\DBALClickHouse\Types\Int64Type float32: FOD\DBALClickHouse\Types\Float32Type float64: FOD\DBALClickHouse\Types\Float64Type decimal: FOD\DBALClickHouse\Types\DecimalType
or you can override DBAL type in your code:
Type::overrideType(Type::BIGINT, 'FOD\DBALClickHouse\Types\Int64Type'); Type::overrideType(Type::DECIMAL, 'FOD\DBALClickHouse\Types\DecimalType');
More information in Doctrine DBAL documentation:
bllem/dbal-clickhouse 适用场景与选型建议
bllem/dbal-clickhouse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.84k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dbal」 「doctrine」 「driver」 「clickhouse」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bllem/dbal-clickhouse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bllem/dbal-clickhouse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bllem/dbal-clickhouse 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
Galera cluster driver for Doctrine
A PHP client driver for the RethinkDB query language (ReQL)
Make pimcore migration simple
Arikam CMS Driver package
统计信息
- 总下载量: 1.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-10