Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 在不返回值的对象数组上使用VTL foreach循环_Amazon Web Services_Api_Templates_Mapping_Vtl - Fatal编程技术网

Amazon web services 在不返回值的对象数组上使用VTL foreach循环

Amazon web services 在不返回值的对象数组上使用VTL foreach循环,amazon-web-services,api,templates,mapping,vtl,Amazon Web Services,Api,Templates,Mapping,Vtl,我使用AWSAPI网关作为restful API端点,然后将记录传递到Kinesis流,然后传递到Lambda。但是,似乎有一些数据未到达Lambda函数 我一直在到处寻找一个例子或接近我正在寻找的东西,但没有运气 设备消息的外观类似于以下内容,并且可以有多条消息,因此它位于数组中 [{ "deviceId": "00000000001", "deviceType": "device", "receivedTs": 1539234374000, "readingL

我使用AWSAPI网关作为restful API端点,然后将记录传递到Kinesis流,然后传递到Lambda。但是,似乎有一些数据未到达Lambda函数

我一直在到处寻找一个例子或接近我正在寻找的东西,但没有运气

设备消息的外观类似于以下内容,并且可以有多条消息,因此它位于数组中

[{
    "deviceId": "00000000001",
    "deviceType": "device",
    "receivedTs": 1539234374000,
    "readingList": [{
        "channelId": 13,
        "type": "temperature",
        "value": 25.3,
        "unit": "°C"
     },{
        "channelId": 12,
        "type": "humidity",
        "value": 3.65,
        "unit": "V",
       "label": "primary-battery"
     }]
}]
我目前拥有的传出映射模板如下:

{
    "StreamName": "my-stream",
    "Records": [
       #foreach($elem in $input.path('$'))
          #set($event =  "{
            ""deviceId"": $elem.deviceId
            ""deviceType"": $elem.deviceType,
            ""receivedTs"": $elem.receivedTs,
            ""readingList"": [
            #foreach($reading in $elem.readingList)
            {
                ""channelId"": $reading.channelId,
                ""type"": ""$reading.type"",
                ""value"": $reading.value,
                ""unit"": ""$reading.unit"",
                ""label"": ""$reading.label""
            }]
            #if($foreach.hasNext),#end
         #end            
         }")
     {
     "Data": "$util.base64Encode($event)",
     "PartitionKey": "$elem.deviceType"
     }#if($foreach.hasNext),#end
      #end
    ]
}
下面是我的CloudWatch日志从lambda函数内的处理中显示的内容。显示了一些数据,但对象的readingList数组没有正确填写,结果显示为空。我觉得这与映射模板中的foreach循环有关,但我似乎不知道是什么

Beginning to process all 1 records...
Event Name: aws:kinesis:record
Getting record contents.
Record contents: {
"deviceId": "00000000001",
"deviceType": "device",
"receivedTs": "1539234374000",
"readingList": [{
    "channelId": "",
    "type": "",
    "value": "",
    "unit": "",
    "label": ""
    }]
}
lambda函数没有什么特别之处。对于测试,它只是使用AWS提供的示例代码将消息内容写入控制台


如果任何人有任何想法或一些有用的链接,将不胜感激。谢谢你抽出时间来。

好吧,我让它工作起来了。这与模板有关

{
    "StreamName": "my-stream",
    "Records": [
       #foreach($elem in $input.path('$'))
          #set($event =  "{
            ""deviceId"": $elem.deviceId,
            ""deviceType"": $elem.deviceType,
            ""receivedTs"": $elem.receivedTs,
            ""readingList"": [
            #foreach($reading in $elem.readingList)
            {
                ""channelId"": $reading.channelId,
                ""type"": ""$reading.type"",
                ""value"": $reading.value,
                ""unit"": ""$reading.unit"",
                ""label"": ""$reading.label""
            }
            #if($foreach.hasNext),#end
         #end
         ]           
     }")
     {
     "Data": "$util.base64Encode($event)",
     "PartitionKey": "$elem.deviceType"
     }#if($foreach.hasNext),#end
      #end
    ]
}
首先,我在$elem.deviceId后面缺少了一个逗号,readingList数组的右括号在foreach循环的结尾后面

错过那些愚蠢的错误,我一定是受够了