Azure-Samples / azure-cosmos-java-sql-api-samples

Sample code for Azure Cosmos DB Java SDK for SQL API
MIT License
38 stars 70 forks source link

CosmosPagedIterable.byPage is loading multiple pages even if we set page sige #22

Open jayaprakashkaluva opened 3 years ago

jayaprakashkaluva commented 3 years ago

Followed the example given below, CosmosPagedIterable.byPage is loading multiple pages with single query and do while loop is getting executed only once.

String query = "SELECT * FROM Families";

    int pageSize = 100; //No of docs per page
    int currentPageNumber = 1;
    int documentNumber = 0;
    String continuationToken = null;

    double requestCharge = 0.0;

    // First iteration (continuationToken = null): Receive a batch of query response pages
    // Subsequent iterations (continuationToken != null): Receive subsequent batch of query response pages, with continuationToken indicating where the previous iteration left off
    do {
        logger.info("Receiving a set of query response pages.");
        logger.info("Continuation Token: " + continuationToken + "\n");

        CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();

        Iterable<FeedResponse<Family>> feedResponseIterator =
                container.queryItems(query, queryOptions, Family.class).iterableByPage(continuationToken,pageSize);

        for (FeedResponse<Family> page : feedResponseIterator) {
            logger.info(String.format("Current page number: %d", currentPageNumber));
             // Access all of the documents in this result page
            for (Family docProps : page.getResults()) {
                documentNumber++;
            }

            // Accumulate the request charge of this page
            requestCharge += page.getRequestCharge();

            // Page count so far
            logger.info(String.format("Total documents received so far: %d", documentNumber));

            // Request charge so far
            logger.info(String.format("Total request charge so far: %f\n", requestCharge));

            // Along with page results, get a continuation token
            // which enables the client to "pick up where it left off"
            // in accessing query response pages.
            continuationToken = page.getContinuationToken();

            currentPageNumber++;
        }

    } while (continuationToken != null);