AWS IoT,规则,Lambda函数,DynamoDB(将流式AWS IoT Core数据存储到DynamoDB)。

huangapple go评论74阅读模式
英文:

AWS IoT , Rules, Lambda Function, DynamoDB (storing streaming AWS iot core data to dynamoDB)

问题

我已经参考了这篇Medium文章:https://medium.com/dev-jam/tutorial-part-2-aws-iot-rules-lambda-function-dynamodb-40a7d4ea35b9

上面是实现的完整图片。

我用于接收来自IoT核心到DynamoDB的Lambda函数如下所示:

console.log('Loading function');
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient();

const collection = "IoTCatalog";

// 处理程序 Lambda 函数
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
   const params = {
    TableName: collection,
    Item:{
        "serialNumber": event.serialNumber,
        "timestamp": event.dateTime,
        "activated": event.activated,
        "clientId": event.clientId,
        "device": event.device,
        "type": event.type,
        "payload": event.payload
        }
    };

    console.log("Saving Telemetry Data");
    
    dynamo.put(params, function(err, data) {
        if (err) {
            console.error("Unable to add device. Error JSON:", JSON.stringify(err, null, 2));
            context.fail();
        } else {
            console.log(data)
            console.log("Data saved:", JSON.stringify(params, null, 2));
            context.succeed();
            return {"message": "Item created in DB"}
        }
    });
}

我每3秒从IoT核心接收数据。我已部署了这个Lambda函数,但是当我运行NodeJS程序以传输数据到IoT核心每3秒一次时,我应该每3秒在DynamoDB表中接收数据,但我只接收到1次。

为了在DynamoDB中接收数据,我应该怎么做,因为我每3秒在IoT核心中接收数据?

英文:

I have refered this meduim article :<https://medium.com/dev-jam/tutorial-part-2-aws-iot-rules-lambda-function-dynamodb-40a7d4ea35b9>

AWS IoT,规则,Lambda函数,DynamoDB(将流式AWS IoT Core数据存储到DynamoDB)。
Above is the full picture of the implementation.

lambda function that I used for receiving data from iot core to dynamoDB is given below

console.log(&#39;Loading function&#39;);
const AWS = require(&#39;aws-sdk&#39;);
const dynamo = new AWS.DynamoDB.DocumentClient();

const collection =&quot;IoTCatalog&quot;

// Handler lamda function
exports.handler = function(event, context) {
console.log(&#39;Received event:&#39;, JSON.stringify(event, null, 2));
   const params = {
    TableName: collection,
    Item:{
        &quot;serialNumber&quot;: event.serialNumber,
        &quot;timestamp&quot;: event.dateTime,
        &quot;activated&quot;: event.activated,
        &quot;clientId&quot;: event.clientId,
        &quot;device&quot;: event.device,
        &quot;type&quot;: event.type,
        &quot;payload&quot;: event.payload
        }
    };

    console.log(&quot;Saving Telemetry Data&quot;);
    
    dynamo.put(params, function(err, data) {
        if (err) {
            console.error(&quot;Unable to add device. Error JSON:&quot;, JSON.stringify(err, null, 2));
            context.fail();
        } else {
            console.log(data)
            console.log(&quot;Data saved:&quot;, JSON.stringify(params, null, 2));
            context.succeed();
            return {&quot;message&quot;: &quot;Item created in DB&quot;}
        }
    });
}

I am receiving data in iot core for every 3 seconds.
And I have deployed this lambda function, but when I run the NodeJS program which functions to transmit data to iot core after every 3 seconds, and so I should be receiving data every 3 seconds in DynamoDB table, but I am receiving only 1 time.

What should I do to receive data in DynamoDB as I am receiving data in IOT core after every 3 seconds?

答案1

得分: 1

DynamoDB项是根据它们的主键(分区键和排序键的组合)是唯一的。如果您使用相同的键写入多个项,那么每次都会简单地覆盖该项。

您需要确保每次插入时您的键都是唯一的。例如,您可以使用纳秒时间戳作为排序键。

英文:

DynamoDB items are unique based on their primary key (partition and sort key combined). If you are writing multiple items with the same key, then you are simply overwriting the item each time.

You need to ensure you key is unique for each and every insert. For example, you can use a nanosecond timestamp as the sort key.

答案2

得分: 0

我建议将Lambda函数删除,并直接将IoT规则操作连接到DynamoDB(这将降低您解决方案的成本)。在这里,Lambda函数没有提供任何有价值的逻辑(除非您隐藏了某些内容)。另外,正如Lee所提到的,确保发送到DDB的每条消息都是唯一的(分区键和排序键)。分区键可以是序列号,排序键可以是时间戳。

英文:

I would highly suggest to remove the lambda and connect directly IoT Rule Action to DynamoDB (this would reduce the cost of your solution). The lambda here doesn't provide any valuable logic (unless you have hidden something). Also, as mentioned by Lee, make sure every message sent to DDB is unique (partition and sort key). Partition key could be serial number and sort key the timestamp.

huangapple
  • 本文由 发表于 2023年3月8日 16:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定