forked from influxdata/influxdb-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteExample.php
70 lines (58 loc) · 1.84 KB
/
WriteExample.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Shows how to write data to InfluxDB via default API using Point structure or line protocol
*/
require __DIR__ . '/../vendor/autoload.php';
use InfluxDB2\Client;
use InfluxDB2\Point;
//
// Creating client
//
$client = new Client([
"url" => "http://localhost:8086",
"token" => "my-token",
"bucket" => "my-bucket",
"org" => "my-org",
"precision" => InfluxDB2\Model\WritePrecision::S
]);
//
// Function for checking results
//
function checkResult(string $filterName, Client $client, string $comment)
{
$query = "from(bucket: \"my-bucket\")
|> range(start: 0)
|> filter(fn: (r) => r[\"location\"] == \"$filterName\")";
$queryApi = $client->createQueryApi();
$result = $queryApi->query($query);
printf("\n\n----------------------- $comment -----------------------\n\n");
foreach ($result as $table) {
foreach ($table->records as $record) {
$location = $record["location"];
$temperature = $record->getValue();
$measurement = $record->getMeasurement();
$dateTime = $record->getTime();
print "$measurement: Temperature in $location at $dateTime is $temperature °C\n";
}
}
}
//
// Write data to InfluxDB using point structure
//
$writeApi = $client->createWriteApi();
$dateTimeNow = new DateTime('NOW');
$point = Point::measurement("weather")
->addTag("location", "Denver")
->addField("temperature", rand(0, 20))
->time($dateTimeNow->getTimestamp());
$writeApi->write($point);
checkResult("Denver", $client, "Write using point structure");
//
// Write data to InfluxDB using line protocol
//
$seconds = time();
$temp = rand(-5, 15)."i";
$writeApi->write("weather,location=Berlin temperature=$temp $seconds");
$writeApi->close();
checkResult("Berlin", $client, "Write using line protocol");
$client->close();