Stream data from our IoT client to a DynamoDB table. We will use the AWS IoT rules to stream data directly into DynamoDB, this is a great and easy way to get data into an extremely fast lookup store for later processing. We need to create a DynamoDB table. Open the DynamoDB dashboard Let's call this iotddb Our partition key can just be "SeqNumber" type String Add a sort key, a requirement for AWS IoT integration The sort key can be "SeqSort" type String Keep all other defaults Lab 8 - Streaming data to DynamoDB Step 1
One of the great things with a NoSQL datastore is that we don't have to define our anything else for our schema. Once it has finished being created move to step 2. We are going to create a new rule for DynamoDB. Open the AWS IoT dashboard Click Create Resource and pick Create Rule We can call this rule iotDynamoDB Our Attribute will be the usual * Our From will be ddb Choose the DynamoDB Action Tablename is iotddb Hashkey and Rangekey should populate as SeqNumber + SeqSort The hash value will be ${SeqNumber} The range value will be ${SeqSort} Step 2
Select your role. Make sure your role has DynamoDB Permissions! As with the other labs we'll want to modify our thingtest.js script to use our new topic but to also specify our Hash and Range keys. Your thingtest.js should look like the following: Step 3
// Lab 8 - DynamoDB var awsIot = require('aws-iot-device-sdk'); var device = awsIot.device({ "host": "data.iot.us-west-2.amazonaws.com", "port": 8883, "clientId": "1234", "thingName": "thingtest", "caPath": "./root-CA.cer", "certPath": "./certificate.pem.crt", "keyPath": "./private.pem.key", "region": "us-west-2" }); var message = { val1: "Value 1", val2: "Value 2", val3: "Value 3", message: "Test Message", SeqNumber: "", SeqSort: "1" }; device.on('connect', function() { console.log('Connected!'); setTimeout(function() { for(x=0;x<100;x++) { message.val1 = "Value 1-" + x; message.val2 = "Value 1-" + x; message.val3 = "Value 1-" + x; message.message = "Test Message " + x; message.SeqNumber = x; device.publish('ddb', JSON.stringify(message)); console.log("Published message " + x + " Data: " + JSON.stringify(message)); } }, 2500); console.log('Sending to DynamoDB ...'); }); You can now run JavaScript
node thingtest.js You should see 100 messages sent directly to DynamoDB. You can now open the DynamoDB console and click your items tab on your table to verify your data arrived. Bash
AWS IoT 핸즈온 워크샵 - 실습 5. DynamoDB에 센서 데이터 저장하기 (김무현 솔루션즈 아키텍트)

AWS IoT 핸즈온 워크샵 - 실습 5. DynamoDB에 센서 데이터 저장하기 (김무현 솔루션즈 아키텍트)

  • 1.
    Stream data fromour IoT client to a DynamoDB table. We will use the AWS IoT rules to stream data directly into DynamoDB, this is a great and easy way to get data into an extremely fast lookup store for later processing. We need to create a DynamoDB table. Open the DynamoDB dashboard Let's call this iotddb Our partition key can just be "SeqNumber" type String Add a sort key, a requirement for AWS IoT integration The sort key can be "SeqSort" type String Keep all other defaults Lab 8 - Streaming data to DynamoDB Step 1
  • 2.
    One of thegreat things with a NoSQL datastore is that we don't have to define our anything else for our schema. Once it has finished being created move to step 2. We are going to create a new rule for DynamoDB. Open the AWS IoT dashboard Click Create Resource and pick Create Rule We can call this rule iotDynamoDB Our Attribute will be the usual * Our From will be ddb Choose the DynamoDB Action Tablename is iotddb Hashkey and Rangekey should populate as SeqNumber + SeqSort The hash value will be ${SeqNumber} The range value will be ${SeqSort} Step 2
  • 3.
    Select your role. Makesure your role has DynamoDB Permissions! As with the other labs we'll want to modify our thingtest.js script to use our new topic but to also specify our Hash and Range keys. Your thingtest.js should look like the following: Step 3
  • 4.
    // Lab 8- DynamoDB var awsIot = require('aws-iot-device-sdk'); var device = awsIot.device({ "host": "data.iot.us-west-2.amazonaws.com", "port": 8883, "clientId": "1234", "thingName": "thingtest", "caPath": "./root-CA.cer", "certPath": "./certificate.pem.crt", "keyPath": "./private.pem.key", "region": "us-west-2" }); var message = { val1: "Value 1", val2: "Value 2", val3: "Value 3", message: "Test Message", SeqNumber: "", SeqSort: "1" }; device.on('connect', function() { console.log('Connected!'); setTimeout(function() { for(x=0;x<100;x++) { message.val1 = "Value 1-" + x; message.val2 = "Value 1-" + x; message.val3 = "Value 1-" + x; message.message = "Test Message " + x; message.SeqNumber = x; device.publish('ddb', JSON.stringify(message)); console.log("Published message " + x + " Data: " + JSON.stringify(message)); } }, 2500); console.log('Sending to DynamoDB ...'); }); You can now run JavaScript
  • 5.
    node thingtest.js You shouldsee 100 messages sent directly to DynamoDB. You can now open the DynamoDB console and click your items tab on your table to verify your data arrived. Bash