You need to enable JavaScript to run this app.
导航
ClickHouse PHP Driver
最近更新时间:2024.08.07 11:12:01首次发布时间:2023.08.17 18:58:10

本文介绍如何在 PHP 开发环境连接并访问 ByteHouse 云数仓。
ByteHouse 兼容下列开源 ClickHouse PHP Driver 程序:

  • HTTP 方式连接:
    • smi2/phpClickHouse ( 本文已在程序 1.5.1/1.4.6 版本下验证)
  • TCP 方式连接:
    • lizhichao/one-ck( 本文已在程序 0.29 版本下验证)

前提条件

访问 ByteHouse 所需的连接信息,请参考 获取连接信息 获取。

通过 HTTP 方式连接

本章节介绍通过 smi2/phpClickHouse 程序连接 ByteHouse 的基本用法,您可以在程序 Github 主页 或者 Packagist 获取最新的文档和发布版本信息。

说明

smi2/phpClickHouse 程序 1.4.6 及以上版本,需要 php 7.3 及以上版本的支持。

程序安装

composer require smi2/phpclickhouse

连接到 ByteHouse

可参考下面代码样例,注意根据前提条件中获取的信息填写$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"); 

通过 TCP 方式连接

本章节介绍通过 lizhichao/one-ck 程序连接 ByteHouse 的基本用法,您可以在程序 Github 主页 获取最新的文档和发布版本信息。

说明

lizhichao/one-ck 程序 0.2.x 版本需要 php 7.2 及以上版本的支持。

程序安装

composer require lizhichao/one-ck

安装完成后,在php代码中插入以下内容。

use OneCk\Client;

连接到 ByteHouse

可参考下面代码样例,注意根据前提条件中获取的信息填写$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);