enepochatova/influxdb-orm
Composer 安装命令:
composer require enepochatova/influxdb-orm
包简介
The PHP library which helps to convert entities to InfluxDB points and back. Uses annotations for entities configuration.
README 文档
README
The PHP library which helps to convert entities to InfluxDB points and back. Uses annotations for entities configuration. For better understanding of key concepts please check the official InfluxDB documentation.
Installation
composer require enepochatova/influxdb-orm
Getting started
Create entity to be stored in DB. Give it the Measurement annotation. The name property of annotation is required.
<?php namespace SomeNamespace; use InfluxDB\ORM\Mapping\Annotations as InfluxDB; /** * Class Cpu * @InfluxDB\Measurement(name="cpu") */ class Cpu { }
Then add some properties to class and annotations for them. The annotations for properties are:
- Value: Must be given to numeric property and be unique among class hierarchy. If this annotation is not given to any property of class hierarchy, it will be equal to null and the field 'value' will not be created.
- Field: Has required key property, which is very similar to column name in relational DB.
- Tag: Has required key and optional type properties. By default type is "string". The other valid values are: "int", "float", "bool". Be attended that tags are always stored as strings in DB. That's why we need to specify the original type of class property for convert it back and build entity correctly after we got data from DB.
- ArrayOfMetrics: Must be given to property which contains one-dimensional associative array. This annotation must be unique among class hierarchy. Array values will be stored in DB as fields with appropriate keys.
- Timestamp: Must be given to numeric property and be unique among class hierarchy. If this annotation is not given to any property of class hierarchy, it will be equal to null and the point will be stored with current server timestamp.
Example:
<?php namespace SomeNamespace; use InfluxDB\ORM\Mapping\Annotations as InfluxDB; /** * Class Cpu * @InfluxDB\Measurement(name="cpu") */ class Cpu { /** * @var float * @InfluxDB\Value */ private $value; /** * @var int * @InfluxDB\Field(key="tasks") */ private $tasks; /** * @var int * @InfluxDB\Field(key="services") */ private $activeServices; /** * @var string * @InfluxDB\Field(key="host") */ private $host; /** * @var int * @InfluxDB\Tag(key="category", type="int") */ private $categoryId; /** * @var array * @InfluxDB\ArrayOfMetrix() */ private $config; /** * Cpu constructor. * @param float $value * @param int $tasks * @param int $activeServices * @param string $host * @param int $categoryId */ public function __construct($value, $tasks, $activeServices, $host, $categoryId, $config) { $this->value = $value; $this->tasks = $tasks; $this->activeServices = $activeServices; $this->host = $host; $this->categoryId = $categoryId; $this->config = $config; } // getters here }You need to set class properties public or implement getters for them.
Also it is possible to map inherited classes. The reading of annotations starts from the target class and goes to its parents. Annotations from children classes have higher priority.
After the entity is done, we need to create the Repository class which extends BaseRepository from this library:
<?php namespace SomeNamespace; use InfluxDB\Database; use InfluxDB\ORM\Mapping\AnnotationDriver; use InfluxDB\ORM\Repository\BaseRepository; class CpuRepository extends BaseRepository { /** * CpuRepository constructor. * @param AnnotationDriver $annotationDriver * @param Database $database * @param string|null $precision * @throws \ReflectionException */ public function __construct(AnnotationDriver $annotationDriver, Database $database, string $precision = null) { parent::__construct($annotationDriver, $database, Cpu::class, $precision); } }
That's it!
Now you are ready to use it like:
<?php ... $cpuRepositoryInstance->write($cpuInstance); $cpuMetrics = $cpuRepositoryInstance->findAll(); ...
Enjoy!
enepochatova/influxdb-orm 适用场景与选型建议
enepochatova/influxdb-orm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 enepochatova/influxdb-orm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 enepochatova/influxdb-orm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-16