-
Notifications
You must be signed in to change notification settings - Fork 128
/
ScalaWriteApi.scala
94 lines (80 loc) · 3.1 KB
/
ScalaWriteApi.scala
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
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package example
import akka.actor.ActorSystem
import akka.stream.scaladsl.{Keep, Source}
import com.influxdb.annotations.{Column, Measurement}
import com.influxdb.client.domain.WritePrecision
import com.influxdb.client.scala.InfluxDBClientScalaFactory
import com.influxdb.client.write.Point
import java.time.Instant
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object ScalaWriteApi {
implicit val system: ActorSystem = ActorSystem("examples")
def main(args: Array[String]): Unit = {
val client = InfluxDBClientScalaFactory.create(
"http://localhost:8086", "my-token".toCharArray, "my-org", "my-bucket")
//
// Use InfluxDB Line Protocol to write data
//
val record = "mem,host=host1 used_percent=23.43234543"
val source = Source.single(record)
val sink = client.getWriteScalaApi.writeRecord()
val materialized = source.toMat(sink)(Keep.right)
Await.result(materialized.run(), Duration.Inf)
//
// Use a Data Point to write data
//
val point = Point
.measurement("mem")
.addTag("host", "host1")
.addField("used_percent", 23.43234543)
.time(Instant.now(), WritePrecision.NS)
val sourcePoint = Source.single(point)
val sinkPoint = client.getWriteScalaApi.writePoint()
val materializedPoint = sourcePoint.toMat(sinkPoint)(Keep.right)
Await.result(materializedPoint.run(), Duration.Inf)
//
// Use POJO and corresponding class to write data
//
val mem = new Mem()
mem.host = "host1"
mem.used_percent = 23.43234543
mem.time = Instant.now
val sourcePOJO = Source.single(mem)
val sinkPOJO = client.getWriteScalaApi.writeMeasurement()
val materializedPOJO = sourcePOJO.toMat(sinkPOJO)(Keep.right)
Await.result(materializedPOJO.run(), Duration.Inf)
client.close()
system.terminate()
}
@Measurement(name = "mem")
class Mem() {
@Column(tag = true)
var host: String = _
@Column
var used_percent: Double = _
@Column(timestamp = true)
var time: Instant = _
}
}