本文介绍如何在 PHP 开发环境连接并访问 ByteHouse 云数仓。
ByteHouse 兼容下列开源 ClickHouse PHP Driver 程序:
访问 ByteHouse 所需的连接信息,请参考 获取连接信息 获取。
本章节介绍通过 smi2/phpClickHouse 程序连接 ByteHouse 的基本用法,您可以在程序 Github 主页 或者 Packagist 获取最新的文档和发布版本信息。
说明
smi2/phpClickHouse 程序 1.4.6 及以上版本,需要 php 7.3 及以上版本的支持。
composer require smi2/phpclickhouse
可参考下面代码样例,注意根据前提条件中获取的信息填写$config
字段。
$config = array( 'host' => '', // Use {HOST} 'port' => '8123', // Use {PORT} 'username' => 'bytehouse', // Default username 'password' => '', // Use {API_KEY} 'https' => true ); $settings = array( 'database' => '', // Set default datebase ); // Create ClickHouse client instance $client = new Client($config, $settings); try { // Connect to ClickHouse server echo "Connected to ByteHouse!\n"; // Example query $query = 'SELECT 1'; // Execute the query $result = $client->select($query); // Display results echo "Results:\n"; foreach ($result->rows() as $row) { print_r($row); // Print each row } } catch (\Exception $e) { echo "Connection failed: " . $e->getMessage . "\n"; }
<?php include_once __DIR__ . '/../include.php'; $config = array( 'host' => 'bytehouse-cn-beijing.volces.com', 'port' => '8123', 'username' => '', 'password' => '', 'https' => true ); $settings = array( 'database' => '', 'password' => '', // user api key 'user' => 'bytehouse', // fix value to be bytehouse ); $db = new ClickHouseDB\Client($config, $settings); $db->setTimeout(15); // 15 second , support only Int value $db->setConnectTimeOut(15); // 15 seconds // select stmt $selectStmt = $db->select('SELECT 25042003 from numbers(100)'); echo $selectStmt->count(); echo $selectStmt->countAll(); echo $selectStmt->fetchOne(); print_r($selectStmt->rows()); // create database $db->write("create database nmba_test100"); // create table $db->write("create table nmba_test100.anhtai ( `col1` String ) ENGINE=CnchMergeTree PRIMARY KEY col1 ORDER BY col1"); // insert into table $insertStmt = $db->insert('nmba_test100.anhtai', [['anhtai'], ['nmba']], ['col1']); // select stmt from table $selectStmt = $db->select('SELECT * from nmba_test100.anhtai'); echo $selectStmt->count(); echo $selectStmt->countAll(); echo $selectStmt->fetchOne(); print_r($selectStmt->rows()); // drop database $db->write("drop database nmba_test100");
本章节介绍通过 lizhichao/one-ck 程序连接 ByteHouse 的基本用法,您可以在程序 Github 主页 获取最新的文档和发布版本信息。
说明
lizhichao/one-ck 程序 0.2.x 版本需要 php 7.2 及以上版本的支持。
composer require lizhichao/one-ck
安装完成后,在php代码中插入以下内容。
use OneCk\Client;
可参考下面代码样例,注意根据前提条件中获取的信息填写$config
字段。
use OneCk\Client; $client = new Client( 'Host:Port', // Use {HOST}:{PORT} 'bytehouse', 'password', // Use {API_KEY} 'database', // Set default datebase [ 'connect_timeout' => 3, 'socket_timeout' => 30, 'tcp_nodelay' => true, 'persistent' => true, ] );
<?php require __DIR__ . '/vendor/autoload.php'; use OneCk\Client; use OneCk\Types; $t1 = microtime(true); $ck = new Client('tcp://{HOST}:{PORT}', 'bytehouse', '{API_KEY}', '{DATABASE_NAME}'); $data['server info'] = $ck->getServerInfo(); $data['drop table'] = $ck->query('DROP TABLE IF EXISTS t6'); $res = $ck->query("SELECT sipHash64(toString('1ace54')) AS result"); if($res[0]['result'] !== '9525649478782099197'){ throw new \Exception('test uint64 fail:' . __LINE__); } $data['set default vw'] = $ck->query('SET DEFAULT WAREHOUSE `lyndon`;'); echo "set default vw\n"; $data['create db'] = $ck->query('create database nmba_test1000'); echo "create db\n"; $data['create tb'] = $ck->query('create table nmba_test1000.anhtai ( `col1` String ) ENGINE=CnchMergeTree PRIMARY KEY col1 ORDER BY col1'); echo "create tb\n"; $data['insert'] = $ck->query(sprintf("insert into nmba_test1000.anhtai values('%s'),('%s')",'anhtai','nmba')); echo "insert\n"; $data['select'] = $ck->query("SELECT * from nmba_test1000.anhtai"); echo "select\n"; $data['drop db'] = $ck->query("DROP DATABASE nmba_test1000"); echo "drop db\n"; echo json_encode($data);