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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Python 在 1 次寫入 DynamoDB 中插入對象列表?

如何使用 Python 在 1 次寫入 DynamoDB 中插入對象列表?

繁星coding 2022-08-02 18:36:26
我正在嘗試生成一個表,該表將包含1天的所有出租車行程。1 次出租車行程的屬性之一將包括該行程中使用的所有折扣的列表??梢允褂萌我鈹?shù)量的折扣。我不確定該怎么做的是,如何將折扣列表包含在一個商品屬性中?那么,基本上將 1 個項目與屬性列表一起寫入 DynamoDB?這是我的表格:def create_table(self):        table = dynamodb.create_table(            TableName='TaxiTable',            KeySchema=[                {                    'AttributeName': 'trip',                    'KeyType': 'HASH'                },            ],            AttributeDefinitions=[                {                    'AttributeName': 'trip',                    'AttributeType': 'S'                },            ],            ProvisionedThroughput={                'ReadCapacityUnits': 10,                'WriteCapacityUnits': 10            }        )        print("Table status:", table.table_status)“trip”鍵將是1個長字符串,其中包含該出租車的起點(diǎn),當(dāng)天的最后一站,出租車號碼,??空緮?shù)和行程日期。trip_key: 12thAve.MapleDrive.0124.02.02202020作為屬性,我想要一個列表,列出那次出租車旅行中使用的所有折扣/類似的東西:trip_key: 12thAve.MapleDrive.0124.02.02202020discount_map: {  discount1: { name: 'Senior', total_saved: '10'},  discount2: { name: 'Student', total_saved: '30'},  discount3: { name: 'Employee', total_saved: '20'},  discount4: { name: 'Hotel', total_saved: '30'}}我不知道一次旅行可以使用多少折扣??赡茉?-10之間。但我想在一個插入中包括所有使用的折扣。我想查詢出租車旅行中使用的特定折扣以及從此表中節(jié)省的總折扣。我不知道是否應(yīng)該首先將列表轉(zhuǎn)換為JSON對象?或者,如果有一種方法可以循環(huán)訪問列表并按照我想要的方式插入它。import boto3class taxi_discount:    name = None    total_saved = None# rest of logic for this class...class insert_to_dynamoDb:    dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")    taxi_trip_key= None                taxi_discounts_list = []    def __init__(taxi_trip, discount_list):            self.taxi_trip_key= taxi_trip            self.discount_list = discount_list    def write_to_table(self):            table = dynamodb.Table('TaxiTable')            response = table.put_item(                Item={                    'trip': taxi_trip_key,                    'discount_map': taxi_discounts_list                    }                } )
查看完整描述

1 回答

?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗 獲得超3個贊

DynamoDB 支持批量寫入。


來自文檔的示例:


with table.batch_writer() as batch:

    for i in range(50):

        batch.put_item(

            Item={

                'account_type': 'anonymous',

                'username': 'user' + str(i),

                'first_name': 'unknown',

                'last_name': 'unknown'

            }

        )


查看完整回答
反對 回復(fù) 2022-08-02
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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