[英]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.