예제 #1
0
//  класс userevent
include_once 'article_01_userevent.php';
// Конфигурация
$config = ['host' => '192.168.1.20', 'port' => '8123', 'username' => 'default', 'password' => ''];
$client = new \ClickHouseDB\Client($config);
//$client->write('DROP TABLE IF EXISTS articles.events');
if (!$client->isExists('articles', 'events')) {
    $client->write('DROP TABLE IF EXISTS articles.events');
    $client->write('CREATE DATABASE IF NOT EXISTS articles');
    $client->write("\n    CREATE TABLE articles.events (\n        event_date Date DEFAULT toDate(event_time),\n        event_time DateTime,\n        event_type Enum8('VIEWS' = 1, 'CLICKS' = 2),\n        site_id Int32,\n        article_id Int32,\n        ip String,\n        city String,\n        user_uuid String,\n        referer String,\n        utm String DEFAULT extractURLParameter(referer, 'utm_campaign')\n    ) ENGINE = MergeTree(event_date, (site_id,event_type, article_id), 8192)\n");
    // ---------------------------- создадим тестовый набор данных ---------------
    $userEvent = new UserEvent();
    @unlink($fileName);
    echo "Write data to : " . $fileName . "\n\n";
    for ($z = 0; $z < $count_rows; $z++) {
        $row = ['event_date' => $userEvent->getDate(), 'event_time' => $userEvent->getTime(), 'event_type' => $userEvent->getType(), 'site_id' => $userEvent->getSiteId(), 'article_id' => $userEvent->getArticleId(), 'ip' => $userEvent->getIp(), 'city' => $userEvent->getCity(), 'user_uuid' => $userEvent->getUserUuid(), 'referer' => $userEvent->getReferer(), 'utm' => $userEvent->getUtm()];
        file_put_contents($fileName, \ClickHouseDB\FormatLine::TSV($row) . "\n", FILE_APPEND);
        if ($z % 100 == 0) {
            echo "{$z}\r";
        }
    }
    // Включаем сжатие
    $client->setTimeout(300);
    $client->database('articles');
    $client->enableHttpCompression(true);
    echo "\n> insertBatchFiles....\n";
    $result_insert = $client->insertBatchTSVFiles('events', [$fileName], ['event_date', 'event_time', 'event_type', 'site_id', 'article_id', 'ip', 'city', 'user_uuid', 'referer', 'utm']);
    echo "insert done\n";
    echo $fileName . " : " . $result_insert[$fileName]->totalTimeRequest() . "\n";
}
$client->database('articles');