在 azure cosmos db 上创建集合太慢

Create collection on azure cosmos db too slow

我正在尝试使用 pydocumentdb 通过 Python 将 CSV 文档自动上传到 Azure Cosmos SQL API DB。我正在使用 pandas 执行一些数据预处理,然后将每一行转换为 JSON 格式。最后,我为 CSV 的每一行转换创建了一个文档。

原始 csv 大约 431 MB 和 5173620 个寄存器(行),这意味着我需要将每个寄存器(行)提取为一个新文档。我发现 createDocument() 函数非常慢(超过一天才能完成所有寄存器)。虽然单分区 Cosmos db 限制为 10000 RU,但计算所有文档查询花费 29025.92 RU,结果似乎是 innacurrate。

不知道有没有办法提高createDocument函数的性能。我也想知道如何在 createCollection 函数上定义 RU。欢迎任何提示。

# create document
coll_link = createColl(client, db_link, coll_id)

... preprocess a pandas Dataframe df...

# create a collection
for index, row in df.iterrows():
    doc = row.to_dict()
    client.CreateDocument(coll_link, doc)

1.I would like to know how to define the RUs on the createCollection function.

添加offerThroughput参数。

databaseLink = "dbs/db"
coll = {
        "id": "testCreate",
        "indexingPolicy": {
            "indexingMode": "lazy",
            "automatic": False
        },
        "partitionKey": {
            "paths": [
              "/AccountNumber"
            ],
            "kind": "Hash"
        }
       }
collection_options = { 'offerThroughput': 2000}
client.CreateCollection(databaseLink , coll, collection_options)

2.I wonder if there is a way to enhance the performance of createDocument function.

首先,我不认为 createDocument() 功能很慢,而应该是您的整个导入过程很慢,因为您的数据。根据你的描述,你 不要面对 Request rate is too large.Considering your economic issues , I don't recommend you endlessly increase RUs settings.You could consider use stored procedure to bulk import your data.Please refer to the sample in the official doc.

这样的错误

3.Other choice.

您可以使用 Azure Data Factory to import data from csv file into cosmos db directly.Please refer to steps in my previous answer:Convert JSON to CSV in Azure Cosmos DB or Azure Blob。只是转换输入和输出。


更新:

Which are the benefits of using the bulk import procedure?

Cosmos db存储过程是运行在cosmos db服务器端的一段js代码。所以如果你用它来批量导入数据,可以减轻你客户端的压力。

Can I add a Java coded store procedure on my Python code?

Cosmos DB 存储过程是在 cosmos db 服务器端运行的一段 js 代码。您可以在 cosmos db python sdk.

中调用存储过程

Concerning to Data Factory, it's possible to automatize the process to generalize to different collections and dbs?

基于Azure Data Factory Cosmos DB Dataset properties,您需要配置集合名称。所以你不能为不同的集合创建数据集。当然,您可以根据需要更改 sdk 中的集合名称,而不是创建多个数据集。

如有任何疑问,请随时告诉我。