const {InfluxDB, flux} = require('@influxdata/influxdb-client')
// const url = "https://us-west-2-1.aws.cloud2.influxdata.com";
const url = "http://localhost:9999";
const token = 'my-token'
const org = 'my-org'
const bucket = 'my-bucket'
const client = new InfluxDB({url: url, token: token})
const queryApi = client.getQueryApi(org)
const query = flux`from(bucket: "${bucket}")
|> range(start: -1d)
|> filter(fn: (r) => r._measurement == "weatherstation")`
queryApi.queryRows(query, {
next(row, tableMeta) {
const o = tableMeta.toObject(row)
console.log(`${o._time} ${o._measurement}: ${o._field}=${o._value}`)
},
error(error) {
console.error(error)
console.log('Finished ERROR')
},
complete() {
console.log('Finished SUCCESS')
},
})
c
const { InfluxDB, Point } = require("@influxdata/influxdb-client");
// const url = "https://us-west-2-1.aws.cloud2.influxdata.com";
const url = "http://localhost:9999";
const token = "my-token";
const org = "my-org";
const bucket = "my-bucket";
const client = new InfluxDB({ url: url, token: token });
const writeApi = client.getWriteApi(org, bucket);
const point = new Point("weatherstation")
.tag("location", "San Francisco")
.floatField("temperature", 23.4)
.timestamp(new Date());
writeApi.writePoint(point);
writeApi
.close()
.then(() => {
console.log("FINISHED");
})
.catch((e) => {
console.error(e);
console.log("Finished ERROR");
});
c
from influxdb_client import InfluxDBClient
url = 'https://us-west-2-1.aws.cloud2.influxdata.com'
token = 'my-token'
org = 'my-org'
bucket = 'my-bucket'
with InfluxDBClient(url=url, token=token, org=org) as client:
query_api = client.query_api()
tables = query_api.query('from(bucket: "my-bucket") |> range(start: -1d)')
for table in tables:
for record in table.records:
print(str(record["_time"]) + " - " + record.get_measurement()
+ " " + record.get_field() + "=" + str(record.get_value()))
c
from datetime import datetime
from influxdb_client import WritePrecision, InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS
url = 'https://us-west-2-1.aws.cloud2.influxdata.com'
token = 'my-token'
org = 'my-org'
bucket = 'my-bucket'
with InfluxDBClient(url=url, token=token, org=org) as client:
p = Point("weatherstation") \
.tag("location", "San Francisco") \
.field("temperature", 25.9) \
.time(datetime.utcnow(), WritePrecision.MS)
with client.write_api(write_options=SYNCHRONOUS) as write_api:
write_api.write(bucket=bucket, record=p)
c
package main
import (
"context"
"fmt"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)
func main() {
url := "https://us-west-2-1.aws.cloud2.influxdata.com"
token := "my-token"
org := "my-org"
bucket := "my-bucket"
client := influxdb2.NewClient(url, token)
queryAPI := client.QueryAPI(org)
query := fmt.Sprintf(`from(bucket: "%v") |> range(start: -1d)`, bucket)
result, err := queryAPI.Query(context.Background(), query)
if err != nil {
panic(err)
}
for result.Next() {
record := result.Record()
fmt.Printf("%v %v: %v=%v\n", record.Time(), record.Measurement(), record.Field(), record.Value())
}
client.Close()
}
c
package main
import (
"context"
"time"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)
func main() {
url := "https://us-west-2-1.aws.cloud2.influxdata.com"
token := "my-token"
org := "my-org"
bucket := "my-bucket"
client := influxdb2.NewClient(url, token)
writeAPI := client.WriteAPIBlocking(org, bucket)
p := influxdb2.NewPointWithMeasurement("weatherstation").
AddTag("location", "San Francisco").
AddField("temperature", 25.7).
SetTime(time.Now())
err := writeAPI.WritePoint(context.Background(), p)
if err != nil {
panic(err)
}
client.Close()
}
c
<?php
require __DIR__ . '/vendor/autoload.php';
use InfluxDB2\Client;
use InfluxDB2\Model\WritePrecision;
$url = "https://us-west-2-1.aws.cloud2.influxdata.com";
$token = "my-token";
$org = "my-org";
$bucket = "my-bucket";
$client = new Client(["url" => $url, "token" => $token, "org" => $org, "precision" => WritePrecision::S]);
$queryApi = $client->createQueryApi();
$query = "from(bucket: \"$bucket\") |> range(start: -1d)";
$result = $queryApi->query($query);
foreach ($result as $table) {
foreach ($table->records as $record) {
$measurement = $record->getMeasurement();
$field = $record->getField();
$time = $record->getTime();
$value = $record->getValue();
print "$time $measurement: $field=$value\n";
}
}
$client->close();
c
<?php
require __DIR__ . '/vendor/autoload.php';
use InfluxDB2\Client;
use InfluxDB2\Model\WritePrecision;
use InfluxDB2\Point;
$url = "https://us-west-2-1.aws.cloud2.influxdata.com";
$token = "my-token";
$org = "my-org";
$bucket = "my-bucket";
$client = new Client(["url" => $url, "token" => $token, "org" => $org,
"bucket" => $bucket, "precision" => WritePrecision::S]);
$writeApi = $client->createWriteApi();
$point = Point::measurement("weatherstation")
->addTag("location", "San Francisco")
->addField("temperature", 25.1)
->time(time(), WritePrecision::S);
$writeApi->write($point);
$client->close();
c
using System;
using System.Threading.Tasks;
using InfluxDB.Client;
namespace Examples
{
public class QueryExample
{
public static async Task Main(string[] args)
{
var url = "https://us-west-2-1.aws.cloud2.influxdata.com";
var token = "my-token";
var org = "my-org";
var bucket = "my-bucket";
using var client = InfluxDBClientFactory.Create(url, token);
var queryApi = client.GetQueryApi();
var query = $"from(bucket:\"{bucket}\") |> range(start: -1d)";
var tables = await queryApi.QueryAsync(query, org);
tables.ForEach(table =>
{
table.Records.ForEach(record =>
{
Console.WriteLine(
$"{record.GetTime()} {record.GetMeasurement()}: {record.GetField()}={record.GetValue()}");
});
});
}
}
}
c
using System;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
namespace Examples
{
public class WriteExample
{
public static async Task Main(string[] args)
{
var url = "https://us-west-2-1.aws.cloud2.influxdata.com";
var token = "my-token";
var org = "my-org";
var bucket = "bucket";
using var client = InfluxDBClientFactory.Create(url, token);
var writeApi = client.GetWriteApiAsync();
var point = PointData.Measurement("weatherstation")
.Tag("location", "San Francisco")
.Field("temperature", 25.5)
.Timestamp(DateTime.UtcNow, WritePrecision.Ns);
await writeApi.WritePointAsync(point, bucket, org);
}
}
}
c