在开发需要与 ClickHouse 数据库交互的 PHP 应用时,我面临着一个挑战:如何高效、可靠地与 ClickHouse 进行数据交互。官方的 PHP 扩展虽然存在,但在配置和使用上相对繁琐。我需要一个更轻量级、更易于使用的解决方案。
这时,我发现了 the-tinderbox/clickhouse–php-client 这个 composer 包。它是一个专门为 clickhouse 设计的 php 客户端,通过 http 协议进行通信,并使用 guzzle http 客户端发送请求。这使得它非常灵活,易于集成到现有的 php 项目中。
首先,你需要使用 Composer 安装这个包:
composer require the-tinderbox/clickhouse-php-client
安装完成后,就可以开始使用了。以下是一些基本的使用示例:
1. 连接到 ClickHouse 服务器:
the-tinderbox/clickhouse-php-client 支持连接到单个 ClickHouse 服务器,也支持连接到 ClickHouse 集群。
立即学习“PHP免费学习笔记(深入)”;
- 连接到单个服务器:
use TinderboxClickhouseServer; use TinderboxClickhouseServerProvider; use TinderboxClickhouseClient; $server = new Server('127.0.0.1', '8123', 'default', 'user', 'pass'); $serverProvider = (new ServerProvider())->addServer($server); $client = new Client($serverProvider);
- 连接到 ClickHouse 集群:
use TinderboxClickhouseCluster; use TinderboxClickhouseServer; use TinderboxClickhouseServerProvider; use TinderboxClickhouseClient; $testCluster = new Cluster('cluster-name', [ 'server-1' => [ 'host' => '127.0.0.1', 'port' => '8123', 'database' => 'default', 'user' => 'user', 'password' => 'pass' ], 'server-2' => new Server('127.0.0.1', '8124', 'default', 'user', 'pass') ]); $serverProvider = (new ServerProvider())->addCluster($testCluster); $client = new Client($serverProvider);
2. 执行 SELECT 查询:
the-tinderbox/clickhouse-php-client 提供了 readOne() 和 read() 方法来执行 SELECT 查询。readOne() 方法用于执行单个查询,并返回一个 Result 对象。read() 方法用于执行多个查询,并返回一个包含多个 Result 对象的数组。
- 同步查询:
$result = $client->readOne('select number from system.numbers limit 100'); foreach ($result as $number) { echo $number['number'].PHP_EOL; }
- 异步查询:
list($clicks, $visits, $views) = $client->read([ ['query' => "select * from clicks where date = '2017-01-01'"], ['query' => "select * from visits where date = '2017-01-01'"], ['query' => "select * from views where date = '2017-01-01'"], ]); foreach ($clicks as $click) { echo $click['date'].PHP_EOL; }
3. 执行 INSERT 查询:
the-tinderbox/clickhouse-php-client 提供了 writeOne() 和 write() 方法来执行 INSERT 查询。
- 插入数据:
$client->writeOne("insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)");
4. 从本地文件导入数据:
the-tinderbox/clickhouse-php-client 允许你从本地 CSV 或 TSV 文件导入数据到 ClickHouse。
$client->writeFiles('table', ['date', 'column'], [ new TinderboxClickhouseCommonFile('/file-1.csv'), new TinderboxClickhouseCommonFile('/file-2.csv') ]); $client->insertFiles('table', ['date', 'column'], [ new TinderboxClickhouseCommonFile('/file-1.tsv'), new TinderboxClickhouseCommonFile('/file-2.tsv') ], TinderboxClickhouseCommonFormat::TSV);
5. 其他查询:
可以使用 statement() 方法执行其他类型的查询,例如 DROP TABLE。
$client->writeOne('DROP TABLE table');
通过使用 the-tinderbox/clickhouse-php-client,我能够轻松地连接到 ClickHouse 数据库,执行各种查询,并从本地文件导入数据。这极大地简化了我的开发流程,并提高了我的应用程序的性能。
总结:
the-tinderbox/clickhouse-php-client 是一个功能强大且易于使用的 PHP ClickHouse 客户端。它具有以下优点:
- 简单易用: 基于 HTTP 协议,易于配置和使用。
- 支持同步和异步查询: 可以根据需要选择合适的查询方式。
- 支持集群操作: 方便连接和操作 ClickHouse 集群。
- 支持本地文件导入: 可以高效地从本地文件导入数据。
- 轻量级: 不依赖于复杂的 PHP 扩展。
如果你正在开发需要与 ClickHouse 数据库交互的 PHP 应用,那么 the-tinderbox/clickhouse-php-client 绝对是一个值得考虑的选择。它将大大简化你的开发工作,并提高你的应用程序的性能。
评论(已关闭)
评论已关闭