zoukankan      html  css  js  c++  java
  • php 链接 clickhouse

    一、通过pypclickhouse插件连接ck数据库
       博主用的1.1.2因为我们公司使用的php5点几版本,这是最高支持
         插件地址:https://github.com/smi2/phpClickHouse/tree/e27b04d482e9922df8bf1ea0880bf2985d2e06d0
      
     
    使用默认配置去连接 clickhouse
    错误:Fatal error: Call to undefined function curl_init()
    解决:在php.ini 文件中 将 extension=php_curl.dll 前的分号去掉
    错误:Warning: PHP Startup: Unable to load dynamic library 'C:phpextphp_curl.dll
    因为刚才php.ini文件引入的 php_curl.dll 文件系统默认在 ‘C:/php/ext/’下去找
    两种解决方式,把该文件拷贝过去,或者php.ini中配置查找路径:
    ; extension_dir = "./"
    ; On windows:
    extension_dir = "ext" #将此行前面分号去掉
     
     
      1 <?php
      2 include_once __DIR__ . '/third_party/phpclickhouse1.1.2/include.php';
      3 
      4 $config = [
      5     'host' => '10.222.1.213',
      6     'port' => '8123',
      7     'username' => 'sangfor',
      8     'password' => 'sangfor123'
      9 ];
     10 $tb_name = 'netflow';
     11 
     12 
     13 $db_conn = new ClickHouseDBClient($config);
     14 $db_conn->database('test');
     15 $db_conn->setTimeout(1.5);      // 1500 ms
     16 $db_conn->setTimeout(10);       // 10 seconds
     17 $db_conn->setConnectTimeOut(5); // 5 seconds
     18 
     19 $db_conn->ping();
     20 
     21 function zifll($param,$char='0')
     22 {
     23     if(!is_string($param))
     24     {
     25         $param = (string)$param;
     26     }
     27     if(strlen($param)<2)
     28     {
     29         $param = "0".$param;
     30     }
     31     else if(strlen($param)>2)
     32     {
     33         $param = substr($param,strlen($param)-2);
     34     }
     35     return $param;
     36 }
     37 
     38 $sql = "update {$tb_name} set record_time=";
     39 
     40 $param = array(
     41     'time_range'=>"2020-05-03 00:00:00|2020-07-07 00:00:00",
     42     'src_ip'=>"192.168.0.1",
     43     'src_port'=>8080,
     44     'dst_ip'=>'',
     45     'dst_port'=>null,
     46     'page'=>0
     47 );
     48 
     49 function get_query_sql($param)
     50 {
     51     $query_sql = "select * from test.netflow ";
     52     if(empty($param)||!is_array($param))
     53     {
     54         echo "No paramter!";
     55         exit();
     56     }
     57     $time_rage = explode("|",$param["time_range"]);
     58     if($time_rage[0])
     59     {
     60        $query_sql .= "where ";
     61        $query_sql .= "record_time > '{$time_rage[0]}' ";
     62     }
     63     if($time_rage[1])
     64     {
     65         $query_sql .= "and ";
     66         $query_sql .= "record_time < '{$time_rage[1]}' ";
     67     }
     68 //    array_splice($param,"time_range");
     69     unset($param["time_range"]);
     70     foreach($param as $key=>$value)
     71     {
     72         if(isset($param[$key]))
     73         {
     74             if ($key=="page"){
     75                 $offset = $value==0?"0":(string)$value*20;
     76                 $query_sql .= "limit 20 offset {$offset}";
     77             }
     78             else if($value)
     79             {
     80                 $query_sql .= "and ";
     81                 if(is_string($value))
     82                 {
     83                     $query_sql .= "{$key} = '{$value}' ";
     84                 }
     85                 else
     86                 {
     87                     $query_sql .= "{$key} = {$value} ";
     88                 }
     89             }
     90 
     91 
     92         }
     93     }
     94 //    $query_sql = $query_sql.trim();
     95     $query_sql = trim($query_sql);
     96     $query_sql .= ";";
     97     return $query_sql;
     98 }
     99 
    100 $result = $db_conn->select(get_query_sql($param));
    101 $cur_info_all = $result->rawData();
    102 $cur_data = $cur_info_all["data"];
    103 echo(json_encode($cur_data));
    104 ?>
  • 相关阅读:
    【转载】python_logging模块
    python安装模块速度慢的解决方法
    2020软件工程作业00
    2020软件工程个人作业——软件工程实践总结
    2020软件工程作业05
    2020软件工程作业04
    2020软件工程作业03
    2020软件工程作业02
    2020软件工程作业01
    列表、元组、集合和字典区别
  • 原文地址:https://www.cnblogs.com/shiqi17/p/12827926.html
Copyright © 2011-2022 走看看