This extension provides the ClickHouse integration for the Yii framework 2.0. Main features:
- SQL commands
- Query builder
- Schema builder
- Migrations
- Batch Insert
- Parallel insert from large CSV files
- Valid handling of UInt64 type in PHP
- Supports Decimals and Nullable fields
The preferred way to install this extension is through composer.
Either run
composer require bashkarev/clickhouse
To use this extension, simply add the following code in your application configuration:
return [
//....
'clickhouse' => [
'class' => 'bashkarev\clickhouse\Connection',
'dsn' => 'host=localhost;port=8123;database=default;connect_timeout_with_failover_ms=10',
'username' => 'default',
'password' => '',
],
];
Add the following to you application config to enable it (if you already have the debug module enabled, it is sufficient to just add the panels configuration):
// ...
'bootstrap' => ['debug'],
'modules' => [
'debug' => [
'class' => 'yii\\debug\\Module',
'panels' => [
'clickhouse' => [
'class' => 'bashkarev\clickhouse\debug\Panel',
// 'db' => 'clickhouse', // ClickHouse component ID, defaults to `db`. Uncomment and change this line, if you registered component with a different ID.
],
],
],
],
// ...
In order to enable this command you should adjust the configuration of your console application:
return [
// ...
'controllerMap' => [
'clickhouse-migrate' => 'bashkarev\clickhouse\console\controllers\MigrateController'
],
];
# creates a new migration named 'create_target'
yii clickhouse-migrate/create create_target
# applies ALL new migrations
yii clickhouse-migrate
# reverts the last applied migration
yii clickhouse-migrate/down
$client = \Yii::$app->clickhouse->getClient();
Files are uploaded in parallel.
$db = \Yii::$app->clickhouse;
$client = $db->getClient();
$results = $client->insertBatchFiles('table_name', ['file_with_data.csv']);
$state = $results['file_with_data.csv'];
$isSuccess = !$state->isError();
$uploadInfo = $state->responseInfo();
print_r($uploadInfo);