jaVer / influxdb-odm
为InfluxDB提供PHP对象映射功能
Requires
- php: >=8.1
- doctrine/instantiator: ^1.3.1 || ^2.0
- doctrine/persistence: ^3.0
- influxdb/influxdb-php: ^1.0
Requires (Dev)
- doctrine/annotations: ^1.12 || ^2.0
- phpstan/phpstan: ^1.7
- squizlabs/php_codesniffer: 3.8.*
- swivl/php-coding-standard: ^1.4
Conflicts
- doctrine/annotations: <1.12 || >=3.0
README
InfluxDB ODM是一个库,为InfluxDB提供PHP对象映射功能。
安装
请确保已全局安装Composer,如Composer文档中的安装章节所述。
打开命令行控制台,进入您的项目目录,然后执行
$ composer require javer/influxdb-odm
用法
理解InfluxDB ODM的最佳方式是看其如何工作。在本节中,您将了解将测量数据持久化到InfluxDB并从中检索所需的所有步骤。
创建具有映射信息的测量类
Doctrine允许您以一种比仅作为数组来回获取数据更有趣的方式与InfluxDB交互。相反,Doctrine允许您将整个对象持久化到InfluxDB,并从InfluxDB中检索整个对象。这是通过将PHP类及其属性映射到InfluxDB测量条目来实现的。
为了让Doctrine能够这样做,您必须创建“元数据”,即配置,告诉Doctrine如何将CpuLoad
类及其属性映射到InfluxDB。此元数据可以直接在CpuLoad
类中通过注解指定
// src/Measurement/CpuLoad.php namespace App\Measurement; use Javer\InfluxDB\ODM\Mapping\Annotations as InfluxDB; /** * @InfluxDB\Measurement(name="cpu_load") */ class CpuLoad { /** * @InfluxDB\Timestamp(precision="u") */ private ?\DateTime $time = null; /** * @InfluxDB\Tag(name="server_id", type="integer") */ private ?int $serverId = null; /** * @InfluxDB\Tag(name="core_number", type="integer") */ private ?int $coreNumber = null; /** * @InfluxDB\Field(name="load", type="float", countable=true) */ private ?float $load = null; }
或使用PHP 8属性
// src/Measurement/CpuLoad.php namespace App\Measurement; use Javer\InfluxDB\ODM\Mapping\Annotations as InfluxDB; #[InfluxDB\Measurement(name: 'cpu_load')] class CpuLoad { #[InfluxDB\Timestamp(precision: 'u')] private ?\DateTime $time = null; #[InfluxDB\Tag(name: 'server_id', type: 'integer')] private ?int $serverId = null; #[InfluxDB\Tag(name: 'core_number', type: 'integer')] private ?int $coreNumber = null; #[InfluxDB\Field(name: 'load', type: 'float', countable: true)] private ?float $load = null; }
创建测量管理器
如果您不使用JaverInfluxDBBundle
,您必须创建一个MeasurementManager实例
use Doctrine\Common\Annotations\AnnotationReader; use Javer\InfluxDB\ODM\Connection\ConnectionFactory; use Javer\InfluxDB\ODM\Mapping\Driver\AnnotationDriver; use Javer\InfluxDB\ODM\MeasurementManager; use Javer\InfluxDB\ODM\Repository\RepositoryFactory; $dsn = 'influxdb://:8086/metrics'; $mappingDir = 'src/Measurements'; $mappingDriver = new AnnotationDriver(new AnnotationReader(), $mappingDir); $connectionFactory = new ConnectionFactory(); $repositoryFactory = new RepositoryFactory(); $measurementManager = new MeasurementManager($mappingDriver, $connectionFactory, $repositoryFactory, $dsn);
要使用注解,您将不得不安装一个名为doctrine/annotations
的额外包。
使用PHP 8属性
use Javer\InfluxDB\ODM\Connection\ConnectionFactory; use Javer\InfluxDB\ODM\Mapping\Driver\AttributeDriver; use Javer\InfluxDB\ODM\MeasurementManager; use Javer\InfluxDB\ODM\Repository\RepositoryFactory; $dsn = 'influxdb://:8086/metrics'; $mappingDir = 'src/Measurements'; $mappingDriver = new AttributeDriver($mappingDir); $connectionFactory = new ConnectionFactory(); $repositoryFactory = new RepositoryFactory(); $measurementManager = new MeasurementManager($mappingDriver, $connectionFactory, $repositoryFactory, $dsn);
将对象持久化到InfluxDB
现在,您已经有了一个带有getter和setter方法的映射CpuLoad
测量完整,您现在可以将数据持久化到InfluxDB
use App\Measurement\CpuLoad; use Javer\InfluxDB\ODM\MeasurementManager; $cpuLoad = new CpuLoad(); $cpuLoad->setTime(new DateTime()); $cpuLoad->setServerId(42); $cpuLoad->setCoreNumber(0); $cpuLoad->setLoad(3.14); /** @var MeasurementManager $measurementManager */ $measurementManager->persist($cpuLoad);
从InfluxDB检索对象
从InfluxDB检索对象也是可能的
$cpuLoad = $measurementManager->getRepository(CpuLoad::class)->find($time);
当您查询特定类型的对象时,您始终使用所谓的“存储库”。您可以将存储库想象成一个PHP类,它的唯一工作就是帮助您获取某个特定类的对象。您可以通过以下方式访问测量类的存储库对象
$repository = $measurementManager->getRepository(CpuLoad::class);
一旦您有了您的存储库,您就有权访问各种有用的方法
// query by the time $cpuLoad = $repository->find($time); // find *all* CPU metrics $cpuLoads = $repository->findAll(); // find a group of CPU metrics for the particular server $cpuLoads = $repository->findBy(['serverId' => 42]);
您还可以利用有用的findBy()
和findOneBy()
方法,根据多个条件轻松地获取对象
// query for one cpuLoad matching by serverId and coreNumber $cpuLoad = $repository->findOneBy(['serverId' => 42, 'coreNumber' => 3]); // query for all cpuLoads matching the serverId, ordered by time desc $cpuLoads = $repository->findBy( ['serverId' => 42], ['time' => 'DESC'] );
更新对象
一旦您从Doctrine中获取了对象,让我们尝试更新它。
$cpuLoad->setLoad(2.54); $measurementManager->persist($cpuLoad);
请注意,您不能更新时间戳或标签字段的值,只能更新简单的字段。
删除对象
删除对象非常类似,但需要调用测量管理器的remove()
方法
$measurementManager->remove($cpuLoad);
查询对象
如您所见,内置的存储库类允许您根据任何数量的不同参数查询一个或多个对象。当这足够时,这是查询测量的最简单方法。您还可以创建更复杂的查询。
使用查询构建器
InfluxDB ODM附带一个查询“构建器”对象,允许您构建一个查询,以返回您想要的精确测量。如果您使用IDE,您还可以在输入方法名称时利用自动完成。在一个控制器内部
$cpuLoads = $measurementManager->createQuery(CpuLoad::class) ->where('serverId', 42) ->orderBy('time', 'ASC') ->limit(10) ->getResult();
自定义存储库类
在上一个章节中,您开始在控制器内部构建和使用更复杂的查询。为了隔离、测试和重用这些查询,为您的测量创建一个自定义仓库类是一个好主意,并将您的查询逻辑放在那里。
为此,将仓库类的名称添加到您的映射定义中。
// src/Measurement/CpuLoad.php namespace App\Measurement; use App\Repository\CpuLoadRepository; use Javer\InfluxDB\ODM\Mapping\Annotations as InfluxDB; /** * @InfluxDB\Measurement(name="cpu_load", repositoryClass=CpuLoadRepository::class) */ class CpuLoad { // ... }
您必须创建上述命名空间中指定的仓库。确保它扩展默认的MeasurementRepository
。接下来,向新仓库类添加一个新方法 - findAllOrderedByTimeDesc()
- 此方法将查询所有按时间降序排列的CpuLoad
测量值。
// src/Repository/CpuLoadRepository.php namespace App\Repository; use Javer\InfluxDB\ODM\Repository\MeasurementRepository; class CpuLoadRepository extends MeasurementRepository { public function findAllOrderedByTimeDesc(): array { return $this->createQuery() ->orderBy('time', 'DESC') ->getResult(); } }
您可以像使用仓库的默认查找方法一样使用这个新方法。
$cpuLoads = $measurementManager->getRepository(CpuLoad::class) ->findAllOrderedByTimeDesc();
在使用自定义仓库类时,您仍然可以访问默认的查找方法,如find()
和findAll()
。