第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何向 Dynamodb 地圖添加新密鑰而不覆蓋現(xiàn)有密鑰?

如何向 Dynamodb 地圖添加新密鑰而不覆蓋現(xiàn)有密鑰?

蕭十郎 2023-09-05 19:45:07
根據(jù)我的代碼,首先,我向地圖添加一個(gè)新字典,它會(huì)成功更新到我的地圖。但是,當(dāng)我向地圖添加另一個(gè)字典時(shí),地圖中的現(xiàn)有鍵會(huì)被覆蓋。def update_item(table_name, id, new_company):    table = dynamodb.Table(table_name)    result = table.update_item(        Key={            'id': id        },        UpdateExpression="SET  company = :company",        ExpressionAttributeValues={            ':company': new_company        },        ReturnValues="ALL_NEW"    )    print(result)if __name__ == '__main__':    company_1 = {        "Facebook" : {            "CEO" : "Mark"        }    }    update_item('companies', 1, company_1)    company_2 = {        "Twitter": {            "CEO": "Jack"        }    }    update_item('companies', 1, company_2)我的輸出:我的 Dynamodb 表中可用的項(xiàng)目{    'company': {        'Twitter': {            'CEO': 'Jack'        }    },    'id': Decimal('1'),    'industry': 'industry'}預(yù)期輸出:{    'company': {        'Facebook': {            'CEO': 'Mark'        }        'Twitter': {            'CEO': 'Jack'        }    },    'id': Decimal('1'),    'industry': 'industry'}當(dāng)我向地圖添加新字典時(shí),如何避免覆蓋現(xiàn)有字典?我是 DynamoDB 的新手,任何建議都會(huì)有幫助。
查看完整描述

2 回答

?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊

問題出在你的UpdateExpression.?您每次都將 map 的值設(shè)置company為新值:

SET??company?=?:company

但聽起來您想向地圖附加一個(gè)新值company。

查看完整回答
反對(duì) 回復(fù) 2023-09-05
?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊

我是 Lucid-Dynamodb 的作者,它是 AWS DynamoDB 的極簡包裝器。使用我的庫可以輕松解決這個(gè)問題。

參考: https: //github.com/dineshsonachalam/Lucid-Dynamodb

from LucidDynamodb.Operations import DynamoDb

import os

import logging

logging.basicConfig(level=logging.INFO)


AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")

AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")


table_schema = {

    "TableName": "company",

    "KeySchema": [

        {

            "AttributeName": "id",

            "KeyType": "HASH"

        }

    ],

    "AttributeDefinitions": [

        {

            "AttributeName": "id",

            "AttributeType": "N"

        }

     ],

    "GlobalSecondaryIndexes": [],

    "ProvisionedThroughput": {

        "ReadCapacityUnits": 1,

        "WriteCapacityUnits": 1

    }

}



if __name__ == "__main__":

    

    # 1. create a new table

    db = DynamoDb(region_name="us-east-1", 

                aws_access_key_id=AWS_ACCESS_KEY_ID, 

                aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

                

    table_creation_status = db.create_table(

                                    TableName=table_schema.get("TableName"),

                                    KeySchema=table_schema.get("KeySchema"),

                                    AttributeDefinitions=table_schema.get("AttributeDefinitions"),

                                    GlobalSecondaryIndexes=table_schema.get("GlobalSecondaryIndexes"),

                                    ProvisionedThroughput=table_schema.get("ProvisionedThroughput")

    )

    if(table_creation_status == True):

        logging.info("{} table created successfully".format(table_schema.get("TableName")))

    else:

        logging.error("{} table creation failed".format(table_schema.get("TableName")))

    

    # 2. Create a new item

    item_creation_status = db.create_item(

        TableName=table_schema.get("TableName"), 

        Item=  {

                'company': {

                    'Facebook': {

                        'CEO': 'Mark'

                    }

                },

                'id': 1,

                'industry': 'internet'

        }

    )

    if(item_creation_status == True):

        logging.info("Item created successfully")

    else:

        logging.warning("Item creation failed")

    

    # 3. Add a new attribute in a item

    item_update_status = db.update_item(

        TableName=table_schema.get("TableName"), 

        Key={

            'id': 1

        },

        AttributesToUpdate={

            'company.Twitter': {

                'CEO': 'Jack'

            }

        }

    )

    if(item_update_status == True):

        logging.info("Update is successful")

    else:

        logging.warning("Update failed")


    item = db.read_item(

        TableName=table_schema.get("TableName"), 

        Key={

            'id': 1

        })

    if(item != None):

        logging.info("Item: {}".format(item))

    else:

        logging.warning("Item doesn't exist")

輸出:

dineshsonachalam@macbook Dynamodb-experiment % python test.py

INFO:root:company table created successfully

INFO:root:Item created successfully

INFO:root:Update is successful

INFO:root:Item: 

{

    'company': {

        'Facebook': {

            'CEO': 'Mark'

        },

        'Twitter': {

            'CEO': 'Jack'

        }

    },

    'id': Decimal('1'),

    'industry': 'internet'

}


查看完整回答
反對(duì) 回復(fù) 2023-09-05
?
紅顏莎娜

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊

您只需在#name 處添加一個(gè)地圖條目即可。


def update_item(table_name, id, new_company):

    table = dynamodb.Table(table_name)

    name = list(new_company)[0]

    result = table.update_item(

        Key={

            'id': id

        },

        UpdateExpression="SET  company.#name = :company",

        ExpressionAttributeNames={"#name": name},

        ExpressionAttributeValues={

            ':company': new_company[name]

        },

        ReturnValues="ALL_NEW"

    )

    print(result)


輸出:我的 Dynamodb 中可用的項(xiàng)目


{

    'company': {

        'Facebook': {

            'CEO': 'Mark'

        },

        'Twitter': {

            'CEO': 'Jack'

        }

    },

    'id': Decimal('1'),

    'industry': 'internet'

}

https://img1.sycdn.imooc.com//64f715180001b91203190212.jpghttps://img1.sycdn.imooc.com//64f7151b0001b91203190212.jpg

查看完整回答
反對(duì) 回復(fù) 2023-09-05
  • 2 回答
  • 0 關(guān)注
  • 253 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)