Skip to content

Commit

Permalink
feat: Improved documentation about batching, WriteOptions, WriteType …
Browse files Browse the repository at this point in the history
…should be in separate file (#24)
  • Loading branch information
bednar authored Apr 20, 2020
1 parent e75969b commit f623e81
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.3.0 [unreleased]

### Documentation

1. [#24](https://github.com/influxdata/influxdb-client-php/pull/24): Improved documentation about batching

## 1.2.0 [2020-04-17]

### Features
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ $write_api = $client->createWriteApi();
$write_api->write('h2o,location=west value=33i 15');
```

<!--- TODO still in progress
#### Batching

The writes are processed in batches which are configurable by `WriteOptions`:
Expand All @@ -153,18 +152,25 @@ The writes are processed in batches which are configurable by `WriteOptions`:
| **flushInterval** | the number of milliseconds before the batch is written | 1000 |

```php
$client = new InfluxDB2\Client(["url" => "http://localhost:9999", "token" => "my-token",
use InfluxDB2\Client;
use InfluxDB2\WriteType as WriteType;

$client = new Client(["url" => "http://localhost:9999", "token" => "my-token",
"bucket" => "my-bucket",
"org" => "my-org",
"precision" => InfluxDB2\Model\WritePrecision::NS
]);

$writeApi = $client->createWriteApi(
["writeType"=>InfluxDB2\WriteType::BATCHING, 'batchSize'=>1000, "flushInterval" =>1000]);
["writeType" => WriteType::BATCHING, 'batchSize' => 1000, "flushInterval" => 1000]);

$writeApi->write('h2o,location=west value=33i 15');
foreach (range(1, 10000) as $number) {
$writeApi->write("mem,host=aws_europe,type=batch value=1i $number");
}

// flush remaining data
$writeApi->close();
```
-->

#### Time precision

Expand Down
49 changes: 49 additions & 0 deletions examples/WriteBatchingExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Shows how to use batching for more performance writes.
*/

require __DIR__ . '/../vendor/autoload.php';

use InfluxDB2\Client;
use InfluxDB2\WriteType as WriteType;

$organization = 'my-org';
$bucket = 'my-bucket';
$token = 'my-token';

$client = new Client([
"url" => "http://localhost:9999",
"token" => $token,
"bucket" => $bucket,
"org" => $organization,
"precision" => InfluxDB2\Model\WritePrecision::S
]);

print "*** Write by batching ***\n";

$writeApi = $client->createWriteApi(
["writeType" => WriteType::BATCHING, 'batchSize' => 1000, "flushInterval" => 1000]);

foreach (range(1, 10000) as $number) {
$writeApi->write("mem,host=aws_europe,type=batch value=1i $number");
}

// flush remaining data
$client->close();

print "*** Check result ***\n";

$queryApi = $client->createQueryApi();
$query = "from(bucket: \"$bucket\")
|> range(start: 0)
|> filter(fn: (r) => r[\"_measurement\"] == \"mem\")
|> filter(fn: (r) => r[\"host\"] == \"aws_europe\")
|> count()";
$tables = $queryApi->query($query);
$record = $tables[0]->records[0];
$value = $record->getValue();
print "Count: $value\n";

$client->close();

33 changes: 0 additions & 33 deletions src/InfluxDB2/WriteApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,6 @@

use InfluxDB2\Model\WritePrecision;

class WriteType
{
const SYNCHRONOUS = 1;
const BATCHING = 2;
}

class WriteOptions
{
const DEFAULT_BATCH_SIZE = 10;
const DEFAULT_FLUSH_INTERVAL = 1000;

public $writeType;
public $batchSize;
public $flushInterval;

/**
* WriteOptions constructor.
* $writeOptions = [
* 'writeType' => methods of write (WriteType::SYNCHRONOUS - default, WriteType::BATCHING)
* 'batchSize' => the number of data point to collect in batch
* 'flushInterval' => flush data at least in this interval
* ]
* @param array $writeOptions Array containing the write parameters (See above)
*/
public function __construct(array $writeOptions = null)
{
//initialize with default values
$this->writeType = $writeOptions["writeType"] ?: WriteType::SYNCHRONOUS;
$this->batchSize = $writeOptions["batchSize"] ?: self::DEFAULT_BATCH_SIZE;
$this->flushInterval = $writeOptions["flushInterval"] ?: self::DEFAULT_FLUSH_INTERVAL;
}
}

/**
* Write time series data into InfluxDB.
* @package InfluxDB2
Expand Down
31 changes: 31 additions & 0 deletions src/InfluxDB2/WriteOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace InfluxDB2;

class WriteOptions
{
const DEFAULT_BATCH_SIZE = 10;
const DEFAULT_FLUSH_INTERVAL = 1000;

public $writeType;
public $batchSize;
public $flushInterval;

/**
* WriteOptions constructor.
* $writeOptions = [
* 'writeType' => methods of write (WriteType::SYNCHRONOUS - default, WriteType::BATCHING)
* 'batchSize' => the number of data point to collect in batch
* 'flushInterval' => flush data at least in this interval
* ]
* @param array $writeOptions Array containing the write parameters (See above)
*/
public function __construct(array $writeOptions = null)
{
//initialize with default values
$this->writeType = $writeOptions["writeType"] ?: WriteType::SYNCHRONOUS;
$this->batchSize = $writeOptions["batchSize"] ?: self::DEFAULT_BATCH_SIZE;
$this->flushInterval = $writeOptions["flushInterval"] ?: self::DEFAULT_FLUSH_INTERVAL;
}
}

9 changes: 9 additions & 0 deletions src/InfluxDB2/WriteType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace InfluxDB2;

class WriteType
{
const SYNCHRONOUS = 1;
const BATCHING = 2;
}

0 comments on commit f623e81

Please sign in to comment.