horr1f1k / kak-clickhouse
此包已被废弃且不再维护。未建议替代包。
为 Yii2 提供的 ClickHouse
dev-master
2017-01-27 04:33 UTC
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
安装
Composer
通过 Composer 安装此扩展是首选方式。
运行以下命令之一
php composer.phar require horr1f1k/kak-clickhouse "dev-master"
或添加以下内容到您的 composer.json 的 require 部分
"horr1f1k/kak-clickhouse": "dev-master"
配置示例
'components' => [ 'clickhouse' => [ 'class' => 'kak\clickhouse\Connection', 'dsn' => '127.0.0.1', 'port' => '8123', // 'database' => 'default', // use other database name 'username' => 'web', 'password' => '123', ], // ...
用法
/** @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();
保存自定义模型
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 模型
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']); } }
使用 Gii 生成器
return [ //.... 'modules' => [ // ... 'gii' => [ 'class' => 'yii\gii\Module', 'generators' => [ 'clickhouseDbModel' => [ 'class' => 'kak\clickhouse\gii\model\Generator' ] ], ], ] ];
ClickHouse 参考手册
https://clickhouse.yandex/reference_en.html
插入数据建议总结
1 累积数据一次插入,将减少磁盘 I/O 操作 2 @todo 如何添加...