首页 > 解决方案 > 是否可以使用 AppSync VTL 使用 BatchPutItem (BatchWriteItem) 在 DynamoDB 上插入超过 25 个项目?

问题描述

仅针对某些情况:当前使用 AppSync + React + Apollo,我正在尝试通过 AppSync 的 Apache VTL 发送 38 - 40 个要更新的项目。

我知道 DynamoDB 将每个请求插入的项目数限制为 25,但我认为 AppSync 没有这些限制。我猜我错了,因为每当我发送超过 25 个项目(26+)时,我的请求都会失败。

这是我的 VTL 脚本:

#set($isTenantValid = false)
#foreach($tenant in $context.identity.claims["https://app.schon.io/tenants"])
    #if($tenant == $ctx.args.tenantId)
        #set($isTenantValid = true)
    #end
#end

## Needs to verify if the employee has permission to insert students.

#if(!$isTenantValid) 
    $utils.unauthorized()
#end


#set($itemsToPut=[])
#set($pk="tenant:${ctx.args.tenantId}")
#set($userSK="tenant:${ctx.args.tenantId}:school-year:${ctx.args.schoolYear}")

#foreach($student in $ctx.args.students)
    #set($studentId = "${util.autoId()}")
    #set($sk="school-year:${ctx.args.schoolYear}:student:${studentId}")
    #set($userPK="student:${studentId}")

    #set($item = {
    "pk": $pk,
    "sk": $sk,
    "id": $studentId,
    "userPK": $userPK,
    "userSK": $userSK,
    "name": {
        "firstName" : $student.name.firstName,
        "lastName" : $student.name.lastName,
        "fullName": "${student.name.firstName} ${student.name.lastName}"
    },
    "schoolYear": $ctx.args.schoolYear,
    "createdAt": ${util.time.nowEpochSeconds()},
    "updatedAt": ${util.time.nowEpochSeconds()},
    "gender": $student.gender,
    "retired": $student.retired
    })
    #if("${student.diseases}" != "")
        $util.qr($item.add("diseases", $student.diseases)) 
    #end
    #if("${student.email}" != "")
        $util.qr($item.add("email", $student.email)) 
    #end
    #if("${student.birthdate}" != "")
        $util.qr($item.add("birthdate", $student.birthdate)) 
    #end

    $util.qr($itemsToPut.add($util.dynamodb.toMapValues($item)))
#end


{
    "version" : "2018-05-29",
    "operation" : "BatchPutItem",
    "tables" : {
        "SchonDB": $utils.toJson($itemsToPut)
    }
}

在这种情况下,我是否需要将其卸载到 Lambda 并为每 25 个项目执行发送和重试逻辑?

标签: reactjsamazon-dynamodbapolloaws-appsyncvtl

解决方案


您可以尝试联系 DDB 团队为您增加服务限制吗?


推荐阅读