1. Go to this page and download the library: Download erikwang2013/clickhouse-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Erikwang2013\ClickHouse\Migration\Migration;
class CreateLogsTable extends Migration
{
public function up(): void
{
$this->schema->create('logs', function ($table) {
$table->date('date');
$table->string('level');
$table->engine('MergeTree')
->partitionBy('toYYYYMM(date)')
->orderBy(['date', 'level']);
});
}
public function down(): void
{
$this->schema->drop('logs');
}
}
use Erikwang2013\ClickHouse\Migration\Migrator;
use Erikwang2013\ClickHouse\Migration\Repository;
$client = ClickHouse::getManager()->connection();
$repository = new Repository($client);
$migrator = new Migrator($client, $repository, '/path/to/migrations');
$migrator->install(); // 创建迁移记录表
$migrator->run(); // 执行待迁移
$migrator->rollback(); // 回滚上一批
$migrator->refresh(); // 回滚后重新执行
use Erikwang2013\ClickHouse\Laravel\Facades\ClickHouse;
ClickHouse::table('logs')->where('level', 'error')->get();
ClickHouse::connection('native')->select('SELECT * FROM logs LIMIT 10');
use think\facade\ClickHouse;
ClickHouse::table('logs')->get();
use Erikwang2013\ClickHouse\Webman\ClickHouse;
ClickHouse::table('logs')->where('level', 'error')->get();
use Erikwang2013\ClickHouse\Hyperf\ClickHouseConnection;
class LogController
{
public function __construct(
private ClickHouseConnection $clickhouse
) {}
public function index()
{
return $this->clickhouse->table('logs')->get();
}
}
use Erikwang2013\ClickHouse\Yii3\ClickHouseConnection;
class LogController
{
public function __construct(private ClickHouseConnection $clickhouse) {}
public function index()
{
return $this->clickhouse->table('logs')->where('level', 'error')->get();
}
}
use Erikwang2013\ClickHouse\Client\StreamingClientInterface;
$client = ClickHouse::client(); // 需要底层客户端时用它(connection() 返回的是构造器)
if ($client instanceof StreamingClientInterface) {
foreach ($client->stream('SELECT * FROM logs') as $row) { // FORMAT JSONEachRow
echo $row['message'], PHP_EOL;
}
}
// 自带 FORMAT 的查询(CSV/TSV 等)用 raw(),原样拿响应体
$csv = $client->raw('SELECT * FROM logs FORMAT CSV');