• Documentation
  • Features
  • Clients
  • How To Use
    • Writes and Queries in InfluxDB 2.x
      • Installation
        • Maven dependency:
        • Or when using Gradle:
    • Use Management API to create a new Bucket in InfluxDB 2.x
      • Installation
        • Maven dependency:
        • Or when using Gradle:
    • InfluxDB 1.8 API compatibility
    • Flux queries in InfluxDB 1.7+
      • Installation
        • Maven dependency:
        • Or when using Gradle:
  • Build Requirements
  • Contributing
  • License

    influxdb-client-java

    This repository contains the reference JVM clients for the InfluxDB 2.x. Currently, Java, Reactive, Kotlin and Scala clients are implemented.

    Java - 图10Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ (). For connecting to InfluxDB 1.7 or earlier instances, use the influxdb-java client library.

    This section contains links to the client library documentation.

    Features

    • InfluxDB 2.x client
      • Querying data using the Flux language
      • 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 ()

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

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

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

    How To Use

    This clients are hosted in Maven central Repository.

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

    Installation

    Download the latest version:

    Maven dependency:
    1. <dependency>
    2. <groupId>com.influxdb</groupId>
    3. <artifactId>influxdb-client-java</artifactId>
    4. <version>5.0.0</version>
    5. </dependency>
    Or when using Gradle:
    1. dependencies {
    2. implementation "com.influxdb:influxdb-client-java:5.0.0"
    3. }
    1. package example;
    2. import java.time.Instant;
    3. import java.util.List;
    4. import com.influxdb.annotations.Column;
    5. import com.influxdb.annotations.Measurement;
    6. import com.influxdb.client.InfluxDBClient;
    7. import com.influxdb.client.InfluxDBClientFactory;
    8. import com.influxdb.client.QueryApi;
    9. import com.influxdb.client.WriteApiBlocking;
    10. import com.influxdb.client.domain.WritePrecision;
    11. import com.influxdb.client.write.Point;
    12. import com.influxdb.query.FluxRecord;
    13. import com.influxdb.query.FluxTable;
    14. public class InfluxDB2Example {
    15. private static char[] token = "my-token".toCharArray();
    16. private static String org = "my-org";
    17. private static String bucket = "my-bucket";
    18. public static void main(final String[] args) {
    19. InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token, org, bucket);
    20. //
    21. // Write data
    22. //
    23. WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();
    24. //
    25. // Write by Data Point
    26. //
    27. Point point = Point.measurement("temperature")
    28. .addTag("location", "west")
    29. .time(Instant.now().toEpochMilli(), WritePrecision.MS);
    30. writeApi.writePoint(point);
    31. //
    32. // Write by LineProtocol
    33. //
    34. writeApi.writeRecord(WritePrecision.NS, "temperature,location=north value=60.0");
    35. //
    36. // Write by POJO
    37. //
    38. Temperature temperature = new Temperature();
    39. temperature.location = "south";
    40. temperature.value = 62D;
    41. temperature.time = Instant.now();
    42. writeApi.writeMeasurement( WritePrecision.NS, temperature);
    43. //
    44. // Query data
    45. //
    46. String flux = "from(bucket:\"my-bucket\") |> range(start: 0)";
    47. QueryApi queryApi = influxDBClient.getQueryApi();
    48. List<FluxTable> tables = queryApi.query(flux);
    49. for (FluxTable fluxTable : tables) {
    50. List<FluxRecord> records = fluxTable.getRecords();
    51. for (FluxRecord fluxRecord : records) {
    52. }
    53. }
    54. influxDBClient.close();
    55. }
    56. @Measurement(name = "temperature")
    57. private static class Temperature {
    58. @Column(tag = true)
    59. String location;
    60. @Column
    61. Double value;
    62. @Column(timestamp = true)
    63. Instant time;
    64. }
    65. }

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

    Java - 图20Installation

    Download the latest version:

    Maven dependency:
    Java - 图22Or when using Gradle:
    1. dependencies {
    2. implementation "com.influxdb:influxdb-client-java:5.0.0"
    3. }
    1. package example;
    2. import java.util.Arrays;
    3. import com.influxdb.client.InfluxDBClient;
    4. import com.influxdb.client.InfluxDBClientFactory;
    5. import com.influxdb.client.domain.Authorization;
    6. import com.influxdb.client.domain.Bucket;
    7. import com.influxdb.client.domain.Permission;
    8. import com.influxdb.client.domain.PermissionResource;
    9. import com.influxdb.client.domain.BucketRetentionRules;
    10. public class InfluxDB2ManagementExample {
    11. private static char[] token = "my-token".toCharArray();
    12. public static void main(final String[] args) {
    13. InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token);
    14. //
    15. // Create bucket "iot_bucket" with data retention set to 3,600 seconds
    16. //
    17. BucketRetentionRules retention = new BucketRetentionRules();
    18. retention.setEverySeconds(3600);
    19. Bucket bucket = influxDBClient.getBucketsApi().createBucket("iot-bucket", retention, "12bdc4164c2e8141");
    20. //
    21. // Create access token to "iot_bucket"
    22. //
    23. PermissionResource resource = new PermissionResource();
    24. resource.setId(bucket.getId());
    25. resource.setOrgID("12bdc4164c2e8141");
    26. resource.setType(PermissionResource.TYPE_BUCKETS);
    27. // Read permission
    28. Permission read = new Permission();
    29. read.setAction(Permission.ActionEnum.READ);
    30. // Write permission
    31. Permission write = new Permission();
    32. write.setResource(resource);
    33. write.setAction(Permission.ActionEnum.WRITE);
    34. Authorization authorization = influxDBClient.getAuthorizationsApi()
    35. .createAuthorization("12bdc4164c2e8141", Arrays.asList(read, write));
    36. //
    37. // Created token that can be use for writes to "iot_bucket"
    38. //
    39. String token = authorization.getToken();
    40. influxDBClient.close();
    41. }
    42. }

    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:

    The following example demonstrates querying using the Flux language.

    Installation

    Download the latest version:

    Maven dependency:
    1. <dependency>
    2. <groupId>com.influxdb</groupId>
    3. <artifactId>influxdb-client-flux</artifactId>
    4. <version>5.0.0</version>
    5. </dependency>
    Or when using Gradle:
    1. package example;
    2. import java.util.List;
    3. import com.influxdb.client.flux.FluxClient;
    4. import com.influxdb.client.flux.FluxClientFactory;
    5. import com.influxdb.query.FluxRecord;
    6. import com.influxdb.query.FluxTable;
    7. public class FluxExample {
    8. public static void main(String[] args) {
    9. FluxClient fluxClient = FluxClientFactory.create("http://localhost:8086/");
    10. //
    11. // Flux
    12. //
    13. String flux = "from(bucket: \"telegraf\")\n" +
    14. " |> range(start: -1d)" +
    15. " |> filter(fn: (r) => (r[\"_measurement\"] == \"cpu\" and r[\"_field\"] == \"usage_system\"))" +
    16. " |> sample(n: 5, pos: 1)";
    17. //
    18. // Synchronous query
    19. //
    20. List<FluxTable> tables = fluxClient.query(flux);
    21. for (FluxTable fluxTable : tables) {
    22. List<FluxRecord> records = fluxTable.getRecords();
    23. for (FluxRecord fluxRecord : records) {
    24. System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));
    25. }
    26. }
    27. //
    28. // Asynchronous query
    29. //
    30. fluxClient.query(flux, (cancellable, record) -> {
    31. // process the flux query result record
    32. System.out.println(record.getTime() + ": " + record.getValue());
    33. }, error -> {
    34. // error handling while processing result
    35. System.out.println("Error occurred: "+ error.getMessage());
    36. }, () -> {
    37. // on complete
    38. System.out.println("Query completed");
    39. });
    40. fluxClient.close();
    41. }
    42. }
    • Java 1.8+ (tested with jdk8)
    • 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

    Contributing

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