influxdb-java
influxdb-java copied to clipboard
Is there any way to use a generic @Measurement annotation when creating classes to use with toPOJO()?
I'm using influx-java and would like to simplify the marshalling of the data returned from influx. I've started working with the toPOJO method from the InfluxDBResultMapper, however it seems that in order for me to get any data into my java objects, I need to have a class defined in advance specific to the measurement I'm querying against (eg, the @Measurement annotation must be specified at compile-time). Given the way our database is designed, this is simply not possible. We maintain a number of distinct measurements per-environment, per-tenant and per-measurement. So, our measurements might look like: productionEnv_tenantId_measurement, where there are a handful of productionEnvs, each with tens of measurements, and n tenantIds (hundreds to thousands).
The POJO I've defined is quite simple - it wants an Instant and a sum for the selected column given a query over time, grouped by time. However, the only way I can get any non-null results into my List<POJO> is if I specify one (of our possible thousands) of measurements in the class annotation, which is (obviously) not feasible. Is there something I'm missing here? Is there any generic way to re-use my generic POJO to get results from any given measurement? Or do I need to go back to marshalling the data into JSON arrays (which is super not fun)?
For reference, my POJO looks like this. Not specifying a measurement causes an exception to be thrown and the only way to populate the class is if the measurement specified is an exact match, which isn't terribly useful for our use-case.
`@Measurement(name = "foo") public class TimestampAndValue { @Column(name = "time") private Instant time; @Column(name = "sum") private long sum;
public TimestampAndValue() {
}
}`
I stumbled into the same issue as our measurements consists of the same structure, but named dependent on the device id, e.g. "measurement1", "measurement2" ... A generic solution would be really nice as I don't see another way to solve this.
I use toPOJO(queryResult, clazz, measurementName) method.
My POJO like this.
public class InfluxResult {
@TimeColumn
@Column(name = "time")
private Instant time;
@Column(name = "val")
private Double val;
}
Use toPOJO.
//Query data from InfluxDB
QueryResult queryResult = influxDB.query(...);
//Convert QueryResult to POJO
String measurement = "measurement1";
InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
List<InfluxResult> results = resultMapper.toPOJO(queryResult, InfluxResult.class, measurement );