C# 引发异常:';Microsoft.Azure.Cosmos.CosmosException';,批量将JSON导入Azure Cosmos DB时请求错误
我正在尝试从.Net 4.6.1控制台应用程序将包含C# 引发异常:';Microsoft.Azure.Cosmos.CosmosException';,批量将JSON导入Azure Cosmos DB时请求错误,c#,.net,azure-cosmosdb,C#,.net,Azure Cosmosdb,我正在尝试从.Net 4.6.1控制台应用程序将包含JSON列表的JSON文件批量导入Azure Cosmos DB 我成功地创建了数据库和容器。 但是,我在第40行遇到以下错误,并且没有创建项目。 错误: DocDBTrace错误:0:将不会重试该操作。当前尝试0,状态代码:引发BadRequest异常:Microsoft.Azure.Cosmos.Client.dll中的Microsoft.Azure.Cosmos.CosmosException异常引发:mscorlib.dll中的Syst
JSON
列表的JSON
文件批量导入Azure Cosmos DB
我成功地创建了数据库和容器。
但是,我在第40行遇到以下错误,并且没有创建项目。
错误:
DocDBTrace错误:0:将不会重试该操作。当前尝试0,状态代码:引发BadRequest异常:Microsoft.Azure.Cosmos.Client.dll中的Microsoft.Azure.Cosmos.CosmosException异常引发:mscorlib.dll中的System.AggregateException异常
示例代码:
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; }
}}
请帮我解决这个问题
在进行建议的更新后,我仍然面临类似的问题:
DocDBTrace错误:0:将不会重试该操作。当前尝试0,状态代码:BadRequest
引发异常:Microsoft.Azure.Cosmos.Client.dll中的“Microsoft.Azure.Cosmos.CosmosException”
在mscorlib.dll中引发异常:“Microsoft.Azure.Cosmos.CosmosException”
您的文档不包含分区键 使容器中的分区键路径与某些属性(例如
/SID
)匹配,或者确保JSON数据包含具有funcId
属性的文档
[
{
"SID": "101",
"SName": "ABC",
"funcId" : "Something"
}
{
"SID": "102",
"SName": "XYZ",,
"funcId" : "Something"
}
]
然后将您的文档加载到:
foreach (StudentInfo item in lists)
{
tasks.Add(container.CreateItemAsync(item, new PartitionKey(item.funcId)));
}
使用模型:
class StudentInfo
{
public string SId { get; set; }
public string funcId { get; set; }
public string SName { get; set; }
}
或者,放下容器并按如下方式创建:
Container container = await database.CreateContainerIfNotExistsAsync(ContainerName, "/SID");
您的文档不包含分区键 使容器中的分区键路径与某些属性(例如
/SID
)匹配,或者确保JSON数据包含具有funcId
属性的文档
[
{
"SID": "101",
"SName": "ABC",
"funcId" : "Something"
}
{
"SID": "102",
"SName": "XYZ",,
"funcId" : "Something"
}
]
然后将您的文档加载到:
foreach (StudentInfo item in lists)
{
tasks.Add(container.CreateItemAsync(item, new PartitionKey(item.funcId)));
}
使用模型:
class StudentInfo
{
public string SId { get; set; }
public string funcId { get; set; }
public string SName { get; set; }
}
或者,放下容器并按如下方式创建:
Container container = await database.CreateContainerIfNotExistsAsync(ContainerName, "/SID");
根据我的测试,当我们创建一个新文档时,我们必须提供“id”属性。有关更多详细信息,请参阅 比如说 我的
.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
}
}
异步静态任务主(字符串[]args)
{
字符串json=File.ReadAllText(@“E:\test.json”);
List lists=JsonConvert.DeserializeObject(json);
CosmosClientOptions=new CosmosClientOptions(){AllowBulkExecution=true,ConnectionMode=ConnectionMode.Gateway};
CosmosClient CosmosClient=新的CosmosClient(端点URL、授权键、选项);
Database Database=await cosmosClient.CreateDatabaseIfNoteExistsAsync(DatabaseName);
Console.WriteLine(database.Id);
Container Container=wait database.CreateContainerIfNotExistsAsync(ContainerName,“/SId”);
Console.WriteLine(container.Id);
列表任务=新列表();
foreach(列表中的StudentInfo项)
{
item.Id=Guid.NewGuid().ToString();//在代码中添加行
添加(container.CreateItemAsync(item,new PartitionKey(item.SId))
.ContinueWith((任务)=>
{
Console.WriteLine(“状态:+task.Result.StatusCode+”资源:+task.Result.Resource.SId);
}));
}
等待任务。何时(任务);
Console.ReadLine();
}
班级学生信息
{
公共字符串SId{get;set;}
公共字符串SName{get;set;}
[JsonProperty(PropertyName=“id”)]//在自定义对象中添加代码
公共字符串Id{get;set;}//在自定义对象中添加代码
}
}
根据我的测试,创建新文档时,必须提供“id”属性。有关更多详细信息,请参阅 比如说 我的
.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
}
}
异步静态任务主(字符串[]args)
{
字符串json=File.ReadAllText(@“E:\test.json”);
List lists=JsonConvert.DeserializeObject(json);
CosmosClientOptions=new CosmosClientOptions(){AllowBulkExecution=true,ConnectionMode=ConnectionMode.Gateway};
CosmosClient CosmosClient=新的CosmosClient(端点URL、授权键、选项);
Database Database=await cosmosClient.CreateDatabaseIfNoteExistsAsync(DatabaseName);
Console.WriteLine(database.Id);
Container Container=wait database.CreateContainerIfNotExistsAsync(ContainerName,“/SId”);
Console.WriteLine(container.Id);
列表任务=新列表();
foreach(列表中的StudentInfo项)
{
item.Id=Guid.NewGuid().ToString();//在代码中添加行
添加(container.CreateItemAsync(item,new PartitionKey(item.SId))
.ContinueWith((任务)=>
{
Console.WriteLine(“状态:+task.Result.StatusCode+”资源:+task.Result.Resource.SId);
}));
}
等待任务。何时(任务);
Console.ReadLine();
}
班级学生信息
{
公共字符串SId{get;set;}
公共字符串SName{get;set;}
[JsonProperty(PropertyName=“id”)]//在自定义对象中添加代码
公共字符串Id{get;set;}//在自定义对象中添加代码
}
}
您是否使用cosmos db local emulator?不,我正在使用它。根据您的json文件和代码,您使用
SID
作为分区键。因此,请更新您的代码数据库
到数据库。CreateContainerIfNotExistsAsync(ContainerName,“/SID”)代码>。此外,json的格式是错误的,它应该像[{“SID”:“101”,“SName”:“ABC”},{“SID”: