Get last measurement not working as expected

snicoll
Sun, 17 Aug 2025 17:50:27 +0000
Client SDKs

I am trying to retrieve the last measurement using com.influxdb:influxdb-client-java:7.3.0.

Here’s the query:

QueryApi queryApi = this.influxDBClient.getQueryApi();
String flux = """
		from(bucket:"energy_meter")
			|> range(start: -2d, stop: now())
			|> filter(fn: (r) => r._measurement == "snapshot")
			|> last()
		""";
List<EnergySnapshotMeasurement> result = queryApi.query(flux, EnergySnapshotMeasurement.class);
return (result.size() == 1) ? result.getFirst() : null;

EnergySnapshotMeasurement is as follows:

@Measurement(name = "snapshot")
public static class EnergySnapshotMeasurement {

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

	@Column(name = "active_power")
	private int activePower;

	@Column(name = "in_t1")
	private Float inT1;

	@Column(name = "in_t2")
	private Float inT2;

	@Column(name = "out_t1")
	private Float outT1;

	@Column(name = "out_t2")
	private Float outT2;
}

Rather than a single result, I am getting a list of 5 entries where all fields are null. I understand my query is wrong and it’s retrieving one row per field rather than the last “entry” in the bucket. I don’t know how to adapt my query so that I can get back the last entry.

For the record, I am ingesting data as follows:

this.writeApi.writeMeasurement(WritePrecision.S, measurement);

Where measurement is an instance of `EnergySnapshotMeasurement.

11 posts - 2 participants