InfluxDB
The Alpakka InfluxDb connector provides Akka Streams integration for InfluxDB.
For more information about InfluxDB, please visit the InfluxDB Documentation
[+] Show project infoProject Info: Alpakka InfluxDB | |
---|---|
Artifact | com.lightbend.akka
akka-stream-alpakka-influxdb
9.0.1
|
JDK versions | Eclipse Temurin JDK 11 Eclipse Temurin JDK 17 |
Scala versions | 2.13.12, 3.3.4 |
JPMS module name | akka.stream.alpakka.influxdb |
License | |
Readiness level |
Since 1.1.0, 2019-07-03
|
Home page | https://doc.akka.io/libraries/alpakka/current |
API documentation | |
Forums | |
Release notes | GitHub releases |
Issues | Github issues |
Sources | https://github.com/akka/alpakka |
Influxdata, the makers of InfluxDB now offer an Akka Streams-aware client library in https://github.com/influxdata/influxdb-client-java/tree/master/client-scala
“The reference Scala client that allows query and write for the InfluxDB 2.0 by Akka Streams.”
Alpakka InfluxDB was added in Alpakka 1.1.0 in July 2019 and is marked as “API may change”. Please try it out and suggest improvements.
Furthermore, the major InfluxDB update to version 2.0 is expected to bring API and dependency changes to Alpakka InfluxDB.
Artifacts
The Akka dependencies are available from Akka’s library repository. To access them there, you need to configure the URL for this repository.
Additionally, add the dependencies as below.
- sbt
val AkkaVersion = "2.10.0" libraryDependencies ++= Seq( "com.lightbend.akka" %% "akka-stream-alpakka-influxdb" % "9.0.1", "com.typesafe.akka" %% "akka-stream" % AkkaVersion )
- Maven
- Gradle
The table below shows direct dependencies of this module and the second tab shows all libraries it depends on transitively.
- Direct dependencies
Organization Artifact Version com.typesafe.akka akka-stream_2.13 2.10.0 org.influxdb influxdb-java 2.15 org.scala-lang scala-library 2.13.12 - Dependency tree
Set up InfluxDB client
Sources, Flows and Sinks provided by this connector need a prepared org.influxdb.InfluxDB
to access to InfluxDB.
- Scala
-
source
influxDB = InfluxDBFactory.connect(INFLUXDB_URL, USERNAME, PASSWORD); influxDB.setDatabase(DatabaseName); influxDB.query(new Query("CREATE DATABASE " + DatabaseName, DatabaseName));
- Java
InfluxDB as Source and Sink
Now we can stream messages from or to InfluxDB by providing the InfluxDB
to the InfluxDbSource
or the InfluxDbSink
.
- Scala
-
source
@Measurement(name = "cpu", database = "InfluxDbSpec") public class InfluxDbSpecCpu extends Cpu { public InfluxDbSpecCpu() { } public InfluxDbSpecCpu(Instant time, String hostname, String region, Double idle, Boolean happydevop, Long uptimeSecs) { super(time, hostname, region, idle, happydevop, uptimeSecs); } public InfluxDbSpecCpu cloneAt(Instant time) { return new InfluxDbSpecCpu(time, getHostname(), getRegion(), getIdle(), getHappydevop(), getUptimeSecs()); } }
- Java
With typed source
Use InfluxDbSource.typed
and InfluxDbSink.typed
to create source and sink. The data is converted by InfluxDBMapper.
- Scala
-
source
val f1 = InfluxDbSource .typed(classOf[InfluxDbSpecCpu], InfluxDbReadSettings(), influxDB, query) .map { (cpu: InfluxDbSpecCpu) => { val clonedCpu = cpu.cloneAt(cpu.getTime.plusSeconds(60000)) List(InfluxDbWriteMessage(clonedCpu)) } } .runWith(InfluxDbSink.typed(classOf[InfluxDbSpecCpu]))
- Java
With QueryResult
source
Use InfluxDbSource.create
and InfluxDbSink.create
to create source and sink.
- Scala
-
source
val query = new Query("SELECT * FROM cpu", DatabaseName); val f1 = InfluxDbSource(influxDB, query) .map(resultToPoints) .runWith(InfluxDbSink.create())
- Java
TODO
Writing to InfluxDB
You can also build flow stages. InfluxDbFlow
. The API is similar to creating Sinks.
- Scala
-
source
val result = Source( List( List(validMessage) ) ).via(InfluxDbFlow.create()) .runWith(Sink.seq) .futureValue
- Java
Passing data through InfluxDbFlow
When streaming documents from Kafka, you might want to commit to Kafka AFTER the document has been written to InfluxDB.
- Scala
-
source
// We're going to pretend we got messages from kafka. // After we've written them to InfluxDB, we want // to commit the offset to Kafka case class KafkaOffset(offset: Int) case class KafkaMessage(cpu: InfluxDbFlowCpu, offset: KafkaOffset) val messagesFromKafka = List( KafkaMessage(new InfluxDbFlowCpu(Instant.now().minusSeconds(1000), "local_1", "eu-west-2", 1.4d, true, 123L), KafkaOffset(0)), KafkaMessage(new InfluxDbFlowCpu(Instant.now().minusSeconds(2000), "local_2", "eu-west-1", 2.5d, false, 125L), KafkaOffset(1)), KafkaMessage(new InfluxDbFlowCpu(Instant.now().minusSeconds(3000), "local_3", "eu-west-4", 3.1d, false, 251L), KafkaOffset(2)) ) var committedOffsets = List[KafkaOffset]() def commitToKafka(offset: KafkaOffset): Unit = committedOffsets = committedOffsets :+ offset val f1 = Source(messagesFromKafka) .map { (kafkaMessage: KafkaMessage) => val cpu = kafkaMessage.cpu println("hostname: " + cpu.getHostname) InfluxDbWriteMessage(cpu).withPassThrough(kafkaMessage.offset) } .groupedWithin(10, 50.millis) .via( InfluxDbFlow.typedWithPassThrough(classOf[InfluxDbFlowCpu]) ) .map { (messages: Seq[InfluxDbWriteResult[InfluxDbFlowCpu, KafkaOffset]]) => messages.foreach { message => commitToKafka(message.writeMessage.passThrough) } } .runWith(Sink.ignore)
- Java