python - 如何使用 Python 在 1 次写入 DynamoDB 中插入对象列表?

标签 python amazon-web-services amazon-dynamodb boto3

您好,我正在尝试生成一个表,其中包含 1 天内的所有出租车行程。 1 次出租车行程的属性之一包括该行程中使用的所有折扣的列表。

可以使用任意数量的折扣。我不确定该怎么做是如何将该折扣列表包含到一项商品属性中?那么基本上是将 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)

“行程”键将是 1 个长字符串,其中包含出租车行程的起点、当天的最后一站、出租车号码、停靠站数量和行程日期。

trip_key: 12thAve.MapleDrive.0124.02.02202020

在下面作为属性,我想要一份该出租车旅行中使用的所有折扣的列表/ 像这样的东西:

trip_key: 12thAve.MapleDrive.0124.02.02202020
discount_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'}
}

我不知道一次旅行可以使用多少折扣。可能在 5-10 之间。但我想将所有折扣包含在一份插入中。我想查询出租车旅行中使用的具体折扣以及从此表中节省的总额。

我不知道是否应该首先将列表转换为 JSON 对象?或者是否有一种方法可以迭代列表并按照我想要的方式插入它。

import boto3

class 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
                    }
                } )

最佳答案

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'
            }
        )

Guide docs
API docs

关于python - 如何使用 Python 在 1 次写入 DynamoDB 中插入对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60267884/

相关文章:

amazon-web-services - 使用 AWS CDK 创建 VPC 时如何排除路由表及其路由的默认创建

amazon-web-services - Amazon DynamoDB 获取过去 24 小时的所有项目

amazon-web-services - 使用多个条件表达式的 AWS DynamoDB 更新项目

php - 简单的Python房间聊天服务器

python - 为MySQL数据库创建Api并使用Python连接到移动App

amazon-web-services - 复制现有 Cognito 用户池的最佳方法是什么

python - 从 DynamoDB 获取项目时出现 "The provided key element does not match the schema"错误

python - 如何嵌入 Evince?

python - 数据库必须是数据库 pymongo 的实例

amazon-web-services - Terraform Remote-Exec 配置程序超时