在开发需要与 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 Tinderbox\Clickhouse\Server; use Tinderbox\Clickhouse\ServerProvider; use Tinderbox\Clickhouse\Client; $server = new Server('127.0.0.1', '8123', 'default', 'user', 'pass'); $serverProvider = (new ServerProvider())->addServer($server); $client = new Client($serverProvider);
use Tinderbox\Clickhouse\Cluster; use Tinderbox\Clickhouse\Server; use Tinderbox\Clickhouse\ServerProvider; use Tinderbox\Clickhouse\Client; $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 Tinderbox\Clickhouse\Common\File('/file-1.csv'), new Tinderbox\Clickhouse\Common\File('/file-2.csv') ]); $client->insertFiles('table', ['date', 'column'], [ new Tinderbox\Clickhouse\Common\File('/file-1.tsv'), new Tinderbox\Clickhouse\Common\File('/file-2.tsv') ], Tinderbox\Clickhouse\Common\Format::TSV);
5. 其他查询:
可以使用 statement() 方法执行其他类型的查询,例如 DROP TABLE。
$client->writeOne('DROP TABLE table');
通过使用 the-tinderbox/clickhouse-php-client,我能够轻松地连接到 ClickHouse 数据库,执行各种查询,并从本地文件导入数据。这极大地简化了我的开发流程,并提高了我的应用程序的性能。
总结:
the-tinderbox/clickhouse-php-client 是一个功能强大且易于使用的 PHP ClickHouse 客户端。它具有以下优点:
如果你正在开发需要与 ClickHouse 数据库交互的 PHP 应用,那么 the-tinderbox/clickhouse-php-client 绝对是一个值得考虑的选择。它将大大简化你的开发工作,并提高你的应用程序的性能。
以上就是使用the-tinderbox/clickhouse-php-client解决PHP应用与ClickHouse数据库交互问题的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号