Issue
I encountered some obstackles with getting results from cosmos db sql api
using methods:
.readAllItems(partitionKey, classType)
.readAllItems(partitionKey, options, classType)
Above methods work fine when I have flat structure of class. I mean no subclasses nested inside my model class. Below is class example, which does not work:
public class Root {
public Root(){};
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Subclass getSubclass () {
return subclass;
}
public void setSubclass (Subclass subclass) {
this.subclass= subclass;
}
private String id = "";
private Subclass subclass= new Subclass ();
}
My subclass just contains partitionKey for simplicity.
public class Subclass {
public String getPk() { return pk; }
public void setPk(String pk) {
this.pk = pk;
}
private String pk="";
}
With such "structure" I can easily create items inside container, with proper values of partition key, but I am not able to load them again just using partitionKey
value. Here is code snippet for reading them from cosmosdb
.
PartitionKey partitionKey = new PartitionKey("properValueWhichWorksWithNoNestedClassesInsideRoot");
CosmosPagedFlux<Root> pagedFluxResponse = container.readAllItems(partitionKey, Root.class);
try {
double totalRequestCharge = pagedFluxResponse.byPage(preferredPageSize).flatMap(fluxResponse -> {
entities.addAll(fluxResponse.getResults());
return Mono.just(fluxResponse.getRequestCharge());
})
.reduce(0.0, (x1, x2) -> x1 + x2)
.block();
logger.info("Query charge: {}", totalRequestCharge);
} catch(Exception err) {
err.printStackTrace();
}
return entities;
I am not quite sure if I am doing sth wrong or it is bug, beacuse I use the same code with .queryItems(query, queryOptions, classType)
, which also returns CosmosPageFlux
instead of .readAllItems
and it works perfectly fine.
I use such dependancy:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>LATEST</version>
</dependency>
I also used some specific versions intead of LATEST
and it does not work.
Solution
I've tested in my side and found some interesting things.
readAllItems
is a function which achieved by SqlQuery, so when I debug your situation which put the partition key in a sub property, it will generate a sql like this:
So, obviously it can got any response so that the result of readAllItems returns none. You'd better to use queryItems directly.
Answered By - tiny-wa
Answer Checked By - Robin (JavaFixing Admin)