问题
In Cosmos DB I can (thanks to the SO Community) insert a document like so:
data = {'attribute1':1, 'attribute2': 2}
client.CreateDocument('dbs/databaseName/colls/collectionName/', data)
It would be great if I could insert multiple documents at a time, like how in SQL you can do:
insert into table values (1, 2), (3,4), (5,6)
I understand that you can do bulk uploads with stored procedures, but if I could basically concat a bunch of documents together I think that would work better for me (...or at least save me learning how to write stored produces at this moment).
回答1:
You're correct in that you can insert multiple documents via a stored procedure.
However: There are no api calls to insert multiple documents at once. You must execute one call per document insert (whether done from your app, or from a stored procedure).
The stored procedure approach will give you a less-chatty set of calls (essentially a single call), and be transactional (all or none succeed).
回答2:
Batch upload or bulk upload is not supported with Cosmo DB python library. Hence, requires iteration over items to be stored in database.
Sample code (Run time - Python 3.6)
import json
import random
import azure.cosmos.cosmos_client as cosmos_client
import azure.cosmos.documents as doc
from faker import Faker
from pprint import pprint
from easydict import EasyDict as edict
config = {
'ENDPOINT': 'https://localhost:8081',
'PRIMARYKEY': 'CREATEME',
'DATABASE': 'funcTest',
'CONTAINER': 'funcTest'
}
# Initialize the Cosmos client
connection_policy = doc.ConnectionPolicy()
# Disable in production
connection_policy.DisableSSLVerification = "true"
client = cosmos_client.CosmosClient(url_connection=config['ENDPOINT'], auth={
'masterKey': config['PRIMARYKEY']},
connection_policy=connection_policy)
# Create a database
db = client.CreateDatabase({'id': config['DATABASE']})
# Create container options
options = {
'offerThroughput': 400
}
container_definition = {
'id': config['CONTAINER']
}
# Create a container
container = client.CreateContainer(db['_self'], container_definition, options)
fake = Faker()
# Iterating over fake person data and storing in DB
for i in range(50):
json_data = edict({'first_name': fake.first_name(),
'last_name': fake.last_name(),
'age': random.randint(30, 50),
'address': {'city': fake.city(),
'state': fake.state()}})
client.CreateItem(container['_self'], json_data)
来源:https://stackoverflow.com/questions/46962800/cosmos-db-insert-multiple-records-with-python