Git Product home page Git Product logo

clickhouse's Introduction

Extension ClickHouse for Yii 2

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

Build Status

Installation

The preferred way to install this extension is through composer.

Either run

composer require bashkarev/clickhouse

Configuration

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' => '',
    ],
];

All settings

Using DebugPanel

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.
                ],
            ],
        ],
    ],
    // ...

Using Migrations

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

Access to native SMI2 ClickHouse client

$client = \Yii::$app->clickhouse->getClient();

Insert csv files

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);

clickhouse's People

Contributors

arkhamvm avatar bashkarev avatar fgh151 avatar mysterydragon avatar sartor avatar tyron098 avatar zkelo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

clickhouse's Issues

Maual not in line with v2

the manual of v2 refers to batchInsertFiles() which does not seem to exist and throws an exception

Sockets vs. Curl

Which are the points to use "low-level" sockets instead of yii-httpclient for example?
Performance? Could you provide a link to some tests or articles?
Otherwise, it looks like the wheel reinventing :)

Exception 'yii\db\Exception' with message 'Specify engine type'

Hello. I make a migration:
use bashkarev\clickhouse\Migration;
class m230706_140057_create_target extends Migration
{
const TABLE_NAME = 'first_table';
public function up()
{
$this->createTable(
self::TABLE_NAME,
[
'id' => $this->primaryKey(),
'account' => $this->string(35),
'account_state' => $this->tinyInteger(),
]
);

}

...
run
yii clickhouse-migrate
and get error:

create table first_table ...Exception 'yii\db\Exception' with message 'Specify engine type'

Need sample options string

BatchInsert from files

Command::batchInsert is a standard feature, but ClickHouse can insert data from uploaded files via Http Transport for better performance.
It would be nice to have this functionality. A signature may be similar to this Command::batchInsertFiles($table, array $files)

Fix error in getClient()

Connection.php

change

    public function getClient(): Client
    {
        return $this->_client;
    } 

to

    public function getClient(): Client
    {
        $this->open();
        
        return $this->_client;
    } 

Migration Table column date as 1970 year alwas

Table structure:

'version' => 'String',
 'date' => 'Date',
 'apply_time' => 'UInt32',
 'is_deleted' => 'UInt8' //0 active 1 //deleted

Migration history insert:

$command->insert($this->migrationTable, [
            'version' => $version,
            'apply_time' => time(),
            'is_deleted' => 0
        ])->execute();

So, no Date in insert...
image

Impossible to set database

Consider to add 'database' property to Connection or improve DSN syntax similar to regular connections (mysql for example): 'dsn' => 'tcp://localhost:8823;dbname=test'

Error while using OFFSET

SELECT `statistic`.* FROM `statistic` ALL INNER JOIN `copyright_owner` ON statistic.copyright_owner_id=copyright_owner.id ORDER BY `statistic`.`viewed_at` LIMIT 20 OFFSET 1;

Syntax error: failed at position 165:

SELECT `statistic`.* FROM `statistic` ALL INNER JOIN `copyright_owner` ON statistic.copyright_owner_id=copyright_owner.id ORDER BY `statistic`.`viewed_at` LIMIT 20 OFFSET 1;

Expected one of: UNION ALL, Comma, BY, SETTINGS, INTO OUTFILE, FORMAT, LIMIT, token

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.