3

I want to do bulk insert in CosmosDB collections. But bulk insertion library is only available for .NET and Java.

I found another alternative as StoredProcedure but in that also I have to store using for loop.

sproc = {
            'id': 'storedProcedure' + str(uuid.uuid4()),
            'body': (
                'function (data) {' +
                '   var client = getContext().getCollection();' +
                '   client.createDocument(client.getSelfLink(), data, {}, function(err, docCreated, options) { ' +
                '   if(err) throw new Error(\'Error while creating document: \' + err.message);' +
                '   else {' +
                         '   getContext().getResponse().setBody(1);' +
                '        }' +
                '   });}')
        }

created_sproc = client.CreateStoredProcedure(coll_link, sproc)
proc_id = sproc['id']
proc_query = "select * from r where r.id = '{0}'".format(proc_id)
proc = list(client.QueryStoredProcedures(coll_link, proc_query))[0]
proc_link = proc['_self']
print(proc)

client.ExecuteStoredProcedure(proc_link, [data])

Any alternative for bulk executor in python?

Anurag
  • 117
  • 10
  • Normally i just loop though the documents to insert them into cosmos db – Jon Oct 04 '19 at 19:13
  • It is too time consuming. As n number of write operations has to be performed so we have to connect to the DB n number of times. In my case Stored Procedure works well. – Anurag Oct 05 '19 at 15:45

0 Answers0