influxdb-client-java

Java - 图1

CircleCI codecov License Maven Central Maven Site GitHub issues GitHub pull requests Slack Status

This repository contains the Java client library for use with InfluxDB 2.x and Flux. Currently, Java, Reactive, Kotlin and Scala clients are implemented. InfluxDB 3.x users should instead use the lightweight v3 client library. InfluxDB 1.x users should use the v1 client library.

For ease of migration and a consistent query and write experience, v2 users should consider using InfluxQL and the v1 client library.

Documentation

Java - 图10

This section contains links to the client library documentation.

Features

Java - 图11

  • InfluxDB 2.x client
  • Querying data using the Flux language
  • Querying data using the InfluxQL
  • Writing data using
  • InfluxDB 2.x Management API client for managing
    • sources, buckets
    • tasks
    • authorizations
    • health check
    • …
  • Supports querying using the Flux language over the InfluxDB 1.7+ REST API (/api/v2/query endpoint)

Clients

Java - 图12

The Java, Reactive, OSGi, Kotlin and Scala clients are implemented for the InfluxDB 2.x:

Client Description Documentation Compatibility
java The reference Java client that allows query, write and InfluxDB 2.x management. javadoc, readme 2.x
reactive The reference RxJava client for the InfluxDB 2.x that allows query and write in a reactive way. javadoc, readme 2.x
kotlin The reference Kotlin client that allows query and write for the InfluxDB 2.x by Kotlin Channel and Flow coroutines. KDoc, readme 2.x
scala The reference Scala client that allows query and write for the InfluxDB 2.x by Pekko Streams. Scaladoc, readme 2.x
osgi The reference OSGi (R6) client embedding Java and reactive clients and providing standard features (declarative services, configuration, event processing) for the InfluxDB 2.x. javadoc, readme 2.x
karaf The Apache Karaf feature definition for the InfluxDB 2.x. readme 2.x

There is also possibility to use the Flux language over the InfluxDB 1.7+ provided by:

Client Description Documentation Compatibility
flux The reference Java client that allows you to perform Flux queries against InfluxDB 1.7+. javadoc, readme 1.7+

The last useful part is flux-dsl that helps construct Flux query by Query builder pattern:

1. Flux flux = Flux
2. .from("telegraf")
3. .window(15L, ChronoUnit.MINUTES, 20L, ChronoUnit.SECONDS)
4. .sum();
Module Description Documentation Compatibility
flux-dsl A Java query builder for the Flux language javadoc, readme 1.7+, 2.x

How To Use

Java - 图13

This clients are hosted in Maven central Repository.

If you want to use it with the Maven, you have to add only the dependency on the artifact.

Writes and Queries in InfluxDB 2.x

Java - 图14

The following example demonstrates how to write data to InfluxDB 2.x and read them back using the Flux language.

Installation

Java - 图15

Download the latest version:

Maven dependency:

Java - 图16

1. <dependency>
2. <groupId>com.influxdb</groupId>
3. <artifactId>influxdb-client-java</artifactId>
4. <version>7.2.0</version>
5. </dependency>
Or when using Gradle:

Java - 图17

1. dependencies {
2. implementation "com.influxdb:influxdb-client-java:7.2.0"
3. }
1. package example;

3. import java.time.Instant;
4. import java.util.List;

6. import com.influxdb.annotations.Column;
7. import com.influxdb.annotations.Measurement;
8. import com.influxdb.client.InfluxDBClient;
9. import com.influxdb.client.InfluxDBClientFactory;
10. import com.influxdb.client.QueryApi;
11. import com.influxdb.client.WriteApiBlocking;
12. import com.influxdb.client.domain.WritePrecision;
13. import com.influxdb.client.write.Point;
14. import com.influxdb.query.FluxRecord;
15. import com.influxdb.query.FluxTable;

17. public class InfluxDB2Example {

19. private static char[] token = "my-token".toCharArray();
20. private static String org = "my-org";
21. private static String bucket = "my-bucket";

23. public static void main(final String[] args) {

25. InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token, org, bucket);

27. //
28. // Write data
29. //
30. WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();

32. //
33. // Write by Data Point
34. //
35. Point point = Point.measurement("temperature")
36. .addTag("location", "west")
37. .addField("value", 55D)
38. .time(Instant.now().toEpochMilli(), WritePrecision.MS);

40. writeApi.writePoint(point);

42. //
43. // Write by LineProtocol
44. //
45. writeApi.writeRecord(WritePrecision.NS, "temperature,location=north value=60.0");

47. //
48. // Write by POJO
49. //
50. Temperature temperature = new Temperature();
51. temperature.location = "south";
52. temperature.value = 62D;
53. temperature.time = Instant.now();

55. writeApi.writeMeasurement( WritePrecision.NS, temperature);

57. //
58. // Query data
59. //
60. String flux = "from(bucket:\"my-bucket\") |> range(start: 0)";

62. QueryApi queryApi = influxDBClient.getQueryApi();

64. List<FluxTable> tables = queryApi.query(flux);
65. for (FluxTable fluxTable : tables) {
66. List<FluxRecord> records = fluxTable.getRecords();
67. for (FluxRecord fluxRecord : records) {
68. System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));
69. }
70. }

72. influxDBClient.close();
73. }

75. @Measurement(name = "temperature")
76. private static class Temperature {

78. @Column(tag = true)
79. String location;

81. @Column
82. Double value;

84. @Column(timestamp = true)
85. Instant time;
86. }
87. }

Use Management API to create a new Bucket in InfluxDB 2.x

Java - 图18

The following example demonstrates how to use a InfluxDB 2.x Management API. For further information see client documentation.

Installation

Java - 图19

Download the latest version:

Maven dependency:

Java - 图20

1. <dependency>
2. <groupId>com.influxdb</groupId>
3. <artifactId>influxdb-client-java</artifactId>
4. <version>7.2.0</version>
5. </dependency>
Or when using Gradle:

Java - 图21

1. dependencies {
2. implementation "com.influxdb:influxdb-client-java:7.2.0"
3. }
1. package example;

3. import java.util.Arrays;

5. import com.influxdb.client.InfluxDBClient;
6. import com.influxdb.client.InfluxDBClientFactory;
7. import com.influxdb.client.domain.Authorization;
8. import com.influxdb.client.domain.Bucket;
9. import com.influxdb.client.domain.Permission;
10. import com.influxdb.client.domain.PermissionResource;
11. import com.influxdb.client.domain.BucketRetentionRules;

13. public class InfluxDB2ManagementExample {

15. private static char[] token = "my-token".toCharArray();

17. public static void main(final String[] args) {

19. InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token);

21. //
22. // Create bucket "iot_bucket" with data retention set to 3,600 seconds
23. //
24. BucketRetentionRules retention = new BucketRetentionRules();
25. retention.setEverySeconds(3600);

27. Bucket bucket = influxDBClient.getBucketsApi().createBucket("iot-bucket", retention, "12bdc4164c2e8141");

29. //
30. // Create access token to "iot_bucket"
31. //
32. PermissionResource resource = new PermissionResource();
33. resource.setId(bucket.getId());
34. resource.setOrgID("12bdc4164c2e8141");
35. resource.setType(PermissionResource.TYPE_BUCKETS);

37. // Read permission
38. Permission read = new Permission();
39. read.setResource(resource);
40. read.setAction(Permission.ActionEnum.READ);

42. // Write permission
43. Permission write = new Permission();
44. write.setResource(resource);
45. write.setAction(Permission.ActionEnum.WRITE);

47. Authorization authorization = influxDBClient.getAuthorizationsApi()
48. .createAuthorization("12bdc4164c2e8141", Arrays.asList(read, write));

50. //
51. // Created token that can be use for writes to "iot_bucket"
52. //
53. String token = authorization.getToken();
54. System.out.println("Token: " + token);

56. influxDBClient.close();
57. }
58. }

InfluxDB 1.8 API compatibility

Java - 图22

InfluxDB 1.8.0 introduced forward compatibility APIs for InfluxDB 2.x. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.x Cloud or open source.

The following forward compatible APIs are available:

API Endpoint Description
QueryApi.java /api/v2/query Query data in InfluxDB 1.8.0+ using the InfluxDB 2.x API and Flux (endpoint should be enabled by flux-enabled option)
WriteApi.java /api/v2/write Write data to InfluxDB 1.8.0+ using the InfluxDB 2.x API
health() /health Check the health of your InfluxDB instance

For detail info see InfluxDB 1.8 example.

Flux queries in InfluxDB 1.7+

Java - 图23

The following example demonstrates querying using the Flux language.

Installation

Java - 图24

Download the latest version:

Maven dependency:

Java - 图25

1. <dependency>
2. <groupId>com.influxdb</groupId>
3. <artifactId>influxdb-client-flux</artifactId>
4. <version>7.2.0</version>
5. </dependency>
Or when using Gradle:

Java - 图26

1. dependencies {
2. implementation "com.influxdb:influxdb-client-flux:7.2.0"
3. }
1. package example;

3. import java.util.List;

5. import com.influxdb.client.flux.FluxClient;
6. import com.influxdb.client.flux.FluxClientFactory;
7. import com.influxdb.query.FluxRecord;
8. import com.influxdb.query.FluxTable;

10. public class FluxExample {

12. public static void main(String[] args) {

14. FluxClient fluxClient = FluxClientFactory.create("http://localhost:8086/");

16. //
17. // Flux
18. //
19. String flux = "from(bucket: \"telegraf\")\n" +
20. " |> range(start: -1d)" +
21. " |> filter(fn: (r) => (r[\"_measurement\"] == \"cpu\" and r[\"_field\"] == \"usage_system\"))" +
22. " |> sample(n: 5, pos: 1)";

24. //
25. // Synchronous query
26. //
27. List<FluxTable> tables = fluxClient.query(flux);

29. for (FluxTable fluxTable : tables) {
30. List<FluxRecord> records = fluxTable.getRecords();
31. for (FluxRecord fluxRecord : records) {
32. System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));
33. }
34. }

36. //
37. // Asynchronous query
38. //
39. fluxClient.query(flux, (cancellable, record) -> {

41. // process the flux query result record
42. System.out.println(record.getTime() + ": " + record.getValue());

44. }, error -> {

46. // error handling while processing result
47. System.out.println("Error occurred: "+ error.getMessage());

49. }, () -> {

51. // on complete
52. System.out.println("Query completed");
53. });

55. fluxClient.close();
56. }
57. }

Build Requirements

Java - 图27

  • Java 17+ (tested with JDK 17)
  • ⚠️ If you want to use older version of JDK, you have to use the 6.x version of the client.
  • Maven 3.0+ (tested with maven 3.5.0)
  • Docker daemon running
  • The latest InfluxDB 2.x and InfluxDB 1.X docker instances, which can be started using the ./scripts/influxdb-restart.sh script

Once these are in place you can build influxdb-client-java with all tests with:

1. $ mvn clean install

If you don’t have Docker running locally, you can skip tests with the -DskipTests flag set to true:

1. $ mvn clean install -DskipTests=true

If you have Docker running, but it is not available over localhost (e.g. you are on a Mac and using docker-machine) you can set optional environment variables to point to the correct IP addresses and ports:

  • INFLUXDB_IP
  • INFLUXDB_PORT_API
  • INFLUXDB_2_IP
  • INFLUXDB_2_PORT_API
1. $ export INFLUXDB_IP=192.168.99.100
2. $ mvn test

Contributing

Java - 图28

If you would like to contribute code you can do through GitHub by forking the repository and sending a pull request into the master branch.

License

Java - 图29

The InfluxDB 2.x JVM Based Clients are released under the MIT License.