Connessione a InfluxDB con Java
In questo articolo vediamo come eseguire una connessione a InfluxDB in Java.
La prima cosa da fare è installare la libreria apposita.
Se usate Maven:
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>6.6.0</version>
</dependency>
Qui sotto il codice di esempio:
package org.example;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import java.time.Instant;
import java.util.List;
public class Main {
public static void main(String[] args) {
String token = "TOKEN";
String org = "EMAIL";
String bucket = "test";
InfluxDBClient client = InfluxDBClientFactory.create("https://eu-central-1-1.aws.cloud2.influxdata.com", token.toCharArray());
Point point = Point
.measurement("mem")
.addTag("host", "host1")
.addField("used_percent", 23.43234543)
.time(Instant.now(), WritePrecision.NS);
WriteApiBlocking writeApi = client.getWriteApiBlocking();
writeApi.writePoint(bucket, org, point);
String query = "from(bucket: "test") |> range(start: -1h)";
List<FluxTable> tables = client.getQueryApi().query(query, org);
for (FluxTable table : tables) {
for (FluxRecord record : table.getRecords()) {
System.out.println(record);
}
}
client.close();
}
}
Dove org è la email con la quale vi siete iscritti; e bucket è quello che avete creato.
Enjoy!
java database influxdb maven
Commentami!