Hi, I've been following this post to hand the bulkUpload over 10,000 docs to cosmos DB, I followed the steps and exec bulk upload stored procedure from .net core sdk.
However, I always get the 408 timeout error when the first execution ends. Ideally, it should return the count it has processed and so I can call the procedure in code again.
Which part is wrong? Thanks in advance.
store procedure:
function bulkUpload(docs) {
var container = getContext().getCollection();
var containerLink = container.getSelfLink();
var count = 0;
if (!docs) throw new Error("The array is undefined or null.");
var docsLength = docs.length;
if (docsLength == 0) {
getContext()
.getResponse()
.setBody(0);
return;
}
tryCreate(docs[count], callback);
function tryCreate(doc, callback) {
var isAccepted = container.createDocument(containerLink, doc, callback);
if (!isAccepted)
getContext()
.getResponse()
.setBody(count);
}
function callback(err, doc, options) {
if (err) throw err;
count++;
if (count >= docsLength) {
getContext()
.getResponse()
.setBody(count);
} else {
tryCreate(docs[count], callback);
}
}
}
.net code:
public static async Task Main(string[] args)
{
using (CosmosClient client = new CosmosClient(_endpointUri, _primaryKey))
{
Database database = client.GetDatabase(_databaseId);
Container container = database.GetContainer(_containerId);
List<Food> foods = new Bogus.Faker<Food>()
.RuleFor(p => p.Id, f => (-1 - f.IndexGlobal).ToString())
.RuleFor(p => p.Description, f => f.Commerce.ProductName())
.RuleFor(p => p.ManufacturerName, f => f.Company.CompanyName())
.RuleFor(p => p.FoodGroup, f => "Energy Bars")
.Generate(10000);
int pointer = 0;
while (pointer < foods.Count)
{
StoredProcedureExecuteResponse<int> result = await container.Scripts.ExecuteStoredProcedureAsync<int>("bulkUpload", new PartitionKey("Energy Bars"), new dynamic[] {foods.Skip(pointer)});
pointer += result.Resource;
await Console.Out.WriteLineAsync($"{pointer} Total Items\t{result.Resource} Items Uploaded in this Iteration");
}
}
}