Azure TableClient ConnectionString update_request 字典上的类型错误

问题描述 投票:0回答:1

我正在创建表客户端,如下所示:

with TableClient.from_connection_string(self.connection_string, table_name=self.table_name) as table_update_entity:
    table_update_entity.update_entity(_entity, mode = UpdateMode.REPLACE)

_entity
变量是一个字典构建,如下所示:

_entity = {
    str: str,
    .
    .
    .
    str: str,
    str: dict # This dict build is shown below
}
dict = {
    str: str,
    str; str,
    str: str,
    str: list,
    str: list,
    str: list
}

我收到以下错误:

向服务发送数据时不支持的类型:

过去几个小时我一直在尝试修复它,尝试过谷歌,尝试过chatGPT,但现在没有运气,没有找到帮助的解决方案。


目前我设法“做到这一点”的唯一方法是删除请求,然后从 0 重新创建它

python azure exception entity azure-table-storage
1个回答
0
投票

我尝试使用以下示例代码在 Azure 表存储中使用嵌套字典创建实体。

代码:

import json
from azure.data.tables import TableClient, UpdateMode

class AzureTableUpdater:
    def __init__(self, connection_string, table_name):
        self.connection_string = connection_string
        self.table_name = table_name

    def update_entity_with_nested_dict(self, entity):
        partition_key = entity['PartitionKey']
        row_key = entity['RowKey']

        nested_dict = entity.pop('nested_data')
        nested_dict_json = json.dumps(nested_dict)

        entity['nested_data'] = nested_dict_json

        with TableClient.from_connection_string(self.connection_string, table_name=self.table_name) as table_client:
            table_client.update_entity(entity)

connection_string = "<Connec_string>"
table_name = "<table_name>"

entity = {
    'PartitionKey': '<Partition_key>',
    'RowKey': '<Row_key>',
    'property1': 'value1',
    'property2': 'value2',
    'nested_data': {
        'nested_key1': 'nested_value1',
        'nested_key2': 'nested_value2',
    }
}

table_updater = AzureTableUpdater(connection_string, table_name)
table_updater.update_entity_with_nested_dict(entity)

输出:

运行成功如下,

enter image description here

在 Azure 门户中的 Azure 表存储中创建的实体如下所示,

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.