How to iterate through MeasurementCollection

MeasurementFilter measurementFilter = new MeasurementFilter();
measurementFilter.bySource(new GId(“12345679”));
measurementFilter.byDate(startDate.toDate(), endDate.toDate());
measurementFilter.byFragmentType(“IsProgramRunning”);

   MeasurementCollection measurementCollection = measurementApi.getMeasurementsByFilter(measurementFilter);

I am getting measurements in measurementCollection type, How to display all the measurements in that collection in java

You can use a simple .get() on the collection and then loop over it as described in the doc:
https://cumulocity.com/guides/microservice-sdk/java/#accessing-events-and-measurements

1 Like

MeasurementFilter measurementFilter = new MeasurementFilter();
measurementFilter.bySource(new GId(“1234567”));
measurementFilter.byDate(startDate.toDate(), endDate.toDate());

    System.out.println("measurementCollection Filter");
    MeasurementCollection mc = measurementApi.getMeasurementsByFilter(measurementFilter);

    MeasurementCollectionRepresentation mcr;
    
    for (mcr = mc.get(); mcr != null; mcr = mc.getNextPage(mcr)) {
        for (MeasurementRepresentation mp : mcr.getMeasurements()) {
            System.out.println(mp);
        }
        System.out.println("2nd loop");
    }

Going in infinite loop

Why not using an iterator?

measurementApi.getMeasurementsByFilter(measurementFilter).get().allPages().forEach(measurement -> {
    System.out.println(measurement);
});
2 Likes

I assume Vaibhavi was following the code example from the doc: Microservice SDK for Java - Cumulocity IoT Guides - even if the code is not as nice as your example, I dont know why that would result in an infinite loop…

Which loop goes into the inifinite loop? I assume the outer one? Does it print “2nd loop” continously?

Yes, Outer Loop, Its printing 2nd loop continuously.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.