首页 > 解决方案 > Azure Cosmos DB 列数限制

问题描述

Azure 表服务文档指出实体(行)最多必须有 255 个属性,我理解这意味着这些表最多可以有 255 个列,这似乎是非常严格的。

两个问题:首先,相同的限制是否适用于 Cosmos DB 表存储?尽管仍在使用“实体”的语言,但我似乎找不到任何说明这种或另一种方式的文档。其次——如果在 Cosmos DB 中应用相同的限制——是否有任何有用的方法可以绕过这个存储和查询限制,就像SQL Server 中的 JSON 那样

编辑:这是一些示例代码,它尝试将具有 260 个属性的实体写入 Cosmos DB 表存储并引发错误。帐户名称和密钥等已编辑

# Libraries
from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity
import csv
import os


# Connect
## Table Storage
"""
access_key = 'access_key'
table_service = TableService(account_name='account_name', account_key= access_key)
"""
## Cosmos DB Table Storage
connection_string = "connection_string"
table_service = TableService(connection_string=connection_string)

# Create Table
if not table_service.exists('testTable'):
    table_service.create_table('testTable')

length = 260
letters = [chr(i) for i in range(ord('a'), ord('z') + 1)]
keys = [a + b + c for a in letters for b in letters for c in letters][:length] 
values = ['0' * (8 - len(str(i))) + str(i) for i in range(length)]
entity = dict(zip(keys, values))
entity['PartitionKey'] = 'TestKey'
entity['RowKey'] = '1'
table_service.insert_entity('testTable', entity)

这会引发“ValueError:实体包含的属性多于允许的属性。”

标签: jsonazurestorageazure-cosmosdb

解决方案


首先,相同的限制是否适用于 Cosmos DB 表存储?

根据Azure 表存储限制,如您所说,表实体中的最大属性数是255. 但是,我刚刚在Azure Cosmos DB 限制中找到了以下语句。

Azure Cosmos DB 是一个全球规模的数据库,可以在其中扩展吞吐量和存储以处理应用程序所需的任何内容。如果您对 Azure Cosmos DB 提供的规模有任何疑问,请发送电子邮件至 askcosmosdb@microsoft.com。

根据我的测试(我厌倦了将 260 个属性添加到实体中),Azure Cosmos DB 表 API 接受属性超过 255。

在此处输入图像描述

如果您想获得官方回复,您可以发送电子邮件至上述地址。

沿着 SQL Server 中的 JSON 行,有什么有用的方法可以绕过这个存储和查询限制?

如果你想存储和查询json格式的数据,我建议你使用cosmos db SQL API。它用途广泛,灵活。你可以参考文档

此外,如果您的数据现在存储在 sql server 数据库中。您可以使用迁移工具将数据导入 cosmos db。或者您可以使用Azure 数据工厂进行更多自定义传输。

希望它可以帮助你。


推荐阅读