/** * @expectedException \ClickHouseDB\QueryException * @expectedExceptionCode 6 */ public function testExceptionConnects() { $config = ['host' => 'x', 'port' => '8123', 'username' => 'x', 'password' => 'x', 'settings' => ['max_execution_time' => 100]]; $db = new ClickHouseDB\Client($config); $db->ping(); }
<?php // Подключаем драйвер include_once __DIR__ . '/../include.php'; // Конфигурация $config = ['host' => '192.168.1.20', 'port' => '8123', 'username' => 'default', 'password' => '']; $client = new \ClickHouseDB\Client($config); // Проверяем соединение с базой $client->ping(); // Создаём таблицу $client->write('CREATE DATABASE IF NOT EXISTS articles'); $client->write('DROP TABLE IF EXISTS articles.events'); $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"); // Выбираем default базу $client->database('articles'); // Получим список таблиц print_r($client->showTables()); // Для упрощения выставляем принудительно таймзону date_default_timezone_set('Europe/Moscow'); // Простая вставка данных `$db->insert(имя_таблицы, [данные], [колонки]);` $client->insert('events', [[time(), 'CLICKS', 1, 1234, '192.168.1.11', 'Moscow', 'user_11', ''], [time(), 'CLICKS', 1, 1235, '192.168.1.11', 'Moscow', 'user_11', 'http://yandex.ru?utm_campaign=abc'], [time(), 'CLICKS', 1, 1236, '192.168.1.11', 'Moscow', 'user_11', 'http://smi2.ru?utm_campaign=abc'], [time(), 'CLICKS', 1, 1237, '192.168.1.11', 'Moscow', 'user_11', ''], [time(), 'CLICKS', 1, 1237, '192.168.1.13', 'Moscow', 'user_13', ''], [time(), 'CLICKS', 1, 1237, '192.168.1.14', 'Moscow', 'user_14', ''], [time(), 'VIEWS', 1, 1237, '192.168.1.11', 'Moscow', 'user_11', ''], [time(), 'VIEWS', 1, 1237, '192.168.1.12', 'Moscow', 'user_12', ''], [time(), 'VIEWS', 1, 1237, '192.168.1.1', 'Rwanda', 'user_55', 'http://smi2.ru?utm_campaign=abc'], [time(), 'VIEWS', 1, 1237, '192.168.1.1', 'Banaadir', 'user_54', 'http://smi2.ru?utm_campaign=abc'], [time(), 'VIEWS', 1, 1237, '192.168.1.1', 'Tobruk', 'user_32', 'http://smi2.ru?utm_campaign=CM1'], [time(), 'VIEWS', 1, 1237, '192.168.1.1', 'Gisborne', 'user_12', 'http://smi2.ru?utm_campaign=CM1'], [time(), 'VIEWS', 1, 1237, '192.168.1.1', 'Moscow', 'user_43', 'http://smi2.ru?utm_campaign=CM3']], ['event_time', 'event_type', 'site_id', 'article_id', 'ip', 'city', 'user_uuid', 'referer']); // Достанем результат вставки данных print_r($client->select('SELECT * FROM events')->rows()); // Допустим нам нужно посчитать сколько уникальных пользователей просмотрело за сутки print_r($client->select(' SELECT event_date, uniqCombined(user_uuid) as count_users FROM events WHERE
$config = ['host' => 'x', 'port' => '8123', 'username' => 'x', 'password' => 'x']; $db = new ClickHouseDB\Client($config); try { $db->ping(); } catch (ClickHouseDB\QueryException $E) { echo "ERROR:" . $E->getMessage() . "\nOK\n"; } // ------------------ $config = ['host' => '192.168.1.20', 'port' => '8123', 'username' => 'x', 'password' => 'x']; $db = new ClickHouseDB\Client($config); try { $db->ping(); } catch (ClickHouseDB\QueryException $E) { echo "ERROR:" . $E->getMessage() . "\nOK\n"; } // ------------------ $config = ['host' => '192.168.1.20', 'port' => '8123', 'username' => 'default', 'password' => '']; $db = new ClickHouseDB\Client($config); try { $db->ping(); echo "PING : OK!\n"; } catch (ClickHouseDB\QueryException $E) { echo "ERROR:" . $E->getMessage() . "\nOK\n"; } try { $db->select("SELECT xxx as PPPP FROM ZZZZZ ")->rows(); } catch (ClickHouseDB\DatabaseException $E) { echo "ERROR : DatabaseException : " . $E->getMessage() . "\n"; // Table default.ZZZZZ doesn't exist. } // ----------------------------