-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathWriteApiAsyncExample.cs
70 lines (58 loc) · 2.14 KB
/
WriteApiAsyncExample.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
66
67
68
69
70
using System;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;
namespace Examples
{
public static class WriteApiAsyncExample
{
[Measurement("temperature")]
private class Temperature
{
[Column("location", IsTag = true)] public string Location { get; set; }
[Column("value")] public double Value { get; set; }
[Column(IsTimestamp = true)] public DateTime Time { get; set; }
}
public static async Task Main()
{
using var client = new InfluxDBClient("http://localhost:9999",
"my-user", "my-password");
//
// Write Data
//
var writeApiAsync = client.GetWriteApiAsync();
//
//
// Write by LineProtocol
//
await writeApiAsync.WriteRecordAsync("temperature,location=north value=60.0", WritePrecision.Ns,
"my-bucket", "my-org");
//
//
// Write by Data Point
//
var point = PointData.Measurement("temperature")
.Tag("location", "west")
.Field("value", 55D)
.Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);
await writeApiAsync.WritePointAsync(point, "my-bucket", "my-org");
//
// Write by POCO
//
var temperature = new Temperature { Location = "south", Value = 62D, Time = DateTime.UtcNow };
await writeApiAsync.WriteMeasurementAsync(temperature, WritePrecision.Ns, "my-bucket", "my-org");
//
// Check written data
//
var tables = await client.GetQueryApi()
.QueryAsync("from(bucket:\"my-bucket\") |> range(start: 0)", "my-org");
tables.ForEach(table =>
{
var fluxRecords = table.Records;
fluxRecords.ForEach(record => { Console.WriteLine($"{record.GetTime()}: {record.GetValue()}"); });
});
}
}
}