python - 如何使用 pydocumentdb 在 Cosmos DB 中创建分区集合?

标签 python azure azure-cosmosdb

pydocumentdb.document_client.DocumentClient 对象有一个 CreateCollection() 方法,定义为 here .

使用此方法创建集合时,需要指定数据库链接(已经知道)、集合(如果没有创建,我不知道如何引用它)和选项。

创建集合时我想控制的参数是:

  • 集合名称
  • 集合类型(固定大小与分区)
  • 分区键
  • RU 值
  • 索引政策(或者至少能够在某处创建默认模板并自动将其复制到新创建的模板)

其中一些参数的枚举似乎已定义 here ,但我在 http_constants.py 中没有看到任何可能有用的 HTTP header ,而且我没有看到 RU 在哪里发挥作用,也没有看到内聚的“集合”对象将作为参数传递。

最佳答案

您可以引用here中的源示例代码以及 here 中的 rest api .

import pydocumentdb;
import pydocumentdb.errors as errors
import pydocumentdb.document_client as document_client

config = {
    'ENDPOINT': 'https://***.documents.azure.com:443/',
    'MASTERKEY': '***'
};

# Initialize the Python DocumentDB client
client = document_client.DocumentClient(config['ENDPOINT'], {'masterKey': config['MASTERKEY']})


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

希望对您有帮助。

关于python - 如何使用 pydocumentdb 在 Cosmos DB 中创建分区集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49324754/

相关文章:

c# - 如何从 C# 中的 Azure HTTP 触发器函数删除 CosmosDB 中的项目

azure - 从 azure 应用程序中处理多个 Web 角色实例的缓存

azure - 如何将序列化数据从 SQL 数据库传输到 cosmos Db

azure - 宇宙数据库 : Optimizing RUs with a time triggered FunctionApp?

python - 从类中返回 subprocess.Popen 的输出

python - Jinja2:使用 pandas 数据框或字符串变量

python - 如何在python中的另外两条线之间插入一条线

java - Clojure - 找不到匹配的方法

node.js - 连接到 Cosmos DB 模拟器

python - 将嵌入式 python asyncio 集成到 boost::asio 事件循环中