smi2/phpclickhouse
PHP client for ClickHouse with an easy, fluent API. Supports queries and inserts, result sets, bindings, and connection configuration for fast analytics workflows. Suitable for Laravel and standalone PHP apps needing reliable ClickHouse access.
$config = [
'host' => 'cluster.clickhouse.dns.com', // any node name in cluster
'port' => '8123',
'username' => 'default', // all nodes share login/password
'password' => '',
];
// Client connects to first node by DNS, reads IP list, then connects to ALL nodes for health check
$cl = new ClickHouseDB\Cluster($config);
$cl->setScanTimeOut(2.5); // 2500 ms max per node
if (!$cl->isReplicasIsOk()) {
throw new Exception('Replica state is bad, error=' . $cl->getError());
}
// All nodes and clusters
print_r($cl->getNodes());
print_r($cl->getClusterList());
// Nodes by cluster name
$name = 'some_cluster_name';
print_r($cl->getClusterNodes($name));
// Counts
echo "Count Shard = " . $cl->getClusterCountShard($name) . "\n";
echo "Count Replica = " . $cl->getClusterCountReplica($name) . "\n";
// Get nodes by table & print size per node
$nodes = $cl->getNodesByTable('shara.adpreview_body_views_sharded');
foreach ($nodes as $node) {
echo "$node >\n";
print_r($cl->client($node)->tableSize('adpreview_body_views_sharded'));
print_r($cl->client($node)->showCreateTable('shara.adpreview_body_views'));
}
// Select by IP pattern, delimiter ";"
// First tries .298, then .964, falls back to first node
$cli = $cl->clientLike($name, '.298;.964');
$cli->ping();
// Truncate table across cluster
$result = $cl->truncateTable('dbName.TableName_sharded');
// Random active node
$cl->activeClient()->setTimeout(500);
$cl->activeClient()->write("DROP TABLE IF EXISTS default.asdasdasd ON CLUSTER cluster2");
// Find leader node
$cl->getMasterNodeForTable('dbName.TableName_sharded');
var_dump($cl->getError());
How can I help you explore Laravel packages today?