public async Task <TDoc> GetOneAsync <TDoc>(QueryDefinition query, Microsoft.Azure.Cosmos.PartitionKey partitionKey) { var container = await GetContainerAsync(); List <TDoc> results = new List <TDoc>(); FeedIterator <TDoc> resultSetIterator = container.GetItemQueryIterator <TDoc>(query, requestOptions: new QueryRequestOptions() { PartitionKey = partitionKey }); while (resultSetIterator.HasMoreResults) { Microsoft.Azure.Cosmos.FeedResponse <TDoc> response = await resultSetIterator.ReadNextAsync(); results.AddRange(response); if (response.Diagnostics != null) { _logger.LogError($"QueryWithSqlParameters Diagnostics: {response.Diagnostics.ToString()}"); } break; } var item = results.FirstOrDefault(); return(item); }
public async Task <IEnumerable <PersistedGrant> > GetAllAsync(string subjectId) { QueryDefinition query = new QueryDefinition("SELECT * FROM operational f WHERE f.subjectId = @subjectId") .WithParameter("@subjectId", subjectId); List <PersistedGrantCosmosDocument> results = new List <PersistedGrantCosmosDocument>(); var container = await _simpleItemDbContext.GetContainerAsync(); FeedIterator <PersistedGrantCosmosDocument> resultSetIterator = container.GetItemQueryIterator <PersistedGrantCosmosDocument>(query, requestOptions: new QueryRequestOptions() { }); while (resultSetIterator.HasMoreResults) { Microsoft.Azure.Cosmos.FeedResponse <PersistedGrantCosmosDocument> response = await resultSetIterator.ReadNextAsync(); results.AddRange(response); if (response.Diagnostics != null) { Console.WriteLine($"\nQueryWithSqlParameters Diagnostics: {response.Diagnostics.ToString()}"); } } var persistedGrants = from item in results select item.ToPersistedGrant(); return(persistedGrants); }
private async Task QueryItemsAsync() { var query = "SELECT * FROM c WHERE c.id = '123'"; QueryDefinition queryDefinition = new QueryDefinition(query); FeedIterator <dynamic> queryResultSetIterator = this.container.GetItemQueryIterator <dynamic>(queryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new Microsoft.Azure.Cosmos.PartitionKey("123") }); List <dynamic> items = new List <dynamic>(); while (queryResultSetIterator.HasMoreResults) { Microsoft.Azure.Cosmos.FeedResponse <dynamic> currentResultSet = await queryResultSetIterator.ReadNextAsync(); string diagDataStr = currentResultSet.Diagnostics.ToString(); DiagData data = JsonConvert.DeserializeObject <DiagData>(diagDataStr); StringBuilder sb = new StringBuilder(); sb.AppendLine($"Total Elapsed time:\t{data.Summary.TotalElapsedTimeInMs / 1000} s"); sb.AppendLine($"Total Request count:\t{data.Summary.TotalRequestCount}"); sb.AppendLine($"Create Query Time:\t{data.Context[0].ElapsedTimeInMs / 1000} s"); sb.AppendLine($"Service Interop Time:\t{data.Context[1].ElapsedTimeInMs / 1000} s"); sb.AppendLine("Query Metrics:"); var queryMetrics = data.Context[2].QueryMetric.Split(';'); foreach (string metric in queryMetrics) { sb.AppendLine($"\t{metric}"); } string physicalStore = data .Context[2] .ContextDetailed .Last(a => a.StoreResult != null) .StoreResult .Split(' ')[1]; sb.AppendLine($"\t{physicalStore}"); Console.WriteLine(sb.ToString()); foreach (dynamic item in currentResultSet) { items.Add(item); Console.WriteLine("\tRead {0}\n", item); } } }
private async Task <IEnumerable <Journey> > _ExecuteIteratorQuery(string sqlQueryText) { QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText); FeedIterator <Journey> queryResultSetIterator = this._container.GetItemQueryIterator <Journey>(queryDefinition); List <Journey> journeys = new List <Journey>(); while (queryResultSetIterator.HasMoreResults) { Microsoft.Azure.Cosmos.FeedResponse <Journey> currentResultSet = await queryResultSetIterator.ReadNextAsync(); foreach (Journey journey in currentResultSet) { journeys.Add(journey); } } return(journeys); }
async Task <TDoc> GetOneAsync <TDoc>(QueryDefinition query, PartitionKey partitionKey) { List <TDoc> results = new List <TDoc>(); FeedIterator <TDoc> resultSetIterator = _simpleItemDbContext.Container.GetItemQueryIterator <TDoc>(query, requestOptions: new QueryRequestOptions() { PartitionKey = partitionKey }); while (resultSetIterator.HasMoreResults) { Microsoft.Azure.Cosmos.FeedResponse <TDoc> response = await resultSetIterator.ReadNextAsync(); results.AddRange(response); if (response.Diagnostics != null) { Console.WriteLine($"\nQueryWithSqlParameters Diagnostics: {response.Diagnostics.ToString()}"); } break; } var item = (TDoc)results.FirstOrDefault(); return(item); }
private async Task RemoveAllAsync(QueryDefinition query) { try { List <Task <ItemResponse <PersistedGrantCosmosDocument> > > deleteTasks = new List <Task <ItemResponse <PersistedGrantCosmosDocument> > >(); bool bContinue = true; while (bContinue) { bContinue = false; List <PersistedGrantCosmosDocument> results = new List <PersistedGrantCosmosDocument>(); var container = await _simpleItemDbContext.GetContainerAsync(); FeedIterator <PersistedGrantCosmosDocument> resultSetIterator = container.GetItemQueryIterator <PersistedGrantCosmosDocument>(query, requestOptions: new QueryRequestOptions() { }); if (resultSetIterator.HasMoreResults) { Microsoft.Azure.Cosmos.FeedResponse <PersistedGrantCosmosDocument> response = await resultSetIterator.ReadNextAsync(); results.AddRange(response); if (response.Diagnostics != null) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"\nQueryWithSqlParameters Diagnostics: {response.Diagnostics}"); } } if (results.Any()) { foreach (var item in results) { deleteTasks.Add(ExecuteDeleteAsync(container, item)); } await Task.WhenAll(deleteTasks); bContinue = true; } } } } catch (Exception ex) { return; } }