PHP code example of tangwei / hyperf-clickhouse

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

    

tangwei / hyperf-clickhouse example snippets


    'clickhouse' => [
        'driver' => 'clickhouse',
        'host' => env('CLICKHOUSE_HOST'),
        'port' => env('CLICKHOUSE_PORT','8123'),
        'database' => env('CLICKHOUSE_DATABASE','default'),
        'username' => env('CLICKHOUSE_USERNAME','default'),
        'password' => env('CLICKHOUSE_PASSWORD',''),
        'https' => (bool)env('CLICKHOUSE_HTTPS',false),
        'settings' => [ // optional
            // 'max_partitions_per_insert_block' => 300,
        ],
        'pool' => [
            'min_connections' => 1,
            'max_connections' => 3,
            'connect_timeout' => 10.0,
            'wait_timeout'    => 3.0,
            'heartbeat'       => -1,
            'max_idle_time'   => 60,
        ],
    ],


$client = \Tang\HyperfClickhouse\DB::query()->getClient();
$statement = $client->select('SELECT * FROM summing_url_views LIMIT 2');




namespace App\Models\Clickhouse;

use Tang\HyperfClickhouse\Model;

class MyTable extends Model
{
    // Not necessary. Can be obtained from class name MyTable => my_table
    protected string $table = 'my_table';

}




class CreateMyTable extends \Tang\HyperfClickhouse\Migration
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
        static::write('
            CREATE TABLE my_table (
                id UInt32,
                created_at DateTime,
                field_one String,
                field_two Int32
            )
            ENGINE = MergeTree()
            ORDER BY (id)
        ');
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        static::write('DROP TABLE my_table');
    }
}

$model = MyTable::create(['field_one' => 'model 1', 'field_two' => 1]);
# or
$model = MyTable::make(['field_one' => 'model 2']);
$model->field_two = 2;
$model->save();
# or
$model = new MyTable();
$model->fill(['field_one' => 'model 3', 'field_two' => 3])->save();

MyTable::query()->insert(['field_one' => 'model 11','field_two' => 11]);

$rows = MyTable::query()->select(['field_one', \Tinderbox\ClickhouseBuilder\raw('sum(field_two)', 'field_two_sum')])
    ->where('created_at', '>', '2020-09-14 12:47:29')
    ->groupBy('field_one')
    ->getCollect();

// Split the result into chunks of 30 rows 
$rows = MyTable::query()->select(['field_one', 'field_two'])
    ->chunk(30, function ($rows) {
        foreach ($rows as $row) {
            echo $row['field_two'] . "\n";
        }
    });



namespace App\Models\Clickhouse;

use Tang\HyperfClickhouse\Model;

class MyTable extends Model
{
    // Not necessary. Can be obtained from class name MyTable => my_table
    protected $table = 'my_table';
    // All inserts will be in the table $tableForInserts 
    // But all selects will be from $table
    protected $tableForInserts = 'my_table_buffer';
}



namespace App\Models\Clickhouse;

use Tang\HyperfClickhouse\Model;

class MyTable extends Model
{
    protected $table = 'my_table_buffer';
}

MyTable::query()->optimize($final = false, $partition = null);

MyTable::query()->where('field_one', 123)->delete();



namespace App\Models\Clickhouse;

use Tang\HyperfClickhouse\Model;

class MyTable extends Model
{
    // All SELECT's and INSERT's on $table
    protected $table = 'my_table_buffer';
    // OPTIMIZE and DELETE on $tableSources
    protected $tableSources = 'my_table';
}