Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: documentation about delete endpoint #63

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
1. [#58](https://github.com/influxdata/influxdb-client-php/pull/58): Updated default docker image to v2.0.3
1. [#60](https://github.com/influxdata/influxdb-client-php/pull/60): Added PHP 8 to CI, Added code style checking to CI

### Documentation
1. [#63](https://github.com/influxdata/influxdb-client-php/pull/63): Updated docs and examples about deleting data

## 1.9.0 [2020-12-04]

### Features
Expand Down
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ This repository contains the reference PHP client for the InfluxDB 2.0.

#### Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ ([see details](#influxdb-18-api-compatibility)). For connecting to InfluxDB 1.7 or earlier instances, use the [influxdb-php](https://github.com/influxdata/influxdb-php) client library.

- [Installation](#installation)
- [Install the library](#install-the-library)
- [Usage](#usage)
- [Creating a client](#creating-a-client)
- [Querying data](#queries)
- [Writing data](#writing-data)
- [Default Tags](#default-tags)
- [Advanced Usage](#advanced-usage)
- [Check the server status](#check-the-server-status)
- [InfluxDB 1.8 API compatibility](#influxdb-18-api-compatibility)
- [InfluxDB 2.0 management API](#influxdb-20-management-api)
- [Writing via UDP](#writing-via-udp)
- [Delete data](#delete-data)
- [Contributing](#contributing)
- [License](#license)

## Installation

The InfluxDB 2 client is bundled and hosted on [https://packagist.org/](https://packagist.org/packages/influxdata/influxdb-client-php).
Expand Down Expand Up @@ -400,6 +416,50 @@ $writer->write('h2o,location=west value=33i 15');
$writer->close();
```

### Delete data

The [DefaultService.php](/src/InfluxDB2/Service/DefaultService.php) supports deletes [points](https://v2.docs.influxdata.com/v2.0/reference/glossary/#point) from an InfluxDB bucket.

```php
<?php
/**
* Shows how to delete data from InfluxDB by client
*/
use InfluxDB2\Client;
use InfluxDB2\Model\DeletePredicateRequest;
use InfluxDB2\Service\DefaultService;

$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
]);

//
// Delete data by measurement and tag value
//
/** @var DefaultService $service */
$service = $client->createService(DefaultService::class);

$predicate = new DeletePredicateRequest();
$predicate->setStart(DateTime::createFromFormat('Y', '2020'));
$predicate->setStop(new DateTime());
$predicate->setPredicate("_measurement=\"mem\" AND host=\"host1\"");

$service->deletePost($predicate, null, $org, $bucket);

$client->close();
```

For more details see [DeleteDataExample.php](examples/DeleteDataExample.php).

## Local tests

```shell script
Expand Down
68 changes: 68 additions & 0 deletions examples/DeleteDataExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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\DefaultService;

$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 DefaultService $service */
$service = $client->createService(DefaultService::class);

$predicate = new DeletePredicateRequest();
$predicate->setStart(DateTime::createFromFormat('Y', '2020'));
$predicate->setStop(new DateTime());
$predicate->setPredicate("_measurement=\"mem\" AND host=\"host1\"");
$service->deletePost($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();