horr1f1k / kak-clickhouse
ClickHouse for Yii2
Installs: 179
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 15
Type:yii2-extension
Requires
- yiisoft/yii2: *
- yiisoft/yii2-httpclient: ^2.0.0.1
This package is not auto-updated.
Last update: 2018-04-14 23:44:56 UTC
README
Installation
Composer
The preferred way to install this extension is through Composer.
Either run
php composer.phar require horr1f1k/kak-clickhouse "dev-master"
or add
"horr1f1k/kak-clickhouse": "dev-master"
to the require section of your composer.json
Configuration example
'components' => [ 'clickhouse' => [ 'class' => 'kak\clickhouse\Connection', 'dsn' => '127.0.0.1', 'port' => '8123', // 'database' => 'default', // use other database name 'username' => 'web', 'password' => '123', ], // ...
Usage
/** @var \kak\clickhouse\Connection $client */ $client = \Yii::$app->clickhouse; $sql = 'select * from stat where counter_id=:counter_id'; $client->createCommand($sql,[ ':counter_id' => 122 ])->queryAll(); // insert data ORM $client->createCommand(null) ->insert('stat', [ 'event_data' => date('Y-m-d'), 'counter_id' => 122 ]) ->execute();
Save custom model
use yii\base\Model; class Stat extends Model { public $event_date; // Date; public $counter_id = 0; // Int32, public function save($validate = true) { /** @var \kak\clickhouse\Connection $client */ $client = \Yii::$app->clickhouse; $this->event_date = date('Y-m-d'); if($validate && !$this->validate()){ return false; } $attributes = $this->getAttributes(); $client->createCommand(null) ->insert('stat', $attributes ) ->execute(); } }
ActiveRecord model
class Stat extends \kak\clickhouse\ActiveRecord { public static function tableName() { return 'stat'; } // use relation in mysql (Only with, do not use joinWith) public function getUser() { return $this->hasOne(User::className(),['id' => 'user_id']); } }
Using Gii generator
return [ //.... 'modules' => [ // ... 'gii' => [ 'class' => 'yii\gii\Module', 'generators' => [ 'clickhouseDbModel' => [ 'class' => 'kak\clickhouse\gii\model\Generator' ] ], ], ] ];
ClickHouse Reference Manual
https://clickhouse.yandex/reference_en.html
Summary of recommendations insert data
1 Accumulated data and insert at one time, it will reduce the operations io disk 2 @todo how that will add...