-
-
Save deborahc/8c7f0dcd149d5b12c2c27f393d8d9041 to your computer and use it in GitHub Desktop.
// 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; | |
} | |
} |
When I run this it returns 0 results. Even though I have data in the Cosmos DB.
@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 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
}
@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
@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?
ReadNextAsync can return 0 results. Would it be better to show it looping until it get's a result?