Skip to content

feat: parameterized queries support#286

Merged
rhajek merged 6 commits intomasterfrom
feat/query-params
Dec 13, 2021
Merged

feat: parameterized queries support#286
rhajek merged 6 commits intomasterfrom
feat/query-params

Conversation

@rhajek
Copy link
Copy Markdown
Contributor

@rhajek rhajek commented Dec 10, 2021

Proposed Changes

This PR implements support for Parameterized Queries for InfluxDB Cloud.
https://docs.influxdata.com/influxdb/cloud/query-data/parameterized-queries

Checklist

  • CHANGELOG.md updated
  • Rebased/mergeable
  • A test has been added if appropriate
  • mvn test completes successfully
  • Commit messages are in semantic format
  • Sign CLA (if not already signed)

@rhajek rhajek requested a review from bednar December 10, 2021 12:57
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Dec 10, 2021

Codecov Report

Merging #286 (7623c83) into master (29a0205) will decrease coverage by 0.85%.
The diff coverage is 29.88%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #286      +/-   ##
============================================
- Coverage     89.71%   88.85%   -0.86%     
  Complexity      473      473              
============================================
  Files           149      149              
  Lines          5913     5987      +74     
  Branches        283      289       +6     
============================================
+ Hits           5305     5320      +15     
- Misses          524      581      +57     
- Partials         84       86       +2     
Impacted Files Coverage Δ
...ava/com/influxdb/client/internal/QueryApiImpl.java 84.83% <16.98%> (-14.00%) ⬇️
...client/reactive/internal/QueryReactiveApiImpl.java 78.57% <50.00%> (-13.64%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 29a0205...7623c83. Read the comment docs.

Copy link
Copy Markdown
Contributor

@bednar bednar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR 👍. There are a few requirements that must be be satisfy before we accept the PR:

  1. Add note to the following APIs how to use query parameters in Query object:
    • com.influxdb.client.scala.QueryScalaApi
    • com.influxdb.client.kotlin.QueryKotlinApi
  2. Add possibility to use Query object in com.influxdb.client.reactive.QueryReactiveApi

and also:

Comment on lines +1 to +70
package example;

import java.time.Instant;
import java.time.Period;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.QueryApi;
import com.influxdb.client.WriteApi;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxTable;

public class ParameterizedQuery {

public static void main(String[] args) {

String url = System.getenv("INFLUX_URL");
String token = System.getenv("INFLUX_TOKEN");
String org = System.getenv("INFLUX_ORG");
String bucket = System.getenv("INFLUX_BUCKET");

InfluxDBClient client = InfluxDBClientFactory.create(url,
token.toCharArray(),
org, bucket);

WriteApi writeApi = client.makeWriteApi();

Instant yesterday = Instant.now().minus(Period.ofDays(1));

Point p = Point.measurement("temperature")
.addTag("location", "north")
.addField("value", 60.0)
.time(yesterday, WritePrecision.NS);

writeApi.writePoint(p);

writeApi.close();

//
// Query range start parameter using Instant
//
QueryApi queryApi = client.getQueryApi();
Map<String, Object> params = new HashMap<>();
params.put("bucketParam", bucket);
params.put("startParam", yesterday.toString());

String parametrizedQuery = "from(bucket: params.bucketParam) |> range(start: time(v: params.startParam))";
List<FluxTable> query = queryApi.query(parametrizedQuery, org, params);
query.forEach(fluxTable -> fluxTable.getRecords()
.forEach(r -> System.out.println(r.getTime() + ": " + r.getValueByKey("_value"))));

//
// Query range start parameter using duration
//
params = new HashMap<>();
params.put("bucketParam", bucket);
params.put("startParam", "-1d10s");
parametrizedQuery = "from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam))";
query = queryApi.query(parametrizedQuery, org, params);
query.forEach(fluxTable -> fluxTable.getRecords()
.forEach(r -> System.out.println(r.getTime() + ": " + r.getValueByKey("_value"))));

client.close();

}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add link to this example into examples/README.md.

Comment on lines +600 to +606
<M> void query(@Nonnull final String query,
@Nonnull final String org,
@Nonnull final Class<M> measurementType,
@Nonnull final BiConsumer<Cancellable, M> onNext,
@Nonnull final Consumer<? super Throwable> onError,
@Nonnull final Runnable onComplete,
@Nullable final Map<String, Object> params);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use same spacing.

Comment on lines +174 to +210
@Override

@Override
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use same spacing.

Comment thread client/README.md Outdated
query.forEach(fluxTable -> fluxTable.getRecords()
.forEach(r -> System.out.println(r.getTime() + ": " + r.getValueByKey("_value"))));

client.close();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use try-catch instead of close().

Comment thread client/README.md Outdated
Comment on lines +369 to +372
String url = System.getenv("INFLUX_URL");
String token = System.getenv("INFLUX_TOKEN");
String org = System.getenv("INFLUX_ORG");
String bucket = System.getenv("INFLUX_BUCKET");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +42 to +45
String url = System.getenv("INFLUX_URL");
String token = System.getenv("INFLUX_TOKEN");
String org = System.getenv("INFLUX_ORG");
String bucket = System.getenv("INFLUX_BUCKET");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhajek rhajek requested a review from bednar December 10, 2021 14:36
Copy link
Copy Markdown
Contributor

@bednar bednar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add possibility to use Query object in com.influxdb.client.reactive.QueryReactiveApi.

Copy link
Copy Markdown
Contributor

@bednar bednar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@rhajek rhajek merged commit 5ca93a5 into master Dec 13, 2021
@rhajek rhajek deleted the feat/query-params branch December 13, 2021 11:39
@bednar bednar added this to the 4.1.0 milestone Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants