PHP code example of arungowda325 / clickhouse-php-client

1. Go to this page and download the library: Download arungowda325/clickhouse-php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arungowda325 / clickhouse-php-client example snippets


$server = new Wolverine\ClickhouseClient\Server('127.0.0.1', '8123', 'default', 'user', 'pass');
$serverProvider = (new Wolverine\ClickhouseClient\ServerProvider())->addServer($server);

$client = new Wolverine\ClickhouseClient\Client($serverProvider);

$testCluster = new Wolverine\ClickhouseClient\Cluster('cluster-name', [
    'server-1' => [
        'host' => '127.0.0.1',
        'port' => '8123',
        'database' => 'default',
        'user' => 'user',
        'password' => 'pass'
    ],
    'server-2' => new Wolverine\ClickhouseClient\Server('127.0.0.1', '8124', 'default', 'user', 'pass')
]);

$anotherCluster = new Wolverine\ClickhouseClient\Cluster('cluster-name', [
    [
        'host' => '127.0.0.1',
        'port' => '8125',
        'database' => 'default',
        'user' => 'user',
        'password' => 'pass'
    ],
    new Wolverine\ClickhouseClient\Server('127.0.0.1', '8126', 'default', 'user', 'pass')
]);

$serverProvider = (new Wolverine\ClickhouseClient\ServerProvider())->addCluster($testCluster)->addCluster($anotherCluster);

$client = (new Wolverine\ClickhouseClient\Client($serverProvider));

$client->using('server-2')->select('select * from table');

$firstServerOptionsWithTag = (new \Wolverine\ClickhouseClient\Common\ServerOptions())->setTag('tag');
$secondServerOptionsWithAnotherTag = (new \Wolverine\ClickhouseClient\Common\ServerOptions())->setTag('another-tag');

$server = new Wolverine\ClickhouseClient\Server('127.0.0.1', '8123', 'default', 'user', 'pass', $firstServerOptionsWithTag);

$cluster = new Wolverine\ClickhouseClient\Cluster('cluster', [
    new Wolverine\ClickhouseClient\Server('127.0.0.2', '8123', 'default', 'user', 'pass', $secondServerOptionsWithAnotherTag)
]);

$serverProvider = (new Wolverine\ClickhouseClient\ServerProvider())->addServer($server)->addCluster($cluster);

$client = (new Wolverine\ClickhouseClient\Client($serverProvider));

$client->usingServerWithTag('tag');

$rows = $result->rows;
$rows = $result->getRows();

$statistic = $result->statistic;
$statistic = $result->getStatistic();

echo $statistic->rows;
echo $statistic->getRows();

echo $statistic->bytes;
echo $statistic->getBytes();

echo $statistic->time;
echo $statistic->getTime();

echo $statistic->rowsBeforeLimitAtLeast;
echo $statistic->getRowsBeforeLimitAtLeast();

$result = $client->readOne('select number from system.numbers limit 100');

foreach ($result as $number) {
    echo $number['number'].PHP_EOL;
}

['UInt64']

$result = $client->readOne('select number from system.numbers where number in _numbers limit 100', new TempTable('_numbers', 'numbers.csv', [
    'number' => 'UInt64'
]));

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;
}



list($clicks, $visits, $views) = $client->read([
    ['query' => "select * from clicks where date = '2017-01-01' and userId in _users", new TempTable('_users', 'users.csv', ['number' => 'UInt64'])],
    ['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;
}


$client->writeOne("insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)");
$client->write([
    ['query' => "insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)"],
    ['query' => "insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)"],
    ['query' => "insert into table (date, column) values ('2017-01-01',1), ('2017-01-02',2)"]
]);

$client->writeFiles('table', ['date', 'column'], [
    new Wolverine\ClickhouseClient\Common\File('/file-1.csv'),
    new Wolverine\ClickhouseClient\Common\File('/file-2.csv')
]);

$client->insertFiles('table', ['date', 'column'], [
    new Wolverine\ClickhouseClient\Common\File('/file-1.tsv'),
    new Wolverine\ClickhouseClient\Common\File('/file-2.tsv')
], Wolverine\ClickhouseClient\Common\Format::TSV);

$client->writeOne('DROP TABLE table');
bash
composer 

file-1.tsv
file-2.tsv