简体   繁体   English

Cosmos DB扇出查询问题

[英]Cosmos DB fan out query issue

I recently started moving our CosmosDB single partitioned collection to new logical partitioned collection. 我最近开始将CosmosDB单个分区集合移动到新的逻辑分区集合。 I am doing this in Phases, where I have already covered the all the create and update operation with the support of the transaction. 我在“阶段”中执行此操作,在“阶段”中,我已经介绍了在事务支持下的所有创建和更新操作。 Now currently, I am going to fan out a query for all read operations and I will fix this fan out in few coming weeks. 目前,我将对所有读取操作展开查询,并将在未来几周内解决此问题。 However, In my current scenario, I am having one issue in the query. 但是,在当前情况下,查询中存在一个问题。 Below is the code sample of my select using C# LINQ and then I will explain u my issue in few steps 下面是使用C#LINQ选择的代码示例,然后我将通过几个步骤向您解释我的问题

Code without Partition key 不带分区键的代码

using (var query = _client.CreateDocumentQuery<User>(_documentCollectionUri,
                    new FeedOptions { EnableCrossPartitionQuery = true })
                                          .Where(u => u.Email == emailAddress.ToLower())
                                          .AsDocumentQuery())
                {
                    if (query.HasMoreResults)
                    {
                        var response = await query.ExecuteNextAsync<User>();
                        return response.FirstOrDefault(); //always returning null 
                    }
                }
                return null;

Now in the code above where no partition key is provided with cross partition query enabled it goes inside the if block but returning null all the time. 现在,在上面的代码中,未启用跨分区查询的情况下未提供分区键,它将进入if块内,但始终返回null。 To make sure that I have got the data in my collection, I checked the client query by doing the query.ToString() and ran the query in Azure DB explorer and it worked and returned the record but it is always returning null in my code above which tells me that something is wrong with the client or it is not working with cross partitioned query. 为确保我已在集合中获取数据,我通过执行query.ToString()检查了客户端查询,并在Azure DB资源管理器中运行了查询,该查询可以正常工作并返回记录,但在我的代码中始终返回null上面的内容告诉我客户端出了点问题,或者不适用于跨分区查询。

Code with Partition key 带分区键的代码

using (var query = _client.CreateDocumentQuery<User>(_documentCollectionUri,
                    new FeedOptions { EnableCrossPartitionQuery = true, PartitionKey = new PartitionKy(“myemail@email.com”) })
                                          .Where(u => u.Email == emailAddress.ToLower())
                                          .AsDocumentQuery())
                {
                    if (query.HasMoreResults)
                    {
                        var response = await query.ExecuteNextAsync<User>();
                        return response.FirstOrDefault(); //always returning null 
                    }
                }
                return null;

Now this code given above is working fine. 现在,上面给出的代码可以正常工作。

Code to get client instance is below 获取客户端实例的代码如下

_client = new DocumentClient
                        (
                            new Uri(Configuration["azureSettings:documentDb:endPoint"]),
                            Configuration["azureSettings:documentDb:key"],
                            _jsonSerializerSettings,
                            new ConnectionPolicy
                            {
                                EnableEndpointDiscovery = false,
                                ConnectionMode = ConnectionMode.Direct,
                                ConnectionProtocol = Microsoft.Azure.Documents.Client.Protocol.Tcp,
                            }
                        );

After waiting for a while and reply from the Azure Cosmos DB team. 等待一会儿并从Azure Cosmos DB团队回复。 I now understand why my fanout query was not working. 我现在知道为什么我的扇出查询不起作用。 All I have to do is change the code a little bit to make sure it will keep looking for the data until the end and then bail out with a result or no result. 我要做的就是稍微更改代码,以确保它将一直在寻找数据,直到最后,然后以结果还是没有结果来解决。 So to get it to work I have changed the code below 因此,为了使其正常工作,我更改了以下代码

if (query.HasMoreResults)
{
   var response = await query.ExecuteNextAsync<User>();
   return response.FirstOrDefault(); //always returning null 
}

With the code 用代码

 var results = new List<MyClass>();
 while(query.HasMoreResults)
 {
     results.AddRange(await query.ExecuteNextAsync<MyClass>());
 }
 return results.FirstOrDefault(x => x.MyProp== propValue.ToLower());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM