InfluxDB v2.5.1
influxdb-client-java 6.7.0
Java POJO
@Data @Builder @NoArgsConstructor @AllArgsConstructor @Measurement(name = "my_measurement") public class MyMeasurement { @Column(timestamp = true) private Instant time; @Column(name = "my_tag", tag = true) private Long myTag; @Column(name = "value") private Integer value; }
Write
influxDBClient.getWriteApiBlocking().writeMeasurements(WritePrecision.NS, IntStream.range(0, 1000) .mapToObj(i -> MyMeasurement.builder() .time(Instant.now()) .myTag(Math.abs(new Random().nextLong())) .value(Math.abs(new Random().nextInt())) .build()) .collect(Collectors.toList()));
Read
influxDBClient.getQueryApi() .query("from(bucket: "mybucket") |> range(start: -1d) |> filter(fn: (r) => r["_measurement"] == "my_measurement")", MyMeasurement.class);
The reading statement throws InfluxException
com.influxdb.exceptions.InfluxException: Class 'MyMeasurement' field 'myTag' was defined with a different field type and caused a ClassCastException. The correct type is 'java.lang.String' (current field value: '1000816197908726879').
Advertisement
Answer
- First of all tag in influxdb always is string link
- Secondly when you get it from influx using influx-client it get it from influxdb as a String and cannot cast to Long.
(because under hood java influx client uses
long.class.isAssignableFrom(fieldType))
and String is not assiganble for Long. Possible solution is to useString
for tags in your POJO.