[cosmos] Using the bulk API
See original GitHub issue- cosmos:
- 3.9.0:
- macosx:
- nodejs
- 12.16.3:
- browser
- name/version:
- typescript
- version:
- Is the bug related to documentation in
- README.md
- source code documentation
- SDK API docs on https://docs.microsoft.com
Describe the bug I am updating some code to make use of the bulk insert API made available in the 3.9.0 update.
I am trying to update the following code:
for (i = 0; i < rows.length; i++) {
container.items.create(rows[i]);
}
So far I have tried:
container.items.create(rows);
this returned a 400 BadRequest One of the specified inputs is invalid
I then had a look into the source and have been trying this (also trying to specify partitionKey
).
operations = [];
for (i = 0; i < rows.length; i++) {
operations.push({
operationType: "Create",
resourceBody: rows[i]
});
}
container.items.bulk(operations);
This doesn’t give me an error but also nothing appears in the cosmos container.
An example of rows[i]
is a flat json object with string values:
{
"Town": "blah",
"City": "blah",
...
}
To Reproduce Steps to reproduce the behavior:
- A working cosmos database with a container
const cosmosClient = new CosmosClient({ endpoint:COSMOS_ENDPOINT, key:COSMOS_KEY });
const database = cosmosClient.database(COSMOS_DB_NAME);
const { container } = await database.containers.createIfNotExists({ id:CONTAINER_NAME });
- Verify the for loop
container.items.create(rows[i]);
(from above) works - Replace with either of the two mentioned attempts.
Expected behavior I expect the bulk API to allow me to bulk create records to avoid having to iterate over a collection, creating one at a time. Additionally I expect the second of my attempts to give me some error as it is not working.
Screenshots If applicable, add screenshots to help explain your problem.
Additional context I originally asked the question in this thread https://github.com/Azure/azure-sdk-for-js/issues/7479 and @zfoster recommended a dedicated bug report. Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Comments:20 (4 by maintainers)
Top GitHub Comments
This bulk insert is blazing fast.
`
`
@tbshrr Thank you! I had that line commented out from previous attempts. Thanks for the reminder.
const { BulkOperationType } = require('@azure/cosmos') operations.push({ operationType: BulkOperationType.Upsert, id: superItem.id, resourceBody: superItem });
Code is working as expected now.