gpt4 book ai didi

c# - 将 JSON 批量导入 Azure Cosmos DB 时抛出异常 : 'Microsoft.Azure.Cosmos.CosmosException' , 错误请求

转载 作者:太空宇宙 更新时间:2023-11-03 22:29:16 30 4
gpt4 key购买 nike

我正在尝试将包含 JSON 列表的 JSON 文件从 .Net 4.6.1 控制台应用程序批量导入到 Azure Cosmos DB。

我能够成功地创建数据库和容器。但是,我在第 40 行收到以下错误,并且未创建项目。错误:

DocDBTrace Error: 0 : Operation will NOT be retried. Current attempt 0, Status Code: BadRequest Exception thrown: 'Microsoft.Azure.Cosmos.CosmosException' in Microsoft.Azure.Cosmos.Client.dll Exception thrown: 'System.AggregateException' in mscorlib.dll

示例代码:

class Program
{

private static string EndpointUrl = $"";
private const string AuthorizationKey = "";
private const string DatabaseName = "TestDB";
private const string ContainerName = "BulkImportTest";
public static async Task Main(string[] args)
{
string json = File.ReadAllText(@"BulkImport.json");

List<StudentInfo> lists = JsonConvert.DeserializeObject<List<StudentInfo>>(json);

CosmosClientOptions options = new CosmosClientOptions() { ConnectionMode = ConnectionMode.Gateway, AllowBulkExecution = true };
CosmosClient cosmosClient = new CosmosClient(EndpointUrl, AuthorizationKey, options);

try
{
Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(DatabaseName);
Console.WriteLine(database.Id);
Container container = await database.CreateContainerIfNotExistsAsync(ContainerName, "/SId");
Console.WriteLine(container.Id);
List<Task> tasks = new List<Task>();
foreach (StudentInfo item in lists)
{
tasks.Add(container.CreateItemAsync(item, new PartitionKey(item.SId)));
}
await Task.WhenAll(tasks); // Line 40
}

catch(Exception ex)
{
Console.WriteLine("Exception = " + ex.Message);
}


Console.ReadLine();
}
class StudentInfo
{
public string SId { get; set; }
public string SName { get; set; }
}}

BulkImport.json :

  [
{
"SId": "101",
"SName": "ABC",
},
{
"SId": "102",
"SName": "XYZ",
}
]

请帮我解决这个问题。

在进行建议的更新后,我仍然面临类似的问题:

DocDBTrace Error: 0 : Operation will NOT be retried. Current attempt 0, Status Code: BadRequest Exception thrown: 'Microsoft.Azure.Cosmos.CosmosException' in Microsoft.Azure.Cosmos.Client.dll Exception thrown: 'Microsoft.Azure.Cosmos.CosmosException' in mscorlib.dll

最佳答案

根据我的测试,当我们创建一个新文档时,我们必须提供“id”属性。更多详情请引用document .

例如

我的.json文件

 [{
"SId": "101",
"SName": "ABC"
}, {
"SId": "102",
"SName": "XYZ"
}
]

我的代码

        async static Task Main(string[] args)
{
string json = File.ReadAllText(@"E:\test.json");
List<StudentInfo> lists = JsonConvert.DeserializeObject<List<StudentInfo>>(json);
CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true, ConnectionMode = ConnectionMode.Gateway };
CosmosClient cosmosClient = new CosmosClient(EndpointUrl, AuthorizationKey, options);
Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(DatabaseName);
Console.WriteLine(database.Id);
Container container = await database.CreateContainerIfNotExistsAsync(ContainerName,"/SId");
Console.WriteLine(container.Id);
List<Task> tasks = new List<Task>();
foreach (StudentInfo item in lists)
{
item.Id = Guid.NewGuid().ToString();// add the line in your code
tasks.Add(container.CreateItemAsync(item, new PartitionKey(item.SId))
.ContinueWith((Task<ItemResponse<StudentInfo>> task) =>
{
Console.WriteLine("Status: " + task.Result.StatusCode + " Resource: " + task.Result.Resource.SId);
}));
}
await Task.WhenAll(tasks);
Console.ReadLine();
}
class StudentInfo
{
public string SId { get; set; }
public string SName { get; set; }
[JsonProperty(PropertyName = "id")]// add the code in your custom object
public string Id { get; set; }//add the code in your custom object

}
}

enter image description here

关于c# - 将 JSON 批量导入 Azure Cosmos DB 时抛出异常 : 'Microsoft.Azure.Cosmos.CosmosException' , 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58991914/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com