PHP code example of deflinhec / laravel-clickhouse

1. Go to this page and download the library: Download deflinhec/laravel-clickhouse 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/ */

    

deflinhec / laravel-clickhouse example snippets


use Deflinhec\LaravelClickHouse\Services\Service;

$clickHouse = new Service();

// Execute custom query
$result = $clickHouse->executeQuery('SELECT * FROM your_table LIMIT 10');

// Test connection
if ($clickHouse->testConnection()) {
    echo "Connection successful!";
}

use Deflinhec\LaravelClickHouse\Exceptions\ClickHouseException;

try {
    $result = $clickHouse->executeQuery('SELECT * FROM non_existent_table');
} catch (ClickHouseException $e) {
    echo "Error Type: " . $e->getErrorType();
    echo "Error Code: " . $e->getErrorCode();
    echo "Error Message: " . $e->getMessage();
    echo "Error Context: " . json_encode($e->getContext());
}

// Available error types
ClickHouseException::connectionError($message, $context);
ClickHouseException::queryError($message, $context);
ClickHouseException::configurationError($message, $context);
ClickHouseException::migrationError($message, $context);
ClickHouseException::clusterError($message, $context);
ClickHouseException::authenticationError($message, $context);
ClickHouseException::permissionError($message, $context);
ClickHouseException::timeoutError($message, $context);
ClickHouseException::syntaxError($message, $context);
ClickHouseException::resourceError($message, $context);



use Deflinhec\LaravelClickHouse\Database\Migration;

class CreateExampleTable extends Migration
{
    public function up()
    {
        return <<<SQL
            CREATE TABLE IF NOT EXISTS example_table (
                id UInt32,
                name String,
                value Float64,
                is_active UInt8 DEFAULT 1,
                tags Array(String),
                metadata String,
                created_at DateTime DEFAULT now(),
                updated_at DateTime DEFAULT now()
            ) ENGINE = MergeTree()
            ORDER BY (id, created_at)
        SQL;
    }

    public function down()
    {
        return <<<SQL
            DROP TABLE IF EXISTS example_table
        SQL;
    }
}

'connections' => [
    'default' => [
        'host' => env('CLICKHOUSE_HOST', 'localhost'),
        'port' => env('CLICKHOUSE_PORT', '8123'),
        'username' => env('CLICKHOUSE_USERNAME', 'default'),
        'password' => env('CLICKHOUSE_PASSWORD', ''),
        'database' => env('CLICKHOUSE_DATABASE', 'default'),
        'options' => [
            'timeout' => env('CLICKHOUSE_TIMEOUT', 30),
            'ssl' => env('CLICKHOUSE_SSL', false),
            'verify' => env('CLICKHOUSE_VERIFY', false),
        ],
    ],
    'cluster' => [
        'mode' => env('CLICKHOUSE_CLUSTER_MODE', 'round_robin'),
        'nodes' => [
            'host' => explode(',', env('CLICKHOUSE_CLUSTER_NODES', 'node1,node2')),
            'port' => explode(',', env('CLICKHOUSE_CLUSTER_PORTS', '8123,8123')),
            'weight' => explode(',', env('CLICKHOUSE_CLUSTER_WEIGHTS', '1,1')),
            'username' => env('CLICKHOUSE_USERNAME', 'default'),    
            'password' => env('CLICKHOUSE_PASSWORD', 'clickhouse'),
            'database' => env('CLICKHOUSE_DATABASE', 'default'),
            'options' => [
                'timeout' => env('CLICKHOUSE_TIMEOUT', 30),
                'ssl' => env('CLICKHOUSE_SSL', false),
                'readonly' => env('CLICKHOUSE_READONLY', true),
            ],
        ],
        'options' => [
            'retry_attempts' => env('CLICKHOUSE_CLUSTER_RETRY_ATTEMPTS', 3),
            'retry_delay' => env('CLICKHOUSE_CLUSTER_RETRY_DELAY', 1000), // milliseconds
            'health_check_interval' => env('CLICKHOUSE_CLUSTER_HEALTH_CHECK_INTERVAL', 30), // seconds
            'failover_timeout' => env('CLICKHOUSE_CLUSTER_FAILOVER_TIMEOUT', 5000), // milliseconds
        ],
    ],
],
bash
php artisan vendor:publish --tag=clickhouse-config
bash
# Open ClickHouse interactive CLI
php artisan clickhouse
bash
# Open interactive CLI (xecute single query
php artisan clickhouse --query="SELECT COUNT(*) FROM your_table"

# Specify connection
php artisan clickhouse --connection=local