Skip to content

Instantly share code, notes, and snippets.

@deborahc
Last active November 10, 2023 20:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save deborahc/8c7f0dcd149d5b12c2c27f393d8d9041 to your computer and use it in GitHub Desktop.
Save deborahc/8c7f0dcd149d5b12c2c27f393d8d9041 to your computer and use it in GitHub Desktop.
Azure Cosmos DB .NET SDK Version 3 Stream API with query example
// Using the stream API to read data, in the context of an ASP.NET Web API
namespace CosmosWebAPI.Controllers
{
[Produces("application/json")]
[Route("api/ProductReview")]
// GET: api/ProductReview/productId/continuationToken
[HttpGet("{productId}/{continuationToken}", Name = "Get")]
// using the new stream api
public async Task<HttpResponseMessage> Query(string productId, string continuationToken)
{
var result = new HttpResponseMessage();
// Read a single query page from Azure Cosmos DB as stream
QueryDefinition query = new QueryDefinition("SELECT * FROM Reviews r WHERE r.productId = @id")
.WithParameter("@id", productId);
var queryIterator = this.container.GetItemQueryStreamIterator(query, continuationToken);
var queryResponse = await queryIterator.ReadNextAsync();
// Pass stream directly to response object, without deserializing
result.StatusCode = queryResponse.StatusCode;
result.Content = new StreamContent(queryResponse.Content);
result.Headers.Add("continuationToken", queryResponse.Headers.ContinuationToken);
return result;
}
}
@j82w
Copy link

j82w commented Jul 25, 2019

ReadNextAsync can return 0 results. Would it be better to show it looping until it get's a result?

@Jeremywhiteley
Copy link

When I run this it returns 0 results. Even though I have data in the Cosmos DB.

@j82w
Copy link

j82w commented Jul 15, 2020

@Jeremywhiteley please look at the following samples to see how to fully drain the query. Empty pages are possible and expected. In most scenarios it just means the result for a single partition came back with 0 results. You need to full drain the query to check the rest of the partitions.

@Jeremywhiteley
Copy link

Jeremywhiteley commented Jul 15, 2020

@Jeremywhiteley please look at the following samples to see how to fully drain the query. Empty pages are possible and expected. In most scenarios it just means the result for a single partition came back with 0 results. You need to full drain the query to check the rest of the partitions.

The above code should return data. Maybe I am confused. What would the Postman request look like?

This is what I get returned, but It does't have any data.

{
"Version": {
"Major": 1,
"Minor": 1,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"Content": {
"Headers": []
},
"StatusCode": 200,
"ReasonPhrase": "OK",
"Headers": [
{
"Key": "continuationToken",
"Value": [
""
]
}
],
"TrailingHeaders": [],
"RequestMessage": null,
"IsSuccessStatusCode": true
}

@fazil1987
Copy link

fazil1987 commented Nov 5, 2023

@j82w I am still finding it hard to stream the content directly without deserializing. I am getting the same result as @Jeremywhiteley. It'd be great if we have a working sample which we can leverage to stream data.

It's an exciting feature but without proper samples it's very hard to implement.

The working samples shared above doesn't have a proper sample to return from web api without intermediate serialization

@j82w
Copy link

j82w commented Nov 10, 2023

@fazil1987 is there a github project to see a sample/project you are trying to use it in? Trying to understand what the problem is with the sample above. Is it the empty pages?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment