简体   繁体   English

Cosmos DB - 使用Python插入多个记录

[英]Cosmos DB - Insert Multiple Records with Python

In Cosmos DB I can (thanks to the SO Community) insert a document like so: 在Cosmos DB中我可以(感谢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: 如果我一次可以插入多个文档会很棒,比如在SQL中你可以这样做:

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). 我知道你可以使用存储过程进行批量上传,但是如果我基本上可以将一堆文档连在一起,我认为这对我来说会更好(...或者至少让我学会如何编写存储的产品) 。

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. 但是:没有api调用一次插入多个文档。 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). 存储过程方法将为您提供不那么繁琐的调用(基本上是单个调用),并且是事务性的(全部或全部都不成功)。

Batch upload or bulk upload is not supported with Cosmo DB python library. Cosmo DB python库不支持批量上传或批量上传。 Hence, requires iteration over items to be stored in database. 因此,需要迭代项目以存储在数据库中。

Sample code (Run time - Python 3.6) 示例代码(运行时 - 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM