Giter VIP home page Giter VIP logo

tdengine-restful-connector's Introduction

tdengine-restful-connector

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(); // 表明总共多少行数据;同接口返回格式

tdengine-restful-connector's People

Contributors

onlyshow avatar yurunsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tdengine-restful-connector's Issues

查询时间戳显示问题

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

$result = $taosClient->sql(sprintf('select * from weather.station');
`
查询出来的时间为"ts": "2023-12-02T15:26:34.000Z",用convert报错,该怎么调整代码能够显示2023-12-02 23:26:34.000

WARNING Channel::~Channel() (ERRNO 10003): channel is destroyed, 976 producers will be discarded

 [Coroutine-1970]
--------------------------------------------------------------------
#0  Swoole\Coroutine\Channel->push() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp/Handler/Swoole/SwooleHttpConnectionPool.php:97]
#1  Yurun\Util\YurunHttp\Handler\Swoole\SwooleHttpConnectionPool->release() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp/Handler/Swoole/SwooleHttpConnectionManager.php:100]
#2  Yurun\Util\YurunHttp\Handler\Swoole\SwooleHttpConnectionManager->release() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp/Handler/Swoole.php:404]
#3  Yurun\Util\YurunHttp\Handler\Swoole->recvDefer() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp/Handler/Swoole.php:227]
#4  Yurun\Util\YurunHttp\Handler\Swoole->send() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp.php:117]
#5  Yurun\Util\YurunHttp::send() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/HttpRequest.php:977]
#6  Yurun\Util\HttpRequest->send() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/HttpRequest.php:1036]
#7  Yurun\Util\HttpRequest->post() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/tdengine-restful-connector/src/Client.php:63]
#8  Yurun\TDEngine\Client->request() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/tdengine-restful-connector/src/Client.php:99]
#9  Yurun\TDEngine\Client->sql() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/tdengine-orm/src/ClientHandler/Restful/Handler.php:18]
#10 Yurun\TDEngine\Orm\ClientHandler\Restful\Handler->query() called at [/home/www/hyperf-skeleton/app/Service/CreateAlert2.php:23]
#11 App\Service\CreateAlert2->handle() called at [/home/www/hyperf-skeleton/app/Command/NodeCommand.php:57]
#12 App\Command\NodeCommand->App\Command\{closure}() called at [/home/www/hyperf-skeleton/vendor/hyperf/utils/src/Functions.php:274]
#13 call() called at [/home/www/hyperf-skeleton/vendor/hyperf/utils/src/Coroutine.php:62]

use 0.94960713386536 s
[2022-11-25 02:57:28 @5365.0]   WARNING Channel::~Channel() (ERRNO 10003): channel is destroyed, 976 producers will be discarded

似乎不支持3.0?

看起来是taosd的RESTful接口返回内容变了:

image

如上图,我是依赖codeception创建了测试代码,在2.6上是可以正确运行的。

翻查了提示信息中对应部分的源码,发现是处理POST请求的返回值时解析错误。

WARNING Channel::~Channel() (ERRNO 10003): channel is destroyed, 976 producers will be discarded

 [Coroutine-1970]
--------------------------------------------------------------------
#0  Swoole\Coroutine\Channel->push() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp/Handler/Swoole/SwooleHttpConnectionPool.php:97]
#1  Yurun\Util\YurunHttp\Handler\Swoole\SwooleHttpConnectionPool->release() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp/Handler/Swoole/SwooleHttpConnectionManager.php:100]
#2  Yurun\Util\YurunHttp\Handler\Swoole\SwooleHttpConnectionManager->release() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp/Handler/Swoole.php:404]
#3  Yurun\Util\YurunHttp\Handler\Swoole->recvDefer() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp/Handler/Swoole.php:227]
#4  Yurun\Util\YurunHttp\Handler\Swoole->send() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/YurunHttp.php:117]
#5  Yurun\Util\YurunHttp::send() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/HttpRequest.php:977]
#6  Yurun\Util\HttpRequest->send() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/yurun-http/src/HttpRequest.php:1036]
#7  Yurun\Util\HttpRequest->post() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/tdengine-restful-connector/src/Client.php:63]
#8  Yurun\TDEngine\Client->request() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/tdengine-restful-connector/src/Client.php:99]
#9  Yurun\TDEngine\Client->sql() called at [/home/www/hyperf-skeleton/vendor/yurunsoft/tdengine-orm/src/ClientHandler/Restful/Handler.php:18]
#10 Yurun\TDEngine\Orm\ClientHandler\Restful\Handler->query() called at [/home/www/hyperf-skeleton/app/Service/CreateAlert2.php:23]
#11 App\Service\CreateAlert2->handle() called at [/home/www/hyperf-skeleton/app/Command/NodeCommand.php:57]
#12 App\Command\NodeCommand->App\Command\{closure}() called at [/home/www/hyperf-skeleton/vendor/hyperf/utils/src/Functions.php:274]
#13 call() called at [/home/www/hyperf-skeleton/vendor/hyperf/utils/src/Coroutine.php:62]

use 0.94960713386536 s
[2022-11-25 02:57:28 @5365.0]   WARNING Channel::~Channel() (ERRNO 10003): channel is destroyed, 976 producers will be discarded

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.