How to get the next page data in JAVA SDK

Product/components used and version/fix level:

Cumulocity Production

Detailed explanation of the problem:

Hi, I am trying to fetch MO ids. I used InventoryFilter.byIds method and passed an id to get the data. Then I used getManagedObjectsByFilter method and passed the filter object and .get(2000) to fetch 2000 records. Then I used PagedManagedObjectCollectionRepresentation to fetch the ids of the devices.

InventoryFilter filter = new InventoryFilter().byIds(new GId(id));
PagedManagedObjectCollectionRepresentation p = inventoryApi.getManagedObjectsByFilter(filter).get(2000);

Now here as you can see I am getting only 2000 records, but we also get a next variable and with that we can check if there are more records. So I need to fetch those records but I don’t know how?

Also, can you please suggest me if this is a good way to fetch the data or if there is any other better way to fetch the data.

Thanks & Regards,
Samanyu

Hi Samanyu,

a convenient way is to use the .allPages() method which returns an Iterable.
Within the get(2000) you can specify the page size - as you correctly did!

Iterable<ManagedObjectRepresentation> moList = inventoryApi.getManagedObjectsByFilter(filter).get(2000).allPages();
	
for (ManagedObjectRepresentation managedObjectRepresentation : moList) {
	// do stuff
}

I believe in your example you actually use a different filter correct? :slight_smile:

Depending on the total number of objects there are better ways to do so in terms of performance.

Regards
Kai

1 Like

Hi Kai,

I am actually using this filter only just without .allPages. Can you please tell me what better ways which can improve the performance?

Thanks & Regards,
Samanyu

Hi Samanyu,

filtering on a specific ID will always return at most 1 object. So there is no need to page the results, that is why I am asking.
The optimized way only works in case you are targeting to get all objects which match a given filter-query and do not require sorting.

Could you please elaborate what kind of objects and how many you will try to collect?
Then only I will be able to show the optimized way.

Thanks!

Hi Kai,

I have multiple use cases.

  1. So, one of the cases is related to MO, my devices are organized in a such a way like first there is a group then there is a sub group and in that subgroup there will be multiple devices. So how I am fetching the devices or the subgroups are I am directly fetching the devices through a filter Like :-
 InventoryFilter filter = new InventoryFilter().byIds(new GId(id));

PagedManagedObjectCollectionRepresentation p = inventoryApi.getManagedObjectsByFilter(filter).get(2000);

p.getManagedObjects().get(i).getChildAssets().getReferences().get(j).getManagedObject().getId().getValue()

So with this I get the subgroups, now i will run again the same code with the subgroup Ids and I will get the respective devices.

  1. The second use case is related to getting the measurements. So in this what I am fetching all the measurements. Now the measurements could be more than 2000, so as you suggested I will be using the .allPages().
MeasurementFilter measurementFilter = new MeasurementFilter().bySource(new GId("60464")).byType("type").byDate("", CommonUtil.currentTime());

PagedMeasurementCollectionRepresentation p = measurementApi.getMeasurementsByFilter(measurementFilter).get(1, RevertQueryParameter.getInstance());
  1. So for third case, i want to fetch all the devices based on a type. So what I am doing is :-
InventoryFilter deviceFilter = new InventoryFilter().byFragmentType("c8y_IsDevice");

PagedManagedObjectCollectionRepresentation p = inventoryApi.getManagedObjectsByFilter(deviceFilter).get(2000);
p.getManagedObjects().get(i).getId().getValue();

This way I will directly get the id of the MOs.

These are the use cases that I have, can you suggest optimization for all.

Thanks & Regards,
Samanyu

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