influxdb-orm v1.0.1 发布,时序数据库 InfluxDB 的 PHP ORM

栏目: 软件资讯 · 发布时间: 4年前

内容简介:介绍 一个用于 InfluxDB 时序数据库的 ORM,终结没有 InfluxDB ORM 的时代。 常用操作一把梭,支持 php-fpm、Swoole 环境,一键轻松切换。 可以用于所有传统框架、所有 Swoole 框架中! 码云:https://gitee.com...

介绍

一个用于 InfluxDB 时序数据库的 ORM,终结没有 InfluxDB ORM 的时代。

常用操作一把梭,支持 php-fpm、Swoole 环境,一键轻松切换。

可以用于所有传统框架、所有 Swoole 框架中!

码云:https://gitee.com/yurunsoft/influxdb-orm

Github:https://github.com/Yurunsoft/influxdb-orm

Composer

本项目可以使用composer安装,遵循psr-4自动加载规则,在你的 composer.json 中加入下面的内容:

{
    "require": {
        "yurunsoft/influxdb-orm": "^1.0.0"
    }
}

然后执行 composer update 安装。

使用

Swoole 支持

在 WorkerStart 事件中执行:

\Yurun\Util\YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class);

定义模型

具体可参考 example/test.php

<?php
namespace Yurun\InfluxDB\ORM\Example\Model;

use Yurun\InfluxDB\ORM\BaseModel;
use Yurun\InfluxDB\ORM\Annotation\Tag;
use Yurun\InfluxDB\ORM\Annotation\Field;
use Yurun\InfluxDB\ORM\Annotation\Value;
use Yurun\InfluxDB\ORM\Annotation\Timestamp;
use Yurun\InfluxDB\ORM\Annotation\Measurement;

/**
 * @Measurement(name="aaa")
 */
class A extends BaseModel
{
    /**
     * @Tag(name="id", type="int")
     *
     * @var int
     */
    private $id;

    /**
     * @Field(name="name", type="string")
     *
     * @var string
     */
    private $name;

    /**
     * @Timestamp(precision="s")
     *
     * @var int|string
     */
    private $time;

    /**
     * @Value
     *
     * @var int
     */
    private $value;

    public static function create($id, $name, $time, $value)
    {
        return new static(compact('id', 'name', 'time', 'value'));
    }

    /**
     * Get the value of time
     *
     * @return int|string
     */ 
    public function getTime()
    {
        return $this->time;
    }

    /**
     * Set the value of time
     *
     * @param int|string $time
     *
     * @return self
     */ 
    public function setTime($time)
    {
        $this->time = $time;

        return $this;
    }

    /**
     * Get the value of id
     *
     * @return int
     */ 
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of id
     *
     * @param int $id
     *
     * @return self
     */ 
    public function setId(int $id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get the value of name
     *
     * @return string
     */ 
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set the value of name
     *
     * @param string $name
     *
     * @return self
     */ 
    public function setName(string $name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get the value of value
     *
     * @return int
     */ 
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Set the value of value
     *
     * @param int $value
     *
     * @return self
     */ 
    public function setValue(int $value)
    {
        $this->value = $value;

        return $this;
    }
}

数据写入

use Yurun\InfluxDB\ORM\InfluxDBManager;

// 设置客户端名称为test,默认数据库为db_test
InfluxDBManager::setClientConfig('test', '127.0.0.1', 8086, '', '', false, false, 0, 0, 'db_test');
// 设置默认数据库为test
InfluxDBManager::setDefaultClientName('test');

// 写入数据,支持对象和数组
$r = A::write([
    A::create(mt_rand(1, 999999), time(), time(), mt_rand(1, 100)),
    ['id'=>1, 'name'=>'aaa', 'time'=>time(), 'value'=>mt_rand(1, 100)],
]);

var_dump($r);

数据查询

// 获取查询器
$query = A::query();

// 常见用法,反正就那一套,不多说了
$query->field('id,name')
      ->from('table')
      ->where([
          'id'    =>  1
      ])->where('id', '=', 1)
      ->orWhere('id', '=', 1)
      ->order('time', 'desc')
      ->group('id')
      ->limit(0, 10);

// 查询结果,与 InfluxDB 官方客户端一样用法
$resultSet = $query->select();

// 查询结果转模型,适合用于查询记录而不是统计数据
$model = $resultSet->getModel(A::class);

// 查询结果转模型列表,适合用于查询记录而不是统计数据
$list = $resultSet->getModelList(A::class);

模型快捷查询

适合用于查询记录而不是统计数据

use Yurun\InfluxDB\ORM\Query\QueryBuilder;

// 查询结果转模型,适合用于查询记录而不是统计数据
$model = A::find(function(QueryBuilder $query){
    $query->where('id', '=', 1)->limit(1);
});

// 查询结果转模型列表,适合用于查询记录而不是统计数据
$list = A::select(function(QueryBuilder $query){
    $query->where('id', '=', 1)->limit(2);
});

获取单个字段值

$count = A::query()->field('count(value)')->select()->getScalar();

以上所述就是小编给大家介绍的《influxdb-orm v1.0.1 发布,时序数据库 InfluxDB 的 PHP ORM》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

工程问题C++语言求解

工程问题C++语言求解

Delores M.Etter、Jeanine A.Ingber / 冯力、周凯 / 机械工业出版社 / 2014-8 / 79元

本书介绍了如何利用ANSIC++编程语言以基于对象的编程方式来解决工程问题。书中引用了大量来自于不同工程、科学和计算机科学领域的示例,是一本理论和实践结合紧密的教材。针对C++基本语法的各个部分,由浅入深地进行讲解。每讲解一部分基础知识,同时会结合多个相关实例,实例内容详实,紧贴所讲内容,使读者能够立刻对所学知识进行练习,实战性强。一起来看看 《工程问题C++语言求解》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具