簡體   English   中英

C# 上傳數據到 Azure Cosmos DB SQL Z8A5DA52ED1260421D359E7AAZ0C

[英]C# to upload data to Azure Cosmos DB SQL api

我有以下部分代碼可以將數據上傳到 CosmosDB Mongo API。 現在我正在使用 Cosmos Client 將文檔上傳到 Cosmos DB SQL API。 但是,SQL api 中不支持以下行。

        var item = objs[i];
        var doc = BsonDocument.Parse(item.ToString());                    
        //saving all  the documents in cosmos
        listTask.Add(collection.ReplaceOneAsync(
            filter: new BsonDocument("id", doc["id"]),
            options: new ReplaceOptions { IsUpsert = true },
            replacement: doc));

是否可以更換 SQL API?

private async Task UploadComplete(List<object> objs)
{
    double total_cycles = (int)((double)objs.Count / BATCH);
    
    var cycle= 0;
    //configuring cosmos connection-SQL
    CosmosClient client = new CosmosClient(cosmos);
    var database = client.GetDatabase(cosmos_database);
    var collection = database.GetContainer(cosmos_collection);

           //works for Cosmos DB Mongo API
            var item = objs[i];
            var doc = BsonDocument.Parse(item.ToString());                    
            //saving all  the documents in cosmos
            listTask.Add(collection.ReplaceOneAsync(
                filter: new BsonDocument("id", doc["id"]),
                options: new ReplaceOptions { IsUpsert = true },
                replacement: doc));

          //Cosmos DB SQL API
          //Code here---------
          //------------------


}

Cosmos .NET SDK 中的等效 function 是 UpsertItemAsync()。

我在這里假設您的分區鍵是/id。

listTask.Add(collection.UpsertItemAsync(
    item: doc,
    new PartitionKey(doc["id"]));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM