【问题标题】:Using a VTL #foreach loop on array of objects not returning values在不返回值的对象数组上使用 VTL #foreach 循环
【发布时间】:2019-05-25 22:22:18
【问题描述】:

我使用 AWS API Gateway 作为 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 提供的示例代码将消息内容写入控制台。

如果有人有任何想法或一些有用的链接,将不胜感激。感谢您的宝贵时间。

【问题讨论】:

    标签: amazon-web-services api templates mapping vtl


    【解决方案1】:

    好吧,我让它工作了。这和模板有关。

    {
        "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 循环的 #end 之后。

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

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 2018-10-08
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2015-03-26
      • 2018-09-15
      相关资源
      最近更新 更多