在開發需要與 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 絕對是一個值得考慮的選擇。它將大大簡化你的開發工作,并提高你的應用程序的性能。