Use the SDK multi-value data model to query the latest data

Updated at:
Copy as MD

Use multiFieldQueryLast() to retrieve the most recent data point for each field of a metric. Unlike the single-value model, you must specify the fields when building the LastPointSubQuery.

Parameters

ParameterRequiredDescription
metricYesThe metric name, for example, "wind"
fieldsYesOne or more fields of the metric, for example, "speed", "level", "temperature"
tagsNoTag-based filter conditions. Pass Collections.emptyMap() to skip tag filtering
tupleFormatNoWhether to return results in tuple format
msResolutionNoWhether to use millisecond resolution for timestamps

Query the latest data

The following example queries the latest values of the speed, level, and temperature fields from the wind metric, with no tag filtering.

Step 1: Build the field list.

List<String> fields = new ArrayList<>();
fields.add("speed");
fields.add("level");
fields.add("temperature");

Step 2: Create a LastPointSubQuery with the metric, fields, and tag conditions.

// Pass Collections.emptyMap() as the tags argument when no tag filtering is needed.
LastPointSubQuery lastPointSubQuery = LastPointSubQuery.builder("wind", fields, Collections.emptyMap()).build();

Step 3: Build the LastPointQuery.

LastPointQuery lastPointQuery = LastPointQuery.builder()
    .sub(lastPointSubQuery)
    .tupleFormat(true)
    .msResolution(false)
    .build();

Step 4: Call multiFieldQueryLast() and retrieve the results.

List<MultiFieldQueryLastResult> results = tsdb.multiFieldQueryLast(lastPointQuery);