forked from influxdata/influxdb-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteDataExample.php
68 lines (58 loc) · 1.71 KB
/
DeleteDataExample.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
<?php
/**
* Shows how to delete data from InfluxDB by client
*/
require __DIR__ . '/../vendor/autoload.php';
use InfluxDB2\Client;
use InfluxDB2\Model\DeletePredicateRequest;
use InfluxDB2\Point;
use InfluxDB2\Service\DeleteService;
$url = 'http://localhost:8086';
$token = 'my-token';
$org = 'my-org';
$bucket = 'my-bucket';
$client = new Client([
"url" => $url,
"token" => $token,
"bucket" => $bucket,
"org" => $org,
"precision" => InfluxDB2\Model\WritePrecision::S
]);
//
// Write data into InfluxDB
//
$writeApi = $client->createWriteApi();
$point1 = Point::measurement("mem")
->addTag("host", "host1")
->addField("used_percent", 24.43234543);
$point2 = Point::measurement("mem")
->addTag("host", "host2")
->addField("used_percent", 54.43234543);
$writeApi->write(array($point1, $point2));
$writeApi->close();
//
// Delete data by tag value: 'host = host2'
//
/** @var DeleteService $service */
$service = $client->createService(DeleteService::class);
$predicate = new DeletePredicateRequest();
$predicate->setStart(DateTime::createFromFormat('Y', '2020'));
$predicate->setStop(new DateTime());
$predicate->setPredicate("_measurement=\"mem\" AND host=\"host1\"");
$service->postDelete($predicate, null, $org, $bucket);
//
// Query Data
//
$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();
$host = $record->values["host"];
print "$time $measurement is $value for host: $host\n";
}
}
$client->close();