version 1.5
This commit is contained in:
+102
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Shows how to create, list and delete Buckets
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
use InfluxDB2\Model\BucketRetentionRules;
|
||||
use InfluxDB2\Model\Organization;
|
||||
use InfluxDB2\Model\PostBucketRequest;
|
||||
use InfluxDB2\Service\BucketsService;
|
||||
use InfluxDB2\Service\OrganizationsService;
|
||||
|
||||
$organization = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "http://localhost:8086",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $organization,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Function for getting organization ID
|
||||
//
|
||||
function findMyOrg($client): ?Organization
|
||||
{
|
||||
/** @var OrganizationsService $orgService */
|
||||
$orgService = $client->createService(OrganizationsService::class);
|
||||
$orgs = $orgService->getOrgs()->getOrgs();
|
||||
foreach ($orgs as $org) {
|
||||
if ($org->getName() == $client->options["org"]) {
|
||||
return $org;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
// Creating bucket service
|
||||
//
|
||||
$bucketsService = $client->createService(BucketsService::class);
|
||||
|
||||
//
|
||||
// Creating bucket
|
||||
//
|
||||
print "\n\n----------------------------------------- Bucket create -----------------------------------------\n";
|
||||
$rule = new BucketRetentionRules();
|
||||
$rule->setEverySeconds(3600);
|
||||
|
||||
$bucketName = "example-bucket-" . microtime();
|
||||
$bucketRequest = new PostBucketRequest();
|
||||
$bucketRequest->setName($bucketName)
|
||||
->setRetentionRules([$rule])
|
||||
->setOrgId(findMyOrg($client)->getId());
|
||||
|
||||
$respBucket = $bucketsService->postBuckets($bucketRequest);
|
||||
|
||||
$bucketName = $respBucket->getName();
|
||||
$bucketId = $respBucket->getID();
|
||||
$createdAt = $respBucket->getCreatedAt()->format('Y-m-d H:i:s');
|
||||
|
||||
print "ID: $bucketId Created: $createdAt Name: $bucketName was created\n";
|
||||
|
||||
//
|
||||
// Get all buckets
|
||||
//
|
||||
print "\n\n----------------------------------------- Bucket List -----------------------------------------\n";
|
||||
$bucketList = $bucketsService->getBuckets();
|
||||
|
||||
foreach ($bucketList->getBuckets() as $item) {
|
||||
$bucketName = $item->getName();
|
||||
$bucketId = $item->getID();
|
||||
$createdAt = $item->getCreatedAt()->format('Y-m-d H:i:s');
|
||||
|
||||
print "ID: $bucketId Created: $createdAt Name: $bucketName \n";
|
||||
}
|
||||
|
||||
//
|
||||
// Delete all buckets contains 'example-bucket'
|
||||
//
|
||||
print "\n\n----------------------------------------- Bucket delete -----------------------------------------\n";
|
||||
$bucketList = $bucketsService->getBuckets();
|
||||
foreach ($bucketList->getBuckets() as $item) {
|
||||
$bucketName = $item->getName();
|
||||
if (strpos($bucketName, 'example-bucket') !== false) {
|
||||
$createdAt = $item->getCreatedAt()->format('Y-m-d H:i:s');
|
||||
|
||||
$bucketsService->deleteBucketsID($item->getID());
|
||||
|
||||
print "Name: $bucketName Created: $createdAt was deleted \n";
|
||||
}
|
||||
}
|
||||
|
||||
$client->close();
|
||||
@@ -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\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();
|
||||
@@ -0,0 +1,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";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
/**
|
||||
* Show to use Invokable scripts Cloud API to create custom endpoints that query data
|
||||
*
|
||||
* Invokable Scripts are supported only in InfluxDB Cloud, currently there is no support in InfluxDB OSS.
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
use InfluxDB2\Model\ScriptCreateRequest;
|
||||
use InfluxDB2\Model\ScriptUpdateRequest;
|
||||
use InfluxDB2\Point;
|
||||
|
||||
$organization = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "https://us-west-2-1.aws.cloud2.influxdata.com",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $organization,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Prepare data
|
||||
//
|
||||
$point1 = Point::measurement("my_measurement")
|
||||
->addTag("location", "Prague")
|
||||
->addField("temperature", 25.3);
|
||||
$point2 = Point::measurement("my_measurement")
|
||||
->addTag("location", "New York")
|
||||
->addField("temperature", 24.3);
|
||||
$client->createWriteApi()->write([$point1, $point2]);
|
||||
|
||||
//
|
||||
// Creating InvokableScripts Api
|
||||
//
|
||||
$scriptsApi = $client->createInvokableScriptsApi();
|
||||
|
||||
//
|
||||
// Create Invokable Script
|
||||
//
|
||||
print "\n------- Create -------\n";
|
||||
$scriptQuery = 'from(bucket: params.bucket_name) |> range(start: -30d) |> limit(n:2)';
|
||||
$createRequest = new ScriptCreateRequest([
|
||||
'name' => "my_script_" . microtime(),
|
||||
'description' => "my first try",
|
||||
'script' => $scriptQuery,
|
||||
'language' => InfluxDB2\Model\ScriptLanguage::FLUX,
|
||||
]);
|
||||
$createdScript = $scriptsApi->createScript($createRequest);
|
||||
print $createdScript;
|
||||
|
||||
//
|
||||
// Update Invokable Script
|
||||
//
|
||||
print "\n------- Update -------\n";
|
||||
$updateRequest = new ScriptUpdateRequest([
|
||||
'description' => "my updated description"
|
||||
]);
|
||||
$createdScript = $scriptsApi->updateScript($createdScript->getId(), $updateRequest);
|
||||
print $createdScript;
|
||||
|
||||
//
|
||||
// Invoke a script
|
||||
//
|
||||
print "\n------- Invoke to FluxTables -------\n";
|
||||
$tables = $scriptsApi->invokeScript($createdScript->getId(), ["bucket_name" => $bucket]);
|
||||
foreach ($tables as $table) {
|
||||
foreach ($table->records as $record) {
|
||||
print "\n{$record['time']} {$record['location']}: {$record['_field']} {$record['_value']}";
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Stream of FluxRecords
|
||||
//
|
||||
print "\n";
|
||||
print "\n------- Invoke to Stream of FluxRecords -------\n";
|
||||
$records = $scriptsApi->invokeScriptStream($createdScript->getId(), ["bucket_name" => $bucket]);
|
||||
foreach ($records->each() as $record) {
|
||||
print "\n{$record['time']} {$record['location']}: {$record['_field']} {$record['_value']}";
|
||||
}
|
||||
|
||||
//
|
||||
// RAW
|
||||
//
|
||||
print "\n";
|
||||
print "\n------- Invoke to Raw-------\n";
|
||||
$raw = $scriptsApi->invokeScriptRaw($createdScript->getId(), ["bucket_name" => $bucket]);
|
||||
print "RAW output:\n\n {$raw}";
|
||||
|
||||
//
|
||||
// List scripts
|
||||
//
|
||||
print "\n------- List -------\n";
|
||||
$scripts = $scriptsApi->findScripts();
|
||||
foreach ($scripts as $script) {
|
||||
$scriptId = $script->getId();
|
||||
$scriptName = $script->getName();
|
||||
$scriptDescription = $script->getDescription();
|
||||
print " ---\n ID: $scriptId\n Name: $scriptName\n Description: $scriptDescription";
|
||||
}
|
||||
|
||||
//
|
||||
// Delete previously created Script
|
||||
//
|
||||
print "\n------- Delete -------\n";
|
||||
$scriptsApi->deleteScript($createdScript->getId());
|
||||
print "Successfully deleted script: '" . $createdScript->getName();
|
||||
|
||||
$client->close();
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Shows how to use parameterized queries
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
use InfluxDB2\Model\Query;
|
||||
use InfluxDB2\Point;
|
||||
use InfluxDB2\WriteType as WriteType;
|
||||
|
||||
$url = "https://us-west-2-1.aws.cloud2.influxdata.com";
|
||||
$organization = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
$client = new Client([
|
||||
"url" => $url,
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $organization,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::NS,
|
||||
"debug" => false
|
||||
]);
|
||||
|
||||
$writeApi = $client->createWriteApi(["writeType" => WriteType::SYNCHRONOUS]);
|
||||
$queryApi = $client->createQueryApi();
|
||||
|
||||
$today = new DateTime("now");
|
||||
$yesterday = $today->sub(new DateInterval("P1D"));
|
||||
|
||||
$p = new Point("temperature");
|
||||
$p->addTag("location", "north")->addField("value", 60)->time($yesterday);
|
||||
$writeApi->write($p);
|
||||
$writeApi->close();
|
||||
|
||||
//
|
||||
// Query range start parameter using duration
|
||||
//
|
||||
$parameterizedQuery = "from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam))";
|
||||
$query = new Query();
|
||||
$query->setQuery($parameterizedQuery);
|
||||
$query->setParams(["bucketParam" => "my-bucket", "startParam" => "-1d"]);
|
||||
$tables = $queryApi->query($query);
|
||||
|
||||
foreach ($tables as $table) {
|
||||
foreach ($table->records as $record) {
|
||||
var_export($record->values);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Query range start parameter using DateTime
|
||||
//
|
||||
$parameterizedQuery = "from(bucket: params.bucketParam) |> range(start: time(v: params.startParam))";
|
||||
$query->setParams(["bucketParam" => "my-bucket", "startParam" => $yesterday]);
|
||||
$query->setQuery($parameterizedQuery);
|
||||
$tables = $queryApi->query($query);
|
||||
|
||||
foreach ($tables as $table) {
|
||||
foreach ($table->records as $record) {
|
||||
var_export($record->values);
|
||||
}
|
||||
}
|
||||
|
||||
$client->close();
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Shows how to query data into `FluxTable`
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
use InfluxDB2\Model\DeletePredicateRequest;
|
||||
use InfluxDB2\Point;
|
||||
use InfluxDB2\Service\DeleteService;
|
||||
|
||||
$org = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "http://localhost:8086",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $org,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Delete data from influxDB
|
||||
//
|
||||
$service = $client->createService(DeleteService::class);
|
||||
|
||||
$predicate = new DeletePredicateRequest();
|
||||
$predicate->setStart(DateTime::createFromFormat('Y', '1900'));
|
||||
$predicate->setStop(new DateTime());
|
||||
$predicate->setPredicate("_measurement=\"weather\"");
|
||||
$service->postDelete($predicate, null, $org, $bucket);
|
||||
|
||||
//
|
||||
// Write test data into InfluxDB
|
||||
//
|
||||
$writeApi = $client->createWriteApi();
|
||||
$pointArray = [];
|
||||
|
||||
$dateNow = new DateTime('NOW');
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$point = Point::measurement("weather")
|
||||
->addTag("location", "London")
|
||||
->addField("temperature", rand(-5, 20))
|
||||
->time($dateNow->getTimestamp());
|
||||
$pointArray[] = $point;
|
||||
$dateNow->sub(new DateInterval('P1D'));
|
||||
}
|
||||
|
||||
$writeApi->write($pointArray);
|
||||
$writeApi->close();
|
||||
|
||||
//
|
||||
// Get query client
|
||||
//
|
||||
$queryApi = $client->createQueryApi();
|
||||
|
||||
//
|
||||
// Synchronously executes query and return result as an Array of FluxTables
|
||||
//
|
||||
$result = $queryApi->query(
|
||||
'from(bucket: "my-bucket")
|
||||
|> range(start: -8d)
|
||||
|> filter(fn: (r) => r["_measurement"] == "weather")'
|
||||
);
|
||||
|
||||
//
|
||||
// Encoding to JSON with json_encode
|
||||
//
|
||||
printf("\n\n----------------------- Query (JsonEncode) -----------------------\n\n");
|
||||
echo json_encode($result, JSON_PRETTY_PRINT);
|
||||
|
||||
//
|
||||
// Working with returned data in FluxTables
|
||||
//
|
||||
printf("\n\n----------------------- Query (FluxTables) -----------------------\n\n");
|
||||
foreach ($result as $table) {
|
||||
foreach ($table->records as $record) {
|
||||
$location = $record["location"];
|
||||
$temperature = $record->getValue();
|
||||
try {
|
||||
$time = (new DateTime($record->getTime()))->format('d.m.Y');
|
||||
} catch (Exception $e) {
|
||||
$time = $record->getTime();
|
||||
}
|
||||
$measurement = $record->getMeasurement();
|
||||
print "$measurement in $location at $time - Temperature is $temperature °C\n";
|
||||
}
|
||||
}
|
||||
|
||||
$client->close();
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Shows how to use a Flux query defined in a separate file
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
use InfluxDB2\Point;
|
||||
|
||||
$org = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "http://localhost:8086",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $org,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Write test data into InfluxDB
|
||||
//
|
||||
$writeApi = $client->createWriteApi();
|
||||
$pointArray = [];
|
||||
|
||||
$dateNow = new DateTime('NOW');
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$point = Point::measurement("weather")
|
||||
->addTag("location", "Prague")
|
||||
->addField("temperature", rand(-5, 15))
|
||||
->time($dateNow->getTimestamp());
|
||||
$pointArray[] = $point;
|
||||
$dateNow->sub(new DateInterval('P1D'));
|
||||
}
|
||||
|
||||
$writeApi->write($pointArray);
|
||||
$writeApi->close();
|
||||
|
||||
//
|
||||
// Get query client
|
||||
//
|
||||
$queryApi = $client->createQueryApi();
|
||||
|
||||
//
|
||||
// Executes Flux query defined in a separate file
|
||||
//
|
||||
$filename = "query.flux";
|
||||
$handle = fopen($filename, "r");
|
||||
$contents = fread($handle, filesize($filename));
|
||||
fclose($handle);
|
||||
|
||||
$result = $queryApi->query($contents);
|
||||
|
||||
//
|
||||
// Working with returned data in FluxTables
|
||||
//
|
||||
printf("\n\n------------------------------- Query -------------------------------\n\n");
|
||||
foreach ($result as $table) {
|
||||
foreach ($table->records as $record) {
|
||||
$day = getDayName($record["weekDay"]);
|
||||
$location = $record["location"];
|
||||
$temperature = $record["temperature"];
|
||||
$measurement = $record->getMeasurement();
|
||||
print "Temperature in $location at $day is $temperature °C\n";
|
||||
}
|
||||
}
|
||||
|
||||
function getDayName(int $weekDay): string
|
||||
{
|
||||
switch ($weekDay) {
|
||||
case "1":
|
||||
return "Monday";
|
||||
case "2":
|
||||
return "Tuesday";
|
||||
case "3":
|
||||
return "Wednesday";
|
||||
case "4":
|
||||
return "Thursday";
|
||||
case "5":
|
||||
return "Friday";
|
||||
case "6":
|
||||
return "Saturday";
|
||||
case "0":
|
||||
return "Sunday";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
$client->close();
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Shows how to query data with profilers
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
use InfluxDB2\Point;
|
||||
|
||||
$org = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "http://localhost:8086",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $org,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Write test data into InfluxDB
|
||||
//
|
||||
$writeApi = $client->createWriteApi();
|
||||
$pointArray = [];
|
||||
|
||||
$dateNow = new DateTime('NOW');
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$point = Point::measurement("weather")
|
||||
->addTag("location", "New York")
|
||||
->addField("temperature", rand(10, 25))
|
||||
->time($dateNow->getTimestamp());
|
||||
$pointArray[] = $point;
|
||||
$dateNow->sub(new DateInterval('P1D'));
|
||||
}
|
||||
|
||||
$writeApi->write($pointArray);
|
||||
$writeApi->close();
|
||||
|
||||
//
|
||||
// Executes query with profilers and return result as an Array of FluxTables
|
||||
//
|
||||
$queryApi = $client->createQueryApi();
|
||||
$query = "import \"profiler\"
|
||||
option profiler.enabledProfilers = [\"query\", \"operator\"]
|
||||
|
||||
from(bucket: \"my-bucket\")
|
||||
|> range(start: 0)
|
||||
|> filter(fn: (r) => r[\"_measurement\"] == \"weather\"
|
||||
and r[\"_field\"] == \"temperature\"
|
||||
and r[\"location\"] == \"New York\")";
|
||||
|
||||
$result = $queryApi->query($query);
|
||||
|
||||
printf("\n\n----------------------------- query result -------------------------------\n\n");
|
||||
foreach ($result as $table) {
|
||||
foreach ($table->records as $record) {
|
||||
$measurement = $record->getMeasurement();
|
||||
|
||||
// Get Query profiler
|
||||
if (strpos($measurement, 'profiler/query') !== false) {
|
||||
$totalDuration = $record["TotalDuration"];
|
||||
$queueDuration = $record["QueueDuration"];
|
||||
$compileDuration = $record["CompileDuration"];
|
||||
$executeDuration = $record["ExecuteDuration"];
|
||||
|
||||
print"\n---------------------------- $measurement -----------------------------\n
|
||||
Total Duration: $totalDuration
|
||||
Queue Duration: $queueDuration
|
||||
Compile Duration: $compileDuration
|
||||
Execute Duration: $executeDuration\n";
|
||||
}
|
||||
// Get Operator profiler
|
||||
elseif (strpos($measurement, 'profiler/operator') !== false) {
|
||||
$minDuration = $record["MinDuration"];
|
||||
$maxDuration = $record["MaxDuration"];
|
||||
$durationSum = $record["DurationSum"];
|
||||
$meanDuration = $record["MeanDuration"];
|
||||
|
||||
print"\n-------------------------- $measurement ----------------------------\n
|
||||
Min Duration: $minDuration
|
||||
Max Duration: $maxDuration
|
||||
Duration Sum: $durationSum
|
||||
Mean Duration: $meanDuration\n";
|
||||
}
|
||||
//Get query data
|
||||
else {
|
||||
$location = $record["location"];
|
||||
$temperature = $record->getValue();
|
||||
$time = $record->getTime();
|
||||
$measurement = $record->getMeasurement();
|
||||
print "\t$measurement in $location at $time - Temperature is $temperature °C\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$client->close();
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Shows how to query data into `string`
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
use InfluxDB2\Point;
|
||||
|
||||
$org = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "http://localhost:8086",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $org,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Write test data into InfluxDB
|
||||
//
|
||||
$writeApi = $client->createWriteApi();
|
||||
$pointArray = [];
|
||||
|
||||
$dateNow = new DateTime('NOW');
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$point = Point::measurement("weather")
|
||||
->addTag("location", "Sydney")
|
||||
->addField("temperature", rand(15, 30))
|
||||
->time($dateNow->getTimestamp());
|
||||
$pointArray[] = $point;
|
||||
$dateNow->sub(new DateInterval('P1D'));
|
||||
}
|
||||
|
||||
$writeApi->write($pointArray);
|
||||
$writeApi->close();
|
||||
|
||||
//
|
||||
// Get query client
|
||||
//
|
||||
$queryApi = $client->createQueryApi();
|
||||
|
||||
//
|
||||
// Synchronously executes query and return result as unprocessed String
|
||||
//
|
||||
$result = $queryApi->queryRaw(
|
||||
"from(bucket: \"my-bucket\")
|
||||
|> range(start: 0)
|
||||
|> filter(fn: (r) => r[\"_measurement\"] == \"weather\"
|
||||
and r[\"_field\"] == \"temperature\"
|
||||
and r[\"location\"] == \"Sydney\")"
|
||||
);
|
||||
|
||||
printf("\n\n-------------------------- Query Raw ----------------------------\n\n");
|
||||
printf($result);
|
||||
|
||||
$client->close();
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Shows how to query data into `Stream`
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
use InfluxDB2\Point;
|
||||
|
||||
$org = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "http://localhost:8086",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $org,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Write test data into InfluxDB
|
||||
//
|
||||
$writeApi = $client->createWriteApi();
|
||||
$pointArray = [];
|
||||
|
||||
$dateNow = new DateTime('NOW');
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$point = Point::measurement("weather")
|
||||
->addTag("location", "San Francisco")
|
||||
->addField("temperature", rand(5, 25))
|
||||
->time($dateNow->getTimestamp());
|
||||
$pointArray[] = $point;
|
||||
$dateNow->sub(new DateInterval('P1D'));
|
||||
}
|
||||
|
||||
$writeApi->write($pointArray);
|
||||
$writeApi->close();
|
||||
|
||||
//
|
||||
// Get query client
|
||||
//
|
||||
$queryApi = $client->createQueryApi();
|
||||
|
||||
//
|
||||
// Synchronously executes the Flux query and return stream of FluxRecord
|
||||
//
|
||||
$queryApi = $client->createQueryApi();
|
||||
$query = "from(bucket: \"my-bucket\")
|
||||
|> range(start: 0)
|
||||
|> filter(fn: (r) => r[\"_measurement\"] == \"weather\"
|
||||
and r[\"_field\"] == \"temperature\")";
|
||||
$result = $queryApi->queryStream($query);
|
||||
|
||||
//
|
||||
// Working with returned data into Stream
|
||||
//
|
||||
printf("\n\n----------------------------- Query Stream -------------------------------\n\n");
|
||||
foreach ($result->each() as $record) {
|
||||
$location = $record["location"];
|
||||
$temperature = $record->getValue();
|
||||
$time = $record->getTime();
|
||||
$measurement = $record->getMeasurement();
|
||||
print " $measurement in $location at $time - Temperature is $temperature °C\n";
|
||||
}
|
||||
|
||||
$client->close();
|
||||
@@ -0,0 +1,24 @@
|
||||
# Examples
|
||||
|
||||
## Writes
|
||||
- [WriteExample.php](WriteExample.php) - How to write data to InfluxDB via default API using `Point` structure or line protocol
|
||||
- [WriteBatchingExample.php](WriteBatchingExample.php) - How to use batching for more performance writes
|
||||
- [WriteUDPExample.php](WriteUDPExample.php) - How to write data to InfluxDB via UDP
|
||||
|
||||
## Queries
|
||||
- [QueryRawExample.php](QueryRawExample.php) - How to query data into `string`
|
||||
- [QueryExample.php](QueryExample.php) - How to query data into `FluxTable` and encode results into JSON
|
||||
- [QueryStreamExample.php](QueryStreamExample.php) - How to query data into `Stream`
|
||||
- [QueryProfilerExample.php](QueryProfilerExample.php) - How to query data with profilers
|
||||
- [QueryFromFileExample.php](QueryFromFileExample.php) - How to use a Flux query defined in a separate file
|
||||
- [ParameterizedQuery.php](ParameterizedQuery.php) - How to use parameterized Flux queries
|
||||
|
||||
## Management API
|
||||
- [BucketManagementExample.php](BucketManagementExample.php) - How to create, list and delete Buckets
|
||||
|
||||
## Others
|
||||
- [InfluxDB_18_Example.php](InfluxDB_18_Example.php) - How to use forward compatibility APIs from InfluxDB 1.8
|
||||
- [DeleteDataExample.php](DeleteDataExample.php) - How to delete data from InfluxDB by client
|
||||
- [InvokableScripts.php](InvokableScripts.php) - How to use Invokable scripts Cloud API to create custom endpoints that query data
|
||||
- [RecordRowExample.php](RecordRowExample.php) - How to use `FluxRecord.row` instead of `FluxRecord.values`,
|
||||
in case of duplicity column names
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* How to use `FluxRecord.row` instead of `FluxRecord.values`
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
|
||||
$org = 'my-org';
|
||||
$bucket = 'my-bucket';
|
||||
$token = 'my-token';
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "http://localhost:8086",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $org,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Write test data into InfluxDB
|
||||
//
|
||||
$writeApi = $client->createWriteApi();
|
||||
|
||||
foreach (range(1, 5) as $i) {
|
||||
$writeApi->write("point,table=my-table result=$i", InfluxDB2\Model\WritePrecision::MS, $bucket, $org);
|
||||
}
|
||||
|
||||
$writeApi->close();
|
||||
|
||||
//
|
||||
// Query data with pivot
|
||||
//
|
||||
$queryApi = $client->createQueryApi();
|
||||
|
||||
$result = $queryApi->query(
|
||||
"from(bucket: \"$bucket\") |> range(start: -1m) |> filter(fn: (r) => (r[\"_measurement\"] == \"point\"))
|
||||
|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")"
|
||||
);
|
||||
|
||||
//
|
||||
// Write data to output
|
||||
//
|
||||
printf("\n--------------------------------------- FluxRecord.values ----------------------------------------\n");
|
||||
foreach ($result as $table) {
|
||||
foreach ($table->records as $record) {
|
||||
$values = implode(", ", $record->values);
|
||||
print "$values\n";
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n----------------------------------------- FluxRecord.row -----------------------------------------\n");
|
||||
foreach ($result as $table) {
|
||||
foreach ($table->records as $record) {
|
||||
$row = implode(", ", $record->row);
|
||||
print "$row\n";
|
||||
}
|
||||
}
|
||||
|
||||
$client->close();
|
||||
@@ -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:8086",
|
||||
"token" => $token,
|
||||
"bucket" => $bucket,
|
||||
"org" => $organization,
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
print "*** Write by batching ***\n";
|
||||
|
||||
$writeApi = $client->createWriteApi(
|
||||
["writeType" => WriteType::BATCHING, 'batchSize' => 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();
|
||||
@@ -0,0 +1,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();
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Shows how to write data to InfluxDB via UDP
|
||||
*
|
||||
* UDP Writer Requirements:
|
||||
* 1. Installed ext-sockets
|
||||
* 2. Since Influxdb 2.0+ does not support UDP protocol natively you need to install and configure
|
||||
* Telegraf plugin: https://docs.influxdata.com/telegraf/v1.16/plugins/#socket_listener
|
||||
* 3. Extra config option passed to client: udpPort. Optionally you can specify udpHost,
|
||||
* otherwise udpHost will parse from url option
|
||||
* 4. Extra config option passed to client: ipVersion. Optionally you can specify the ip version,
|
||||
* defaults to IPv4
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use InfluxDB2\Client;
|
||||
|
||||
//
|
||||
// Creating client
|
||||
//
|
||||
$client = new Client([
|
||||
"url" => "http://localhost:8086",
|
||||
"token" => "my-token",
|
||||
"bucket" => "my-bucket",
|
||||
"org" => "my-org",
|
||||
"precision" => InfluxDB2\Model\WritePrecision::S
|
||||
]);
|
||||
|
||||
//
|
||||
// Write data to InfluxDB via UDP Writer
|
||||
//
|
||||
try {
|
||||
$seconds = time();
|
||||
$temp = rand(5, 25)."i";
|
||||
|
||||
$writeApi = $client->createUdpWriter();
|
||||
$writeApi->write("weather,location=Tokio temperature=$temp $seconds");
|
||||
$writeApi->close();
|
||||
|
||||
//
|
||||
// Check result
|
||||
//
|
||||
$query = "from(bucket: \"my-bucket\")
|
||||
|> range(start: 0)
|
||||
|> filter(fn: (r) => r[\"location\"] == \"Tokio\")";
|
||||
$queryApi = $client->createQueryApi();
|
||||
$result = $queryApi->query($query);
|
||||
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";
|
||||
}
|
||||
}
|
||||
} catch (Exception|Throwable $e) {
|
||||
print "\n\n $e \n\n";
|
||||
}
|
||||
|
||||
$client->close();
|
||||
@@ -0,0 +1,7 @@
|
||||
import "date"
|
||||
|
||||
from(bucket: "my-bucket")
|
||||
|> range(start: -10d)
|
||||
|> filter(fn: (r) => r["_measurement"] == "weather" and r["_field"] == "temperature" and r["location"] == "Prague")
|
||||
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|
||||
|> map(fn: (r) => ({ r with weekDay: date.weekDay(t: r._time) }))
|
||||
Reference in New Issue
Block a user