0

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.

1
  • Do you get any progress? And if you agree with my opinion, could you pls accept it as the answer? Looking forward for your reply if there's any further questions on this case. Commented Mar 23, 2021 at 2:10

1 Answer 1

1

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:

enter image description here

So, obviously it can got any response so that the result of readAllItems returns none. You'd better to use queryItems directly.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.