crodas / influx-php
InfluxDB 简单客户端
This package is auto-updated.
Last update: 2024-09-12 18:26:19 UTC
README
Simple PHP client for InfluxDB, an open-source, distributed, time series, events, and metrics database with no external dependencies.
InfluxDB > v0.9 支持
本库的 0.2.0 版本现在支持 InfluxDB 0.9。请注意,InfluxDB 0.9 仍然是预发布软件。
InfluxDB v0.8.X 用户
Influxdb >=0.9.0 对 API 做了许多破坏性更改。InfluxDB 0.8.X 用户可以通过使用 0.1.* 版本来使用旧版客户端。请参阅 0.1.* 版本的文档!
如何安装它
最简单的方法是通过 composer 安装
创建一个包含以下内容的 composer.json 文件
使用 InfluxDB 0.9 及以上版本
{ "require": { "crodas/influx-php": "^0.9" } }
使用 InfluxDB 0.8.* 版本
{ "require": { "crodas/influx-php": "0.1.*" } }
如何使用它
您需要创建一个客户端对象。
$client = new \crodas\InfluxPHP\Client( "localhost" /*default*/, 8086 /* default */, "root" /* by default */, "root" /* by default */ );
第一次应该创建一个数据库。
$db = $client->createDatabase("foobar"); $client->createUser("foo", "bar"); // <-- create user/password
您可以创建比默认 root 用户更多的用户。
$client->createUser('username', "password");
显示现有用户
$users = $client->getUsers();
用户权限由每个数据库的用户控制。任何用户都可以有 'read'、'write' 或 'all' 访问权限,分别由 InfluxClient::PRIV_READ、InfluxClient::PRIV_WRITE 和 InfluxClient::PRIV_ALL 常量表示。
$client->grantPrivilege(InfluxClient::PRIV_ALL, 'database', 'username'); // revoke rights by $client->revokePrivilege(InfluxClient::PRIV_ALL, 'database', 'username');
可以通过以下方式设置集群管理员
$client->setAdmin('username'); // delete admin righty by: $client->deleteAdmin('einuser');
创建数据非常简单。
$db = $client->foobar; // single input: $db->insert("some label", [ 'fields' => array('value' => 2)]); // single input; the 'name' identifier will be overwritten by array content: $db->insert("some label", ['name'=> 'some label', 'fields' => array('value' => 2)]); // multiple insert, this is better... // The 'name' field is optional, if not set, the default parameter (here: 'foobar') will be used $db->insert("foobar", array( array('name' => 'lala', 'fields' => array('type' => 'foobar', 'karma' => 25)), array( 'fields' => array('type' => 'foobar', 'karma' => 45)), ));
建议将大多数元数据编码到系列标签中。
$db->insert("foobar",[['tags' => ['type' => 'one'], 'fields' => ['value' => 10]]]);
现在您可以获取数据库对象并开始查询。
$db = $client->foobar; // OR $db = $client->getDatabase("foobar"); foreach ($db->query("SELECT * FROM foo") as $row) { var_dump($row, $row->time); }
如果有多个结果系列,将返回 MultipleResultSeriesObject 实例。因此,如果您不确定查询的结果,请检查返回对象的类型。
获取多个结果集的示例
// Here the 'type' value is stored as metadata in the tags entry. So if there are two 'type' tags found, you will get two result series $result = $db->query("SELECT count(value) FROM test1 where time >= '2015-01-01T12:00:00Z' and time < '2015-01-02T00:00:00Z' group by time(1h), type");
请查看 DBTest.php 类,您将找到更多示例。