microsoft / spring-data-cosmosdb

Access data with Azure Cosmos DB
MIT License
94 stars 64 forks source link

Support `@Query` annotation from spring-data-commons #389

Closed Incarnation-p-lee closed 4 years ago

Incarnation-p-lee commented 5 years ago

Add related issue here.

ashishc commented 4 years ago

When is this feature coming?

linux-code commented 4 years ago

Hi , Could you please let me know , when this feature is supposed to be released?

Can we have custom query example using cosmosTemplate in readme.md file. I didn't find any documentation for this in readme.md file.

Thanks.

kushagraThapar commented 4 years ago

Hi , Could you please let me know , when this feature is supposed to be released?

Can we have custom query example using cosmosTemplate in readme.md file. I didn't find any documentation for this in readme.md file.

Thanks.

Hello @linux-code This feature is planned to release sometime around February. Regarding executing custom queries, you can get the cosmosClient bean from application context and execute custom queries. Here is an example to execute custom queries: https://github.com/microsoft/spring-data-cosmosdb/blob/840072ed130ba5cbf77c4f277883c148bdeaef2b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/PageableMemoRepositoryIT.java#L133

Since this is not a conventional way to execute custom queries, we don't like to document this in readme file. If you have any issues trying out the above approach, feel free to reach out.

anfeldma-ms commented 4 years ago

I am a collaborator with Kushagra on this. I just wanted to update that Query Annotation feature is WIP and we currently expect ETA of late May 2020.

BingjieGao commented 4 years ago

Using

        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>

Hi, also running into same issue with so called 'user execution query' without @Query annotation. @anfeldma-ms could you help on giving out the examples how to make validate return data value with the right appropriate defined type/POJO class.

I found few examples online documentation java example of queryItems and also example from official website

CosmosPagedIterable<Family> familiesPagedIterable = container.queryItems(
    "SELECT * FROM Family WHfERE Family.lastName IN ('Andersen', 'Wakefield', 'Johnson')", queryOptions, Family.class);

It's really confused how exactly this whole user defined query should be used with cosmos azure sql java starter. Found hard time playing around the return type on queryItems.

kushagraThapar commented 4 years ago

@BingjieGao Return type of queryItems API is just a pagination helper class which provides you with some APIs to work through pagination. When you call the pagination API, you will get the response type as FeedResponse object - which has getResults APIs, and many other APIs to deal with results.

//  <QueryItems>
        // Set some common query options
        FeedOptions queryOptions = new FeedOptions();
        //queryOptions.setEnableCrossPartitionQuery(true); //No longer necessary in SDK v4
        //  Set populate query metrics to get metrics around query executions
        queryOptions.setPopulateQueryMetrics(true);

        CosmosPagedIterable<Family> familiesPagedIterable = container.queryItems(
            "SELECT * FROM Family WHfERE Family.lastName IN ('Andersen', 'Wakefield', 'Johnson')", 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()));
        });
        //  </QueryItems>

So in this above examples, take a look at how iterableByPage() API returns cosmosItemPropertiesFeedResponse which is the actual FeedResponse which contains results from backend.

Please go through this getting started guide to know more on this: https://github.com/Azure-Samples/azure-cosmos-java-getting-started

BingjieGao commented 4 years ago

@kushagraThapar Hi I'm using version 2.3.1 on springboot azure cosmos starter while for some reason I didn't see if the queryItems returns such type but instead I got the working bits like:

        Flux<FeedResponse<CosmosItemProperties>> feedResponseFlux =
                cosmosClient.getDatabase(databaseName)
                        .getContainer(collectionName)
                        .queryItems(query, feedOptions);

        feedResponseFlux.subscribe(
                fluxResponse -> {
                    System.out.println(fluxResponse.results());
                }
        );

Also I was reading from official Microsoft documentation from here the example shows:


Flux<FeedResponse<CosmosItemProperties>> feedResponseFlux =
    cosmosClient.getDatabase(databaseId)
                .getContainer(collectionId)
                .queryItems(query, feedOptions);
    feedResponseFlux.subscribeOn(Schedulers.parallel())
                    .flatMap(feedResponse -> {
                        List<CosmosItemProperties> results =
                        feedResponseFlux.results();
                        log.info("Results are {}", results);
                        return feedResponse;
                    })
                    .subscribe();

With feedResponseFlux.results() it's getting me confused, is there something I was doing wrong as I could not find such method in Flux<FeedResponse<CosmosItemProperties>> type while instead I found feedResponse might have the same.

kushagraThapar commented 4 years ago

@BingjieGao my bad - what you got is right. For some reason I confused this version with V4 Java SDK . Ignore my comments over there. what you have got looks completely good.

Once spring-data-cosmosdb moves to V4 (hopefully by the end of this month) - then the newer model will come into play (which I have described above)

apescione commented 4 years ago

Which version of Spring data Cosmos will use Cosmos SDK V4? Only 2.3.x or even 2.2.x that at the moment I'm using it in my project? Just to understand if I need to plan also migration to Spring boot 2.3.x.

kushagraThapar commented 4 years ago

We are coming up with a new version schema for spring-data-cosmosdb. Current version schema has some limitations on the versions which will be fixed in new spring-data-cosmosdb version schema. Cosmos DB V4 support will land in this new version schema based spring-data-cosmosdb. Not in 2.2.x or 2.3.x

The timeframe is roughly by July end.

kushagraThapar commented 4 years ago

Closing it here as this is being tracked in a separate repo.