yurunsoft/tdengine-restful-connector

v2.0.2 2023-11-01 02:18 UTC

This package is auto-updated.

Last update: 2024-04-30 00:26:43 UTC


README

Latest Version GitHub Workflow Status (branch) Php Version License

简介

封装了 TDEngine 的 RESTful 接口,可以使用 PHP 轻松地操作 TDEngine 的数据插入和查询了。

此项目支持在 PHP >= 7.1 的项目中使用。

支持在 ThinkPHP、Laravel、Swooleimi 等项目中使用

在 Swoole 环境中支持协程化,不会阻塞!

技术支持 QQ 群: 17916227点击加群,如有问题可以及时解答和修复。

安装

composer require yurunsoft/tdengine-restful-connector

使用

使用连接管理器:

// 增加名称为 test 的连接配置
\Yurun\TDEngine\TDEngineManager::setClientConfig('test', new \Yurun\TDEngine\ClientConfig([
    // 'host'            => '127.0.0.1',
    // 'hostName'        => '',
    // 'port'            => 6041,
    // 'user'            => 'root',
    // 'password'        => 'taosdata',
    // 'db'              => 'database'
    // 'ssl'             => false,
    // 'timestampFormat' => \Yurun\TDEngine\Constants\TimeStampFormat::LOCAL_STRING, // 此配置 TDengine 3.0 下失效,统一返回格式为:2022-11-25T05:41:04.690Z
    // 'keepAlive'       => true,
    // 'version'         => '3', // TDengine 版本
    // 'timezone'        => 'UTC', // TDengine >= 3.0 时支持,可选参数,指定返回时间的时区
]));
// 设置默认数据库为test
\Yurun\TDEngine\TDEngineManager::setDefaultClientName('test');
// 获取客户端对象(\Yurun\TDEngine\Client)
$client = \Yurun\TDEngine\TDEngineManager::getClient();

直接 new 客户端:

$client = new \Yurun\TDEngine\Client(new \Yurun\TDEngine\ClientConfig([
    // 'host'            => '127.0.0.1',
    // 'hostName'        => '',
    // 'port'            => 6041,
    // 'user'            => 'root',
    // 'password'        => 'taosdata',
    // 'db'              => 'database'
    // 'ssl'             => false,
    // 'timestampFormat' => \Yurun\TDEngine\Constants\TimeStampFormat::LOCAL_STRING, // 此配置 TDengine 3.0 下失效,统一返回格式为:2022-11-25T05:41:04.690Z
    // 'keepAlive'       => true,
    // 'version'         => '3', // TDengine 版本
    // 'timezone'        => 'UTC', // TDengine >= 3.0 时支持,可选参数,指定返回时间的时区
]));

// 通过 sql 方法执行 sql 语句
var_dump($client->sql('create database if not exists db_test'));
var_dump($client->sql('show databases'));
var_dump($client->sql('create table if not exists db_test.tb (ts timestamp, temperature int, humidity float)'));
var_dump($client->sql(sprintf('insert into db_test.tb values(%s,%s,%s)', time() * 1000, mt_rand(), mt_rand() / mt_rand())));

$result = $client->sql('select * from db_test.tb');

$result->getResponse(); // 获取接口原始返回数据

// 获取列数据
foreach ($result->getColumns() as $column)
{
    $column->getName(); // 列名
    $column->getType(); // 列类型值
    $column->getTypeName(); // 列类型名称
    $column->getLength(); // 类型长度
}

// 获取数据
foreach ($result->getData() as $row)
{
    echo $row['列名']; // 经过处理,可以直接使用列名获取指定列数据
}

$result->getStatus(); // 告知操作结果是成功还是失败;同接口返回格式。仅 TDengine 2.x 有用。

$result->getHead(); // 表的定义,如果不返回结果集,则仅有一列“affected_rows”。(从 2.0.17 版本开始,建议不要依赖 head 返回值来判断数据列类型,而推荐使用 column_meta。在未来版本中,有可能会从返回值中去掉 head 这一项。);同接口返回格式

$result->getRows(); // 表明总共多少行数据;同接口返回格式