AnswerBun.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!

Related Questions

onMessageReceived() not being called | Discord Bot

1  Asked on February 7, 2021 by l0raxeo-c

     

Providing tabulated data to a SQL Update

0  Asked on February 7, 2021 by greg-b-roberts

     

Use list item index as index to another list in python

2  Asked on February 7, 2021 by jonna_983

     

how to create a persistence arraylist structure in java?

2  Asked on February 7, 2021 by pikachu-sir

   

IndexError: tuple index out of range when running python 3.9.1

1  Asked on February 7, 2021 by ahmad-nur-hasybi

       

Azure Functions in VS Code

2  Asked on February 7, 2021 by nje

     

Python __hash__ performance for bulky data

0  Asked on February 7, 2021 by user14848693

   

error: failed to push some refs to ‘https://github.com/

8  Asked on February 7, 2021 by koeradoera

     

Ask a Question

Get help from others!

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