我正在嘗試生成一個表,該表將包含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'
}
)
添加回答
舉報
0/150
提交
取消