0

Hi i have a sample stored procedure created like this

Bulk insert

function bulkImport(items) {
    var container = getContext().getCollection();
    var containerLink = container.getSelfLink();

var items=[{ 
"UserAccountID":"1236", 
"FirstName": "Sanjeev", 
},{ 
"UserAccountID":"1235", 
"FirstName": "Sanjeev",  
}];

    // The count of imported items, also used as current item index.
    var count = 0;

    // Validate input.
    if (!items) throw new Error("The array is undefined or null.");

    var itemsLength = items.length;
    if (itemsLength == 0) {
        getContext().getResponse().setBody(0);
    }

    // Call the create API to create an item.
    tryCreate(items[count], callback);

    function tryCreate(item, callback) {
        var isAccepted = container.createDocument(containerLink, item, callback);

        if (!isAccepted) getContext().getResponse().setBody(count);
    }


    function callback(err, item, options) {
        if (err) throw err;

        // One more item has been inserted, increment the count.
        count++;

        if (count >= itemsLength) {
            // If we created all items, we are done. Just set the response.
            getContext().getResponse().setBody(count);
        } else {
            // Create next document.
            tryCreate(items[count], callback);
        }
    }
}

How to execute this sp in the cosmos emulator. How to set the partition key uniquely for each of the item.

Since the partition key should be unique we cannot provide a single partition key while executing.

Community
  • 1
  • 1
Sanjeev S
  • 1,113
  • 2
  • 13
  • 33

1 Answers1

0

Firstly,you could create non-partitioned collection in the cosmos db if you do not need it. The portal has some limitations but the sdk allows it. You could refer to my previous case:Is it still a good idea to create comos db collection without partition key?.

Secondly, the bulk import of stored procedure is not suitable for the partitioned collection because you need to provide the pk for every execution of a stored procedure. Please refer to this link: Azure Cosmos DB asking for partition key for stored procedure

Finally,there are also many other solutions to import data into cosmos db,such as Azure Cosmos DB Data migration tool which is worth trying for you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jay Gong
  • 23,163
  • 2
  • 27
  • 32