The client that allow perform Flux Query against the InfluxDB 1.7+.
This section contains links to the client library documentation.
The FluxClientFactory
creates an instance of a FluxClient
client that can be customized with FluxConnectionOptions
.
FluxConnectionOptions
parameters:
url
- the url to connect to InfluxDBokHttpClient
- custom HTTP client to use for communications with InfluxDB (optional)
// client creation
FluxConnectionOptions options = FluxConnectionOptions.builder()
.url("http://localhost:8086/")
.build();
FluxClient fluxClient = FluxClientFactory.create(options);
fluxClient.query(...)
...
A client can be constructed using a connection string that can contain the FluxConnectionOptions parameters encoded into the URL.
FluxClient fluxClient = FluxClientFactory.create("http://localhost:8086?readTimeout=5000&connectTimeout=5000&logLevel=BASIC")
The following options are supported:
Property name | default | description |
---|---|---|
readTimeout | 10000 ms | read timeout |
writeTimeout | 10000 ms | write timeout |
connectTimeout | 10000 ms | socket timeout |
logLevel | NONE | rest client verbosity level |
The library supports both synchronous and asynchronous queries.
A simple synchronous example:
String query = "from(bucket:\"telegraf\") |> range(start: -1d) |> filter(fn: (r) => r[\"_measurement\"] == \"cpu\" and r[\"_field\"] == \"usage_user\") |> sum()";
//simple synchronous query
List<FluxTable> tables = fluxClient.flux(query);
For larger data sets it is more effective to stream data and to use asynchronous requests or the reactive client based on RxJava2.
Construct queries using the flux-dsl query builder
Flux-dsl contains java classes representing elements of the Flux language to help build Flux queries and expressions.
All supported operators are documented in Operators and in javadoc. Custom functions can be added easily—see Custom operator.
An example of using the Flux
query builder:
Flux.from("telegraf")
.range(-1L, ChronoUnit.DAYS)
.filter(
Restrictions.and(
Restrictions.measurement().equal("cpu"),
Restrictions.field().equal("usage_system"))
)
.sample(5, 1);
The asynchronous query API allows streaming of FluxRecord
s with the possibility of implementing custom
error handling and onComplete
callback notification.
A Cancellable
object is used for aborting a query while processing.
For developers that are familiar with reactive programming and for more advanced usecases it is possible to use the flux-client-rxjava extension.
An asynchronous query example:
String fluxQuery = "from(bucket: \"telegraf\")\n" +
" |> range(start: -1d)" +
" |> filter(fn: (r) => (r[\"_measurement\"] == \"cpu\" and r[\"_field\"] == \"usage_system\"))" +
" |> sample(n: 5, pos: 1)";
fluxClient.query(
fluxQuery, (cancellable, record) -> {
// process the flux query result record
System.out.println(
record.getTime() + ": " + record.getValue());
// found what I'm looking for ?
if (some condition) {
// abort processing
cancellable.cancel();
}
}, error -> {
// error handling while processing result
System.out.println("Error occurred: "+ error.getMessage());
}, () -> {
// on complete notification
System.out.println("Query completed");
});
It is possible to parse a result line-by-line using the queryRaw
method.
void queryRaw(@Nonnull final String query,
@Nonnull final BiConsumer<Cancellable, String> onResponse,
@Nonnull final Consumer<? super Throwable> onError,
@Nonnull final Runnable onComplete);
Currently unsupported by the server.
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.
fluxClient.setLogLevel(Level.HEADERS);
Server availability can be checked using the fluxClient.ping()
endpoint. Server version can be obtained using fluxClient.version()
.
The latest version for Maven dependency:
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-flux</artifactId>
<version>7.2.0</version>
</dependency>
Or when using with Gradle:
dependencies {
implementation "com.influxdb:influxdb-client-flux:7.2.0"
}
The snapshots are deployed into OSS Snapshot repository.
<repository>
<id>ossrh</id>
<name>OSS Snapshot repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}