-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathParametrizedQuery.cs
65 lines (55 loc) · 2.08 KB
/
ParametrizedQuery.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
namespace Examples
{
/// <summary>
/// Parameterized Queries are supported only in InfluxDB Cloud, currently there is no support in InfluxDB OSS.
/// </summary>
public static class ParametrizedQuery
{
private const string Url = "https://us-west-2-1.aws.cloud2.influxdata.com";
private const string Token = "my-token";
private const string Org = "my-org";
private const string Bucket = "my-bucket";
public static async Task Main()
{
var options = new InfluxDBClientOptions(Url)
{
Token = Token,
Org = Org,
Bucket = Bucket
};
using var client = new InfluxDBClient(options);
//
// Prepare Data
//
Console.WriteLine("*** Write Points ***");
var point = PointData.Measurement("mem")
.Tag("location", "Prague")
.Field("temperature", 21.5);
await client.GetWriteApiAsync().WritePointAsync(point);
Console.WriteLine($"{point.ToLineProtocol()}");
//
// Query Data
//
Console.WriteLine("*** Query Points ***");
var query = "from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam))";
var bindParams = new Dictionary<string, object>
{
{ "bucketParam", Bucket },
{ "startParam", "-1h" }
};
var tables = await client.GetQueryApi()
.QueryAsync(new Query(query: query, _params: bindParams, dialect: QueryApi.Dialect));
// print results
foreach (var record in tables.SelectMany(table => table.Records))
Console.WriteLine(
$"{record.GetTime()} {record.GetMeasurement()}: {record.GetField()} {record.GetValue()}");
}
}
}