forked from influxdata/influxdb-client-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformExample.cs
More file actions
132 lines (108 loc) · 4.62 KB
/
PlatformExample.cs
File metadata and controls
132 lines (108 loc) · 4.62 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;
using Task = System.Threading.Tasks.Task;
namespace Examples
{
public static class PlatformExample
{
[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 override string ToString()
{
return $"{Time:MM/dd/yyyy hh:mm:ss.fff tt} {Location} value: {Value}";
}
}
public static async Task Main(string[] args)
{
var influxDB = InfluxDBClientFactory.Create("http://localhost:9999",
"my-user", "my-password".ToCharArray());
var organizationClient = influxDB.GetOrganizationsApi();
var medicalGMBH = await organizationClient
.CreateOrganizationAsync("Medical Corp " +
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture));
//
// Create New Bucket with retention 1h
//
var temperatureBucket =
await influxDB.GetBucketsApi().CreateBucketAsync("temperature-sensors", medicalGMBH.Id);
//
// Add Permissions to read and write to the Bucket
//
var resource = new PermissionResource
{ Type = PermissionResource.TypeBuckets, OrgID = medicalGMBH.Id, Id = temperatureBucket.Id };
var readBucket = new Permission(Permission.ActionEnum.Read, resource);
var writeBucket = new Permission(Permission.ActionEnum.Write, resource);
var authorization = await influxDB.GetAuthorizationsApi()
.CreateAuthorizationAsync(medicalGMBH, new List<Permission> { readBucket, writeBucket });
Console.WriteLine($"The token to write to temperature-sensors bucket is: {authorization.Token}");
influxDB.Dispose();
//
// Create new client with specified authorization token
//
influxDB = InfluxDBClientFactory.Create("http://localhost:9999", authorization.Token);
var writeOptions = WriteOptions
.CreateNew()
.BatchSize(5000)
.FlushInterval(1000)
.JitterInterval(1000)
.RetryInterval(5000)
.Build();
//
// Write data
//
using (var writeClient = influxDB.GetWriteApi(writeOptions))
{
//
// Write by POCO
//
var temperature = new Temperature { Location = "south", Value = 62D, Time = DateTime.UtcNow };
writeClient.WriteMeasurement(temperature, WritePrecision.Ns, "temperature-sensors", medicalGMBH.Id);
//
// Write by Point
//
var point = PointData.Measurement("temperature")
.Tag("location", "west")
.Field("value", 55D)
.Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);
writeClient.WritePoint(point, "temperature-sensors", medicalGMBH.Id);
//
// Write by LineProtocol
//
var record = "temperature,location=north value=60.0";
writeClient.WriteRecord(record, WritePrecision.Ns, "temperature-sensors", medicalGMBH.Id);
writeClient.Flush();
Thread.Sleep(2000);
}
//
// Read data
//
var fluxTables = await influxDB.GetQueryApi()
.QueryAsync("from(bucket:\"temperature-sensors\") |> range(start: 0)", medicalGMBH.Id);
fluxTables.ForEach(fluxTable =>
{
var fluxRecords = fluxTable.Records;
fluxRecords.ForEach(fluxRecord =>
{
Console.WriteLine($"{fluxRecord.GetTime()}: {fluxRecord.GetValue()}");
});
});
//
// Delete data
//
await influxDB.GetDeleteApi().Delete(DateTime.UtcNow.AddMinutes(-1), DateTime.Now, "", temperatureBucket,
medicalGMBH);
influxDB.Dispose();
}
}
}