首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Container.CreateTransactionalBatch替换Container.CreateItemAsync

用Container.CreateTransactionalBatch替换Container.CreateItemAsync
EN

Stack Overflow用户
提问于 2020-08-06 20:29:33
回答 1查看 82关注 0票数 0

我想使用Container.CreateTransactionlBatch进行大容量插入操作。目前我正在使用container.CreateItemAsync方法来做这件事,这需要更多的时间。

有没有可能用Container.CreateTransactionalBatch取代container.CreateItemAsync?

代码语言:javascript
复制
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);
        }
EN

回答 1

Stack Overflow用户

发布于 2020-08-06 23:31:54

这里有两个不同的东西。

根据你正在分享的代码,看起来你正在尝试使用批量?如果是这样的话,您需要确保您的CosmosClient实例是使用具有Bulk on的CosmosClientOptions创建的:

代码语言:javascript
复制
CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
CosmosClient cosmosclient = new CosmosClient(connectionString, options);

在您的代码中,您可能还希望从其他方法中删除await,而只返回Task,例如:

代码语言:javascript
复制
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);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63283664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档