1. Go to this page and download the library: Download fr05t1k/phpclickhouse 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/ */
$statement = $db->select('SELECT * FROM summing_url_views LIMIT 2');
// Count select rows
$statement->count();
// Count all rows
$statement->countAll();
// fetch one row
$statement->fetchOne();
// get extremes min
print_r($statement->extremesMin());
// totals row
print_r($statement->totals());
// result all
print_r($statement->rows());
// totalTimeRequest
print_r($statement->totalTimeRequest());
// raw answer JsonDecode array, for economy memory
print_r($statement->rawData());
// raw curl_info answer
print_r($statement->responseInfo());
// human size info
print_r($statement->info());
// if clickhouse-server version >= 54011
$db->settings()->set('output_format_write_statistics',true);
print_r($statement->statistics());
$db->write('DROP TABLE IF EXISTS summing_url_views');
$state1 = $db->selectAsync('SELECT 1 as ping');
$state2 = $db->selectAsync('SELECT 2 as ping');
// run
$db->executeAsync();
// result
print_r($state1->rows());
print_r($state2->fetchOne('ping'));
$select = $db->selectAsync('SELECT * FROM summing_url_views LIMIT 1');
$insert = $db->insertBatchFiles('summing_url_views', ['/tmp/clickHouseDB_test.1.data'], ['event_time']);
// 'Exception' with message 'Queue must be empty, before insertBatch, need executeAsync'
class my
{
/**
* @return \ClickHouseDB\Cluster
*/
public function getClickHouseCluster()
{
return $this->_cluster;
}
public function msg($text)
{
echo $text."\n";
}
private function cleanTable($dbt)
{
$sizes=$this->getClickHouseCluster()->getSizeTable($dbt);
$this->msg("Clean table : $dbt,size = ".$this->humanFileSize($sizes));
// split string "DB.TABLE"
list($db,$table)=explode('.',$dbt);
// Get Master node for table
$nodes=$this->getClickHouseCluster()->getMasterNodeForTable($dbt);
foreach ($nodes as $node)
{
$client=$this->getClickHouseCluster()->client($node);
$size=$client->database($db)->tableSize($table);
$this->msg("$node \t {$size['size']} \t {$size['min_date']} \t {$size['max_date']}");
$client->dropOldPartitions($table,30,30);
}
}
public function clean()
{
$this->msg("clean");
$this->getClickHouseCluster()->setScanTimeOut(2.5); // 2500 ms
$this->getClickHouseCluster()->setSoftCheck(true);
if (!$this->getClickHouseCluster()->isReplicasIsOk())
{
throw new Exception('Replica state is bad , error='.$this->getClickHouseCluster()->getError());
}
$this->cleanTable('model.history_full_model_sharded');
$this->cleanTable('model.history_model_result_sharded');
}
}
$db = new ClickHouseDB\Client($config);
$db->settings()->https();