本文介绍了COSMOS DB批量插入Initialize()无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

自过去一周以来,我正在检查用COSMOS DB替换现有SQL服务器数据库的可行性对于我的项目,

Since past 1 week, i am checking feasibility to replace my Existing SQL server DB with COSMOS DB for my project,

在那,我的一个实验是使用COSMOS DB SQL API更新近1M记录的批量插入概念

In that, one of my experiment is Bulk insert concepts to update nearly 1M records using COSMOS DB SQL API

我推荐如下链接,但我没有得到执行intializeasync(),例外是该值不能为空,参数名是'键'

I referred following link, but i am failed to get execute intializeasync(), the exception is The value cannot be null,Parametername is 'Key'

github.com/Azure/azure-cosmosdb-bulkexecutor- dotnet-getting-started / blob / master / BulkImportSample / BulkImportSample / Program.cs

github.com/Azure/azure-cosmosdb-bulkexecutor-dotnet-getting-started/blob/master/BulkImportSample/BulkImportSample/Program.cs

版本是: Microsoft.Azure.CosmosDB -   2.1.3

Version is : Microsoft.Azure.CosmosDB - 2.1.3

我的代码如下,

 私人DocumentClient客户;

 private DocumentClient client;

DocumentCollection collectionInfo;

        private static readonly ConnectionPolicy ConnectionPolicy = new ConnectionPolicy

        {

            ConnectionMode = ConnectionMode.Direct,

            ConnectionProtocol = Protocol.Tcp

        };

DocumentCollection collectionInfo;
        private static readonly ConnectionPolicy ConnectionPolicy = new ConnectionPolicy
        {
            ConnectionMode = ConnectionMode.Direct,
            ConnectionProtocol = Protocol.Tcp
        };

                private void StartBulk()

                private void StartBulk()

{

                          this.client = new DocumentClient(new Uri(EndpointUri),AuthKey,ConnectionPolicy);



           数据库databaseInfo =新数据库{Id = DatabaseId};

            await this.client.CreateDatabaseIfNotExistsAsync(databaseInfo);



            collectionInfo = new DocumentCollection();

            collectionInfo.Id = ContainerId;

            PartitionKeyDefinition partitionKey = new PartitionKeyDefinition

            {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Paths = new Collection< string> {PartitionKey}
$


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; };

                         this.client = new DocumentClient(new Uri(EndpointUri), AuthKey, ConnectionPolicy);

            Database databaseInfo = new Database { Id = DatabaseId };
            await this.client.CreateDatabaseIfNotExistsAsync(databaseInfo);

            collectionInfo = new DocumentCollection();
            collectionInfo.Id = ContainerId;
            PartitionKeyDefinition partitionKey = new PartitionKeyDefinition
            {
                Paths = new Collection<string> { PartitionKey }

            };

await this.client.CreateDocumentCollectionIfNotExistsAsync(

       ;   UriFactory.CreateDatabaseUri(databaseInfo.Id),

         collectionInfo,

          new RequestOptions {OfferThroughput = 400});

await this.client.CreateDocumentCollectionIfNotExistsAsync(
                UriFactory.CreateDatabaseUri(databaseInfo.Id),
                collectionInfo,
                new RequestOptions { OfferThroughput = 400 });

client.ConnectionPolicy.RetryOptions .MaxRetryWaitTimeInSeconds = 30;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; client.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 9;



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; IBulkExecutor bulkExecutor = new BulkExecutor(client,collectionInfo);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;等待bulkExecutor.InitializeAsync();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var tokenSource = new CancellationTokenSource();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var token = tokenSource.Token;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //将重试次数设置为0以将完全控制权传递给批量执行者。

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; client.ConnectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds = 0;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; client.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 0;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; BulkImportResponse bulkImportResponse = await bulkExecutor.BulkImportAsync(

  documents:result,

  enableUpsert:true,

  disableAutomaticIdGeneration:true,

  maxConcurrencyPerPartitionKeyRange:null,

  maxInMemorySortingBatchSize:null,

  cancellationToken:token);

client.ConnectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds = 30;
            client.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 9;

            IBulkExecutor bulkExecutor = new BulkExecutor(client, collectionInfo);

            await bulkExecutor.InitializeAsync();
            var tokenSource = new CancellationTokenSource();
            var token = tokenSource.Token;
            // Set retries to 0 to pass complete control to bulk executor.
            client.ConnectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds = 0;
            client.ConnectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 0;
            BulkImportResponse bulkImportResponse = await bulkExecutor.BulkImportAsync(
  documents: result,
  enableUpsert: true,
  disableAutomaticIdGeneration: true,
  maxConcurrencyPerPartitionKeyRange: null,
  maxInMemorySortingBatchSize: null,
  cancellationToken: token);

}

异常是,例外是,该值不能为空,参数名称为'Key'

Exception is, the exception is, The value cannot be null,Parameter name is 'Key'

请帮我解决,

谢谢,

Thiru

推荐答案

需要调查的两个方面:

  • Do you have a partition key configured for your collection? 
  • In BulkImportSample/BulkImportSample/App.config you are missing an (or have an incorrect) entry in appSettings:
  <appSettings>
    <add key="EndPointUrl" value="https://localhost:443/" />
    <add key="AuthorizationKey" value="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" />
    <add key="DatabaseName" value="TestDb" />
    <add key="CollectionName" value="TestColl" />
    <add key="CollectionThroughput" value="50000" />
    <add key="ShouldCleanupOnStart" value="true" />
    <add key="ShouldCleanupOnFinish" value="true" />
    <add key="NumberOfDocumentsToImport" value="200000" />
    <add key="NumberOfBatches" value="2" />
    <add key="CollectionPartitionKey" value="/profileid" />
    <add key="SampleDocumentTemplateFile" value="SampleDocumentTemplate.json" />
  </appSettings>

如果您有其他问题,请告诉我们。谢谢,〜迈克

Please let us know if you have additional questions. Thanks, ~Mike


这篇关于COSMOS DB批量插入Initialize()无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 18:50