ierusalim/php-clickhouse

ClickHouse 简单访问库 (http, https)

0.2.4 2019-05-12 11:35 UTC

This package is auto-updated.

Last update: 2024-09-12 23:46:40 UTC


README

Build Status codecov

ClickHouse 简单访问库 (http, https)

ClickHouseAPI

ClickHouseAPI 包含用于 ClickHouse 服务器的简单 http/https 连接器,并且没有依赖项(可以独立使用,文件 src/ClichHouseAPI.php)。

API 简单请求函数

  • query($sql [,$post_data]) - 对象风格的 SQL 查询(返回 $this,抛出异常)
  • getQuery($h_query [, $sess]) - 发送 GET 请求
  • postQuery($h_query, $post_data [, $sess]) - 发送 POST 请求

异步(并行)请求

  • 在任意请求之前设置 toSlot(name),请求将被异步启动。
  • toSlot("name")>query($sql) - 开始异步 $sql 查询,结果将被写入槽 "name"。
  • 可以在任何时间从该槽获取结果。
  • slotResults("name") - 从槽 "name" 获取结果。

服务器状态函数

  • setServerUrl($url) - 通过 URL 设置 ClickHouse 服务器参数(主机、端口等)
  • getVersion() - 返回 ClickHouse 服务器的版本(副作用 - 检测服务器功能)
  • isSupported(feature-name) - 根据服务器是否支持功能返回 true 或 false。
会话
  • getSession() - 从选项获取当前 session_id
  • setSession([$sess]) - 设置 session_id 或生成新的 session_id 并设置它
选项
  • setOption($key, $value) - 设置所有后续请求的 http 选项
  • getOption($key) - 获取当前的 http 选项值
  • delOption($key) - 删除 http 选项(等同于 ->setOption($key, null)

ClickHouseQuery

ClickHouseQuery 包含 ClickHouseAPI 的包装器,允许轻松地向 ClickHouse 服务器发送查询并解析响应数据。

主要查询函数用于使用

  • queryFalse($sql, [post])- 对于不应返回任何内容查询。如果成功返回 false,否则返回错误字符串。
  • queryTrue($sql, [post]) - 只有在错误的情况下返回 false,否则返回 true 或响应数据。
  • queryValue($sql, [post]) - 发送任何查询并接收所有数据作为一个字符串(错误时返回 false)
  • queryArray($sql) - 对于返回结构化数据(通常是一行或多行表)的查询
  • queryKeyValues(see descr.) - 对于返回两列的查询,第一列作为键,第二列作为值
  • queryInsertArray($table, $fields_names, $fields_set) - 从数组中插入数据到表中
  • queryInsertFile($table, $file, $structure) - 从文件中插入数据到表中
  • queryInsertGzip($table, $file, $format [, $fields]) - 从文件中插入数据,在发送时使用 gzip

ClickHouseFunctions

ClickHouseFunctions 基于 ClickHouseQuery 和 ClickHouseAPI,并包含用于与 ClickHouse 简单操作的函数。

函数

  • createTableQuick($table, $fields_arr) - 使用指定的字段创建表
  • sendFileInsert($file, $table) - 将 TabSeparated 文件发送到表中(自动检测结构)
  • clearTable($table [, $sess]) - 清空表(DROP 并重新创建)
  • dropTable($table [, $sess]) - 删除指定的表
  • renameTable($from_name_or_arr [, $to_name] [, $sess]) - 重命名表
  • getTableFields($table, ...) - 返回 [field_name=>field_type] 数组
  • getTableInfo($table [, $extended]) - 返回包含表信息的数组
  • getTablesList([$db] [,$pattern]) - 通过 SHOW TABLES 请求返回表列表
  • createDatabase($db) - 创建具有指定名称的新数据库
  • dropDatabase($db) - 删除指定的数据库并移除所有表
  • getDatabasesList() - 返回包含现有数据库名称的数组
  • setCurrentDatabase($db [, $sess]) - 通过'USE db'请求或选项设置当前数据库
  • getCurrentDatabase([$sess]) - 返回'SELECT currentDatabase()'的结果或从选项中获取
  • getUptime() - 返回服务器运行时间(以秒为单位)
  • getSystemSettings() - 以数组[name=>value]的形式获取系统设置信息

示例

<?php
    namespace ierusalim\ClickHouse;

    require "vendor/autoload.php";

    $ch = new ClickHouseFunctions("http://127.0.0.1:8123/");

    echo "ClickHouse version: " . $ch->getVersion();
    if (!$ch->isSupported('query')) {
        die(" Server not ready");
    }
    echo " Server uptime: " . $ch->getUptime();
    
    echo "\n\nDatabases: ";
    print_r($ch->getDatabasesList());

    $ch->setCurrentDatabase("system");
    echo "Tables in '" . $ch->getCurrentDatabase() ."' database:\n";
    print_r($ch->getTablesList());

    $ch->setCurrentDatabase("default");

    $ch->createTableQuick("temptab", [
        'id'   => 'integer',
        'dt' => 'date now()',
        'name' => "char(32) 'example'",
        'email'  => 'string'
    ]);
    
    $ch->queryInsertArray("temptab", null, [
        'id' => 1,
        'email' => 'noreply@github.com'
    ]);
    
    $ch->queryInsertArray("temptab", ['id', 'email', 'name'], [
        [2, 'reply@github.com', 'Andy'],
        [3, null , 'Donald'],
    ]);

    $ch->queryInsertArray("temptab", null, [
        ['id'=>4, 'name'=>'Ronald', 'email'=>'no'],
        ['id'=>5, 'name'=>'', 'email'=>'yes'],
    ]);
    
    $rows = $ch->queryArray("SELECT * FROM temptab");
    print_r($rows);
    
    $name_emails_arr = $ch->queryKeyValues('temptab', 'name, email');
    print_r($name_emails_arr);
    
    print_r($ch->getTableInfo("temptab"));