• influxdb-client-kotlin
    • Features
    • Queries
    • Advanced Usage
      • Client configuration file
        • Configuration example
    • Client connection string
    • Gzip support
    • Log HTTP Request and Response
    • Check the server status
    • Construct queries using the flux-dsl query builder
  • Version
    • Snapshot Repository
      • Maven
      • Gradle

    Kotlin - 图1influxdb-client-kotlin

    KDoc

    The reference Kotlin client that allows query and write for the InfluxDB 2.0 by Kotlin Channel coroutines.

    Kotlin - 图3Features

    Kotlin - 图4Queries

    The QueryKotlinApi supports asynchronous queries by Kotlin Channel coroutines.

    The following example demonstrates querying using the Flux language:

    1. package example
    2. import com.influxdb.client.kotlin.InfluxDBClientKotlinFactory
    3. import kotlinx.coroutines.channels.consumeEach
    4. import kotlinx.coroutines.channels.filter
    5. import kotlinx.coroutines.channels.take
    6. import kotlinx.coroutines.runBlocking
    7. fun main(args: Array<String>) = runBlocking {
    8. val influxDBClient = InfluxDBClientKotlinFactory
    9. .create("http://localhost:8086", "my-token".toCharArray(), "my-org")
    10. val fluxQuery = ("from(bucket: \"my-bucket\")\n"
    11. + " |> range(start: -1d)"
    12. + " |> filter(fn: (r) => (r[\"_measurement\"] == \"cpu\" and r[\"_field\"] == \"usage_system\"))")
    13. //Result is returned as a stream
    14. val results = influxDBClient.getQueryKotlinApi().query(fluxQuery)
    15. //Example of additional result stream processing on client side
    16. results
    17. //filter on client side using `filter` built-in operator
    18. .filter { "cpu0" == it.getValueByKey("cpu") }
    19. //take first 20 records
    20. .take(20)
    21. //print results
    22. .consumeEach { println("Measurement: ${it.measurement}, value: ${it.value}") }
    23. influxDBClient.close()
    24. }

    It is possible to parse a result line-by-line using the queryRaw method:

    1. package example
    2. import com.influxdb.client.kotlin.InfluxDBClientKotlinFactory
    3. import kotlinx.coroutines.channels.consumeEach
    4. import kotlinx.coroutines.runBlocking
    5. fun main(args: Array<String>) = runBlocking {
    6. val influxDBClient = InfluxDBClientKotlinFactory
    7. .create("http://localhost:8086", "my-token".toCharArray(), "my-org")
    8. val fluxQuery = ("from(bucket: \"my-bucket\")\n"
    9. + " |> range(start: -5m)"
    10. + " |> filter(fn: (r) => (r[\"_measurement\"] == \"cpu\" and r[\"_field\"] == \"usage_system\"))"
    11. + " |> sample(n: 5, pos: 1)")
    12. //Result is returned as a stream
    13. val results = influxDBClient.getQueryKotlinApi().queryRaw(fluxQuery)
    14. //print results
    15. results.consumeEach { println("Line: $it") }
    16. influxDBClient.close()
    17. }

    Kotlin - 图5Advanced Usage

    Kotlin - 图6Client configuration file

    A client can be configured via configuration file. The configuration file has to be named as influx2.properties and has to be in root of classpath.

    The following options are supported:

    Property namedefaultdescription
    influx2.url-the url to connect to InfluxDB
    influx2.org-default destination organization for writes and queries
    influx2.bucket-default destination bucket for writes
    influx2.token-the token to use for the authorization
    influx2.logLevelNONErest client verbosity level
    influx2.readTimeout10000 msread timeout
    influx2.writeTimeout10000 mswrite timeout
    influx2.connectTimeout10000 mssocket timeout

    The influx2.readTimeout, influx2.writeTimeout and influx2.connectTimeout supports ms, s and m as unit. Default is milliseconds.

    Kotlin - 图7Configuration example
    1. influx2.url=http://localhost:8086
    2. influx2.org=my-org
    3. influx2.bucket=my-bucket
    4. influx2.token=my-token
    5. influx2.logLevel=BODY
    6. influx2.readTimeout=5s
    7. influx2.writeTimeout=10s
    8. influx2.connectTimeout=5s

    and then:

    1. val influxDBClient = InfluxDBClientKotlinFactory.create();

    Kotlin - 图8Client connection string

    A client can be constructed using a connection string that can contain the InfluxDBClientOptions parameters encoded into the URL.

    1. val influxDBClient = InfluxDBClientKotlinFactory
    2. .create("http://localhost:8086?readTimeout=5000&connectTimeout=5000&logLevel=BASIC", token)

    The following options are supported:

    Property namedefaultdescription
    org-default destination organization for writes and queries
    bucket-default destination bucket for writes
    token-the token to use for the authorization
    logLevelNONErest client verbosity level
    readTimeout10000 msread timeout
    writeTimeout10000 mswrite timeout
    connectTimeout10000 mssocket timeout

    The readTimeout, writeTimeout and connectTimeout supports ms, s and m as unit. Default is milliseconds.

    Kotlin - 图9Gzip support

    InfluxDBClientKotlin does not enable gzip compress for http requests by default. If you want to enable gzip to reduce transfer data’s size, you can call:

    1. influxDBClient.enableGzip();

    Kotlin - 图10Log HTTP Request and Response

    The Requests and Responses can be logged by changing the LogLevel. LogLevel values are NONE, BASIC, HEADER, BODY. Note that applying the BODY LogLevel will disable chunking while streaming and will load the whole response into memory.

    1. influxDBClient.setLogLevel(LogLevel.HEADERS)

    Kotlin - 图11Check the server status

    Server availability can be checked using the influxDBClient.health() endpoint.

    Kotlin - 图12Construct queries using the flux-dsl query builder

    1. package example
    2. import com.influxdb.client.kotlin.InfluxDBClientKotlinFactory
    3. import com.influxdb.query.dsl.Flux
    4. import com.influxdb.query.dsl.functions.restriction.Restrictions
    5. import kotlinx.coroutines.channels.consumeEach
    6. import kotlinx.coroutines.channels.filter
    7. import kotlinx.coroutines.channels.take
    8. import kotlinx.coroutines.runBlocking
    9. import java.time.temporal.ChronoUnit
    10. fun main(args: Array<String>) = runBlocking {
    11. val influxDBClient = InfluxDBClientKotlinFactory
    12. .create("http://localhost:8086", "my-token".toCharArray(), "my-org")
    13. val mem = Flux.from("my-bucket")
    14. .range(-30L, ChronoUnit.MINUTES)
    15. .filter(Restrictions.and(Restrictions.measurement().equal("mem"), Restrictions.field().equal("used_percent")))
    16. //Result is returned as a stream
    17. val results = influxDBClient.getQueryKotlinApi().query(mem.toString())
    18. //Example of additional result stream processing on client side
    19. results
    20. //filter on client side using `filter` built-in operator
    21. .filter { (it.value as Double) > 55 }
    22. // take first 20 records
    23. .take(20)
    24. //print results
    25. .consumeEach { println("Measurement: ${it.measurement}, value: ${it.value}") }
    26. influxDBClient.close()
    27. }

    Kotlin - 图13Version

    The latest version for Maven dependency:

    1. <dependency>
    2. <groupId>com.influxdb</groupId>
    3. <artifactId>influxdb-client-kotlin</artifactId>
    4. <version>1.13.0</version>
    5. </dependency>

    Or when using with Gradle:

    1. dependencies {
    2. compile "com.influxdb:influxdb-client-kotlin:1.13.0"
    3. }

    Kotlin - 图14Snapshot Repository

    The snapshots are deployed into OSS Snapshot repository.

    Kotlin - 图15Maven

    1. <repository>
    2. <id>ossrh</id>
    3. <name>OSS Snapshot repository</name>
    4. <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    5. <releases>
    6. <enabled>false</enabled>
    7. </releases>
    8. <snapshots>
    9. <enabled>true</enabled>
    10. </snapshots>
    11. </repository>

    Kotlin - 图16Gradle

    1. repositories {
    2. maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    3. }