PHP code example of justontheroad / clickhouse-log

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

    

justontheroad / clickhouse-log example snippets


$client = new ClickHouseClient($clickHouseConfig['db']);
$client->setTable('base_log');
// 同步
$result = $client->select('`add_date` = \'{addDate}\'', ['addDate' => '2021-02-01']);
// 异步
$sqlMap = [];
$table  = 'base_log';
for ($f = 1; $f < 4; $f++) {
    $addDate    = '2021-02-0' . $f;
    $sqlMap[$f] = "SELECT `id` FROM {$table} WHERE `add_date` = '{$addDate}' LIMIT 0, 1";
}

$stats = $client->asyncSelect($sqlMap);

$stat   = $client->delete("`add_date` < '{add_date}'", ['add_date' => $addDate]);

$dbConfig        = $clickHouseConfig['db'];
$logConfig       = $clickHouseConfig['log'];
$logFieldRequire = [
    'level',
    'request_host',
    'request_uri',
    'message',
    'content',
    'add_date',
    'add_time',
    'server_ip'
];
$res             = '';
$serverIp        = @file_get_contents(SYS_PATH . 'runtime/server_ip.txt');

try {
    $client     = new ClickHouseClient($dbConfig);
    $client->setTable($dbConfig['table']);
    $elogReader = new ElogReader(new LogManager($logConfig));
    !empty($serverIp) && $elogReader->serverIp = $serverIp;
    $config     = [
        'logFieldRequire'  => $logFieldRequire,
        'clickHouseClient' => $client,
        'readLogCallable'  => [$elogReader, 'read']
    ];
    $logTo      = new LogToClickHouse($config);
    $logTo->transfer();
    $res = $this->response(200, []);
} catch (Exception $e) {
    $res = $this->response(500, ['exception' => $e->getMessage()]);
}