我想使用Container.CreateTransactionlBatch进行大容量插入操作。目前我正在使用container.CreateItemAsync方法来做这件事,这需要更多的时间。
有没有可能用Container.CreateTransactionalBatch取代container.CreateItemAsync?
private async Task AddSubscription(EnableOrDisableSubscriptionCommand command, SubscriptionAction subscriptionAction, IList<int> notificationCategoryTypes)
{
List<Task> bulkOperations = new List<Task>();
foreach (var notificationCategory in notificationCategoryTypes)
{
var notificationTypes = Utility.GetNotificationTypes((NotificationCategoryType)notificationCategory);
foreach (var notificationType in notificationTypes)
{
foreach (var payerAccountSubscriptions in command.Subscriptions)
{
if (payerAccountSubscriptions.AccountNumbers?.Any() ?? false)
{
foreach (var accountNumber in payerAccountSubscriptions.AccountNumbers.Where(a => !string.IsNullOrEmpty(a)))
{
bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, accountNumber, command.UserRole,
command.UserId));
}
}
else
{
bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, null, command.UserRole,
command.UserId));
}
}
}
}
await Task.WhenAll(bulkOperations);
}
public async Task<ItemResponse<Subscription>> Create(SubscriptionAction subscriptionAction, NotificationType notificationType,
int colCoId, string payerNumber, string accountNumber, UserRole userRole, string userId, string cardId = null)
{
var eventType = Utility.GetEventType(notificationType);
var subscriptionBase = new Subscription
{
Id = Guid.NewGuid(),
IsActive = true,
Action = subscriptionAction,
ActionDesc = subscriptionAction.ToString(),
Version = (int)SubscriptionVersion.V2,
NotificationType = notificationType,
NotificationTypeDesc = notificationType.ToString(),
EventType = eventType,
EventTypeDesc = eventType.ToString(),
ColCoId = colCoId,
PayerNumber = payerNumber,
AccountNumber = accountNumber,
CardId = cardId,
DistributionGroups = new List<string> { userRole.ToString() },
DistributionUserIds = new List<string> { userId }
};
return await CreateItemAsync(subscriptionBase);
}
public async Task<ItemResponse<T>> CreateItemAsync(T item)
{
return await _container.CreateItemAsync<T>(item);
}发布于 2020-08-06 23:31:54
这里有两个不同的东西。
根据你正在分享的代码,看起来你正在尝试使用批量?如果是这样的话,您需要确保您的CosmosClient实例是使用具有Bulk on的CosmosClientOptions创建的:
CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
CosmosClient cosmosclient = new CosmosClient(connectionString, options);在您的代码中,您可能还希望从其他方法中删除await,而只返回Task,例如:
public Task<ItemResponse<Subscription>> Create(SubscriptionAction subscriptionAction, NotificationType notificationType,
int colCoId, string payerNumber, string accountNumber, UserRole userRole, string userId, string cardId = null)
{
// ... OTHER CODE
return CreateItemAsync(subscriptionBase);
}
public Task<ItemResponse<T>> CreateItemAsync(T item)
{
return _container.CreateItemAsync<T>(item);
}https://stackoverflow.com/questions/63283664
复制相似问题