Cosmos DB - 创建分区键时出错 "Cross partition query is required but disabled"
Cosmos DB - Getting error while creating a partition key "Cross partition query is required but disabled"
我在 cosmos DB 中创建分区键时遇到以下错误。
Exception while executing function: SetUserSubscriptions -> Cross
partition query is required but disabled. Please set
x-ms-documentdb-query-enablecrosspartition to true, specify
x-ms-documentdb-partitionkey, or revise your query to avoid this
exception.\r\nActivityId: 4685a5b7-bce9-4855-b2d8-33353f2957d9,
Microsoft.Azure.Documents.Common/2.2.0.0, documentdb-dotnet-sdk/2.1.3
Host/32-bit MicrosoftWindowsNT/6.2.9200.0"
这是我的代码:
public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByDesc,int takeCount =-1)
{
;
var criteria = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId))
.Where(predicate)
.OrderByDescending(orderByDesc)
.AsDocumentQuery();
IDocumentQuery<T> query = criteria;
List<T> results = new List<T>();
while (query.HasMoreResults)
{
if (takeCount>-1 && results.Count >= takeCount)
{
break;
}
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
private static async Task CreateCollectionIfNotExistsAsync()
{
try
{
await client.ReadDocumentCollectionAsync(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));
}
catch (DocumentClientException e)
{
if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await client.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri(DatabaseId),
new DocumentCollection
{
Id = CollectionId,
IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }),
PartitionKey = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection<string> { GetPartitionKeyAttributeCosmoDbCollection(typeof(T)) } }
},
new RequestOptions { OfferThroughput = 1000 });
}
else
{
throw;
}
}
}
public static string GetPartitionKeyAttributeCosmoDbCollection(Type t)
{
// Get instance of the attribute.
CosmoDbCollection attribute =
(CosmoDbCollection)Attribute.GetCustomAttribute(t, typeof(CosmoDbCollection));
if (attribute == null)
{
throw new Exception("The attribute CosmoDbCollection was not found.");
}
return attribute.PartitionKey;
}
我添加 new FeedOptions { EnableCrossPartitionQuery=true}
后问题得到解决
public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByDesc, int takeCount = -1)
{
var criteria = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),new FeedOptions { EnableCrossPartitionQuery=true})
.Where(predicate)
.OrderByDescending(orderByDesc)
.AsDocumentQuery();
IDocumentQuery<T> query = criteria;
List<T> results = new List<T>();
while (query.HasMoreResults)
{
if (takeCount > -1 && results.Count >= takeCount)
{
break;
}
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
如 comment 中所述,您需要使用 Feed 选项启用 Cross partition query
,
var criteria = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),new FeedOptions { EnableCrossPartitionQuery=true})
我在 cosmos DB 中创建分区键时遇到以下错误。
Exception while executing function: SetUserSubscriptions -> Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.\r\nActivityId: 4685a5b7-bce9-4855-b2d8-33353f2957d9, Microsoft.Azure.Documents.Common/2.2.0.0, documentdb-dotnet-sdk/2.1.3 Host/32-bit MicrosoftWindowsNT/6.2.9200.0"
这是我的代码:
public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByDesc,int takeCount =-1)
{
;
var criteria = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId))
.Where(predicate)
.OrderByDescending(orderByDesc)
.AsDocumentQuery();
IDocumentQuery<T> query = criteria;
List<T> results = new List<T>();
while (query.HasMoreResults)
{
if (takeCount>-1 && results.Count >= takeCount)
{
break;
}
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
private static async Task CreateCollectionIfNotExistsAsync()
{
try
{
await client.ReadDocumentCollectionAsync(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId));
}
catch (DocumentClientException e)
{
if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await client.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri(DatabaseId),
new DocumentCollection
{
Id = CollectionId,
IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }),
PartitionKey = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection<string> { GetPartitionKeyAttributeCosmoDbCollection(typeof(T)) } }
},
new RequestOptions { OfferThroughput = 1000 });
}
else
{
throw;
}
}
}
public static string GetPartitionKeyAttributeCosmoDbCollection(Type t)
{
// Get instance of the attribute.
CosmoDbCollection attribute =
(CosmoDbCollection)Attribute.GetCustomAttribute(t, typeof(CosmoDbCollection));
if (attribute == null)
{
throw new Exception("The attribute CosmoDbCollection was not found.");
}
return attribute.PartitionKey;
}
我添加 new FeedOptions { EnableCrossPartitionQuery=true}
public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByDesc, int takeCount = -1)
{
var criteria = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),new FeedOptions { EnableCrossPartitionQuery=true})
.Where(predicate)
.OrderByDescending(orderByDesc)
.AsDocumentQuery();
IDocumentQuery<T> query = criteria;
List<T> results = new List<T>();
while (query.HasMoreResults)
{
if (takeCount > -1 && results.Count >= takeCount)
{
break;
}
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
如 comment 中所述,您需要使用 Feed 选项启用 Cross partition query
,
var criteria = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),new FeedOptions { EnableCrossPartitionQuery=true})