gpt4 book ai didi

amazon-web-services - AWS Appsync 批量解析器

转载 作者:行者123 更新时间:2023-12-02 03:30:08 25 4
gpt4 key购买 nike

现在为此苦苦挣扎了一段时间,并且applogies我将问题的查询名称更改为getDeviceReadings,我一直在使用getAllUserDevices(抱歉造成任何混淆)

type Device {
id: String
device: String!
}

type Reading {
device: String
time: Int
}

type PaginatedDevices {
devices: [Device]
readings: [Reading]
nextToken: String
}

type Query {
getDevicesReadings(nextToken: String, count: Int): PaginatedDevices
}

然后我在查询 getDevicesReadings 上有一个解析器,它工作正常并返回用户迄今为止拥有的所有设备

{
"version": "2017-02-28",
"operation": "Query",
"query" : {
"expression": "id = :id",
"expressionValues" : {
":id" : { "S" : "${context.identity.username}" }
}
}
#if( ${context.arguments.count} )
,"limit": ${context.arguments.count}
#end
#if( ${context.arguments.nextToken} )
,"nextToken": "${context.arguments.nextToken}"
#end
}

现在我想返回设备基于源结果的所有读数,因此我在 getDevicesReadings/readings 上有一个解析器

#set($ids = [])
#foreach($id in ${ctx.source.devices})
#set($map = {})
$util.qr($map.put("device", $util.dynamodb.toString($id.device)))
$util.qr($ids.add($map))
#end

{
"version" : "2018-05-29",
"operation" : "BatchGetItem",
"tables" : {
"readings": {
"keys": $util.toJson($ids),
"consistentRead": true
}
}
}

像这样的响应映射..

$utils.toJson($context.result.data.readings)

我运行查询

query getShit{
getDevicesReadings{
devices{
device
}
readings{
device
time
}
}
}

这将返回以下结果

{
"data": {
"getAllUserDevices": {
"devices": [
{
"device": "123"
},
{
"device": "a935eeb8-a0d0-11e8-a020-7c67a28eda41"
}
],
"readings": [
null,
null
]
}
}
}

enter image description here

正如您在图像上看到的,主分区键是读数表上的设备,我查看日志,得到以下内容

enter image description here

抱歉,如果您无法阅读日志,它基本上表示存在未处理的 key

以及以下错误消息

"message": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 0H21LJE234CH1GO7A705VNQTJVVV4KQNSO5AEMVJF66Q9ASUAAJG)",

我猜测我的映射不太正确,并且我将读数作为我的键传递?

非常感谢任何帮助

最佳答案

不,当您有主排序键时,您绝对可以使用批处理解析器。您的示例中的错误是您没有向解析器提供主排序键。

此代码需要提供“时间”和“设备”,因为您需要两者来完全指定主键。

#set($ids = [])
#foreach($id in ${ctx.source.devices})
#set($map = {})
$util.qr($map.put("device", $util.dynamodb.toString($id.device)))
$util.qr($ids.add($map))
#end

你应该有类似的东西:

#set($ids = [])
#foreach($id in ${ctx.source.devices})
#set($map = {})
# The tables primary key is made up of "device" AND "time"
$util.qr($map.put("device", $util.dynamodb.toString($id.device)))
$util.qr($map.put("time", $util.dynamodb.toString($id.time)))
$util.qr($ids.add($map))
#end

如果您想要获取共享相同“设备”值但具有不同“时间”值的许多记录,则需要使用 DynamoDB 查询操作,而不是批量获取。

关于amazon-web-services - AWS Appsync 批量解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52069820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com