简体   繁体   中英

CosmosDB SQL API Java SDK v.4 ReadAllItems method does not return results

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.

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM