TransWikia.com

Get Item ID in a container of Cosmos DB

Stack Overflow Asked by rohini on November 18, 2021

I want to print the content of ID in Items of a container in Cosmos DB (SQL) from Java.
I tried container.queryItem() where the sql was "select containerName.id from containerName" but I was getting a not found exception.

Original Code:
CosmosPagedIterable familiesPagedIterable = container.queryItems(
"SELECT Family.id FROM Family", queryOptions, Family.class);

    familiesPagedIterable.iterableByPage(10).forEach(cosmosItemPropertiesFeedResponse -> {
        System.out.println("Got a page of query result with " +
            cosmosItemPropertiesFeedResponse.getResults().size() + " items(s)"
            + " and request charge of " + cosmosItemPropertiesFeedResponse.getRequestCharge());

        System.out.println("Item Ids " + cosmosItemPropertiesFeedResponse
            .getResults()
            .stream()
            .map(Family::getId)
            .collect(Collectors.toList()));
    });

Above code prints all the entires in the items, what I want is to get the item id present in Family, without any Family model class created in java. Trying to get the items id for all the container present in the database

One Answer

Please try something like this:

    CosmosPagedIterable<String> familiesPagedIterable  = container.queryItems("select value c.id from c",queryOptions, String.class);
    familiesPagedIterable.iterableByPage(10).forEach(cosmosItemPropertiesFeedResponse -> {
        System.out.println("Got a page of query result with " +
            cosmosItemPropertiesFeedResponse.getResults().size() + " items(s)"
            + " and request charge of " + cosmosItemPropertiesFeedResponse.getRequestCharge());
    
        System.out.println("Item Ids " + cosmosItemPropertiesFeedResponse
                            .getResults()
                            .stream()
                            .collect(Collectors.toList()));
    });

Answered by Steve Zhao on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP