-
Notifications
You must be signed in to change notification settings - Fork 47
/
InfluxDB_18_Example.php
46 lines (37 loc) · 1.1 KB
/
InfluxDB_18_Example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Shows how to use forward compatibility APIs from InfluxDB 1.8.
*/
require __DIR__ . '/../vendor/autoload.php';
use InfluxDB2\Client;
use InfluxDB2\Point;
$username = 'username';
$password = 'password';
$database = 'telegraf';
$retentionPolicy = 'autogen';
$bucket = "$database/$retentionPolicy";
$client = new Client([
"url" => "http://localhost:8086",
"token" => "$username:$password",
"bucket" => $bucket,
"org" => "-",
"precision" => InfluxDB2\Model\WritePrecision::S
]);
$writeApi = $client->createWriteApi();
$point = Point::measurement("mem")
->addTag("host", "host1")
->addField("used_percent", 24.43234543)
->time(microtime(true));
$writeApi->write($point);
$writeApi->close();
$queryApi = $client->createQueryApi();
$query = "from(bucket: \"{$bucket}\") |> range(start: -1h)";
$tables = $queryApi->query($query);
foreach ($tables as $table) {
foreach ($table->records as $record) {
$time = $record->getTime();
$measurement = $record->getMeasurement();
$value = $record->getValue();
print "$time $measurement is $value\n";
}
}