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
- Java
-
source
final InfluxDB influxDB = InfluxDBFactory.connect(INFLUXDB_URL, USERNAME, PASSWORD); influxDB.setDatabase(databaseName); influxDB.query(new Query("CREATE DATABASE " + databaseName, databaseName)); return influxDB;
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
- Java
-
source
@Measurement(name = "cpu", database = "InfluxDbTest") public class InfluxDbCpu extends Cpu { public InfluxDbCpu() {} public InfluxDbCpu( Instant time, String hostname, String region, Double idle, Boolean happydevop, Long uptimeSecs) { super(time, hostname, region, idle, happydevop, uptimeSecs); } public InfluxDbCpu cloneAt(Instant time) { return new InfluxDbCpu( time, getHostname(), getRegion(), getIdle(), getHappydevop(), getUptimeSecs()); } }
With typed source
Use InfluxDbSource.typed
and InfluxDbSink.typed
to create source and sink. The data is converted by InfluxDBMapper.
- Scala
- Java
-
source
CompletionStage<Done> completionStage = InfluxDbSource.typed(InfluxDbCpu.class, InfluxDbReadSettings.Default(), influxDB, query) .map( cpu -> { InfluxDbCpu clonedCpu = cpu.cloneAt(cpu.getTime().plusSeconds(60000l)); return InfluxDbWriteMessage.create(clonedCpu, NotUsed.notUsed()); }) .groupedWithin(10, Duration.of(50l, ChronoUnit.MILLIS)) .runWith(InfluxDbSink.typed(InfluxDbCpu.class, influxDB), system);
With QueryResult
source
Use InfluxDbSource.create
and InfluxDbSink.create
to create source and sink.
- Scala
- Java
-
source
Query query = new Query("SELECT * FROM cpu", DATABASE_NAME); CompletionStage<Done> completionStage = InfluxDbSource.create(influxDB, query) .map(queryResult -> points(queryResult)) .mapConcat(i -> i) .groupedWithin(10, Duration.of(50l, ChronoUnit.MILLIS)) .runWith(InfluxDbSink.create(influxDB), system);
TODO
Writing to InfluxDB
You can also build flow stages. InfluxDbFlow
. The API is similar to creating Sinks.
- Scala
- Java
-
source
CompletableFuture<List<List<InfluxDbWriteResult<Point, NotUsed>>>> completableFuture = Source.single(Collections.singletonList(influxDbWriteMessage)) .via(InfluxDbFlow.create(influxDB)) .runWith(Sink.seq(), system) .toCompletableFuture();
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
- Java
-
source
// We're going to pretend we got metrics from kafka. // After we've written them to InfluxDb, we want // to commit the offset to Kafka /** Just clean the previous data */ influxDB.query(new Query("DELETE FROM cpu")); List<Integer> committedOffsets = new ArrayList<>(); List<MessageFromKafka> messageFromKafka = Arrays.asList( new MessageFromKafka( new InfluxDbCpu( Instant.now().minusSeconds(1000), "local_1", "eu-west-2", 1.4d, true, 123L), new KafkaOffset(0)), new MessageFromKafka( new InfluxDbCpu( Instant.now().minusSeconds(2000), "local_2", "eu-west-1", 2.5d, false, 125L), new KafkaOffset(1)), new MessageFromKafka( new InfluxDbCpu( Instant.now().minusSeconds(3000), "local_3", "eu-west-4", 3.1d, false, 251L), new KafkaOffset(2))); Consumer<KafkaOffset> commitToKafka = kafkaOffset -> committedOffsets.add(kafkaOffset.getOffset()); Source.from(messageFromKafka) .map( kafkaMessage -> { return InfluxDbWriteMessage.create( kafkaMessage.influxDbCpu, kafkaMessage.kafkaOffset); }) .groupedWithin(10, Duration.ofMillis(10)) .via(InfluxDbFlow.typedWithPassThrough(InfluxDbCpu.class, influxDB)) .map( messages -> { messages.stream() .forEach( message -> { KafkaOffset kafkaOffset = message.writeMessage().passThrough(); commitToKafka.accept(kafkaOffset); }); return NotUsed.getInstance(); }) .runWith(Sink.seq(), system) .toCompletableFuture() .get(10, TimeUnit.SECONDS);