Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem Mapping Long fields #410

Closed
claudio-au opened this issue Sep 2, 2022 · 3 comments · Fixed by #414
Closed

Problem Mapping Long fields #410

claudio-au opened this issue Sep 2, 2022 · 3 comments · Fixed by #414
Labels
bug Something isn't working
Milestone

Comments

@claudio-au
Copy link

claudio-au commented Sep 2, 2022

When It is mapped a Long (Not primitive) field on model class and we use pivot function to return the values for each field on model, a Cast exception is thrown during the execution.

It says is not possible cast Long to Double.
When we use long (primitive) it doesn't throw error, however it returns zero (not behavior expected).

Steps to reproduce:

  1. Create a model with 2 fields:

ModelInflux.java

//use lombok

@Getter
@Setter
@Measurement(name="model2")
@ToString
public class ModelInflux {

   @Column
  private Long field1;
  @Column
  private Double field2;

  @Column(timestamp = true, name = "time")
  private Instant timestamp;

}

  1. Save some data on InfluxDB
    Main.java
  public static void save(ModelInflux object, InfluxDBClientReactive client) {
    WriteReactiveApi writeApi = client.getWriteReactiveApi();

    var publisher = writeApi.writeMeasurement(WritePrecision.NS, object);
    Disposable subscriber =
        Flowable.fromPublisher(publisher)
            .subscribe(success -> log.info("Successfully written ModelInflux measurement"));
    subscriber.dispose();
  }
  1. Create a query with pivot function:
private static void runModelInflux(String host, String org, String token, String bucket)
      throws InterruptedException {

    String query = "from(bucket: \"test2\")"
        + "  |> range(start: -1h, stop: now())"
        + "  |> filter(fn: (r) => r[\"_measurement\"] == \"model2\")"
        + "  |> aggregateWindow(every: 10s, fn: mean, createEmpty: false)"
        + "  |> yield(name: \"mean\")"
        + "  |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";

    InfluxDBClientReactive client = InfluxDBClientReactiveFactory
        .create(host, token.toCharArray(), org, bucket);

    ModelInflux modelInflux = new ModelInflux();
    modelInflux.setField1((long)(Math.random() * 100));
    modelInflux.setField2((Math.random() * 100));

    save(modelInflux, client);

    Thread.sleep(1000);

    Instant end = Instant.now();
    Instant start = Instant.now().minus(1, ChronoUnit.HOURS);

    execute(query, client).doOnEach( e -> {
      if (e.get() != null) {
        log.info(e.get().toString());
      }
    }).doOnError(e -> {
      log.error(e.getMessage());
    }).subscribe();

    // Giving a time to process the result.
    Thread.sleep(10000);
  }

Main.java

  public static void main(String[] args) throws InterruptedException {
    String host = Main.HOST;
    String org = Main.ORG;
    String token= Main.TOKEN;
    String bucket = Main.BUCKET;

    runModelInflux2(host, org, token, "test2");
  }

Expected Behavior:

Return the model without error and value available.

Actual Behavior:
Error:
Caused by: com.influxdb.exceptions.InfluxException: Can not set java.lang.Long field com.autonomic.test.domain.ModelInflux2.field1 to java.lang.Double

Specifications:

  • Client Version: 6.4.0
  • InfluxDB Version: 2.0.9
  • JDK Version: Corretto 11.0.15.9.1
  • Platform: Mac OS M1

PS.: I see this code is converting only to primitives. (Maybe include more options to conversion?)

if (double.class.isAssignableFrom(fieldType)) {
field.setDouble(object, toDoubleValue(value));
return;
}
if (long.class.isAssignableFrom(fieldType)) {
field.setLong(object, toLongValue(value));
return;
}
if (int.class.isAssignableFrom(fieldType)) {
field.setInt(object, toIntValue(value));
return;
}
if (boolean.class.isAssignableFrom(fieldType)) {

@bednar
Copy link
Contributor

bednar commented Sep 5, 2022

Hi @claudio-au,

thanks for using our client.

Your issue is caused by using aggregateWindow function in your query. This function transform a type of the _value to the long due the mean calculation.

Try to use your query without aggregateWindow.

PS.: I see this code is converting only to primitives. (Maybe include more options to conversion?)

It will be good improvement... Is this something you might be willing to help with?

Regards

@bednar bednar added the enhancement New feature or request label Sep 5, 2022
@claudio-au
Copy link
Author

Alright @bednar , thanks for checking it.
Unfortunately, the documentation for the Influxdb is not clear what kind of return is for each function.
I'll modify it on my application.
I'll try to work on this improvement as well

Do you want I close this issue?
Thanks

@bednar
Copy link
Contributor

bednar commented Sep 7, 2022

I've prepared the fix in #414... so we can keep this issue opened until acceptance the PR

@bednar bednar added bug Something isn't working and removed enhancement New feature or request labels Sep 7, 2022
@bednar bednar added this to the 6.6.0 milestone Sep 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants