6
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

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

Embed Size (px)

Citation preview

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

Stream data from our IoT client to a DynamoDB table. We will use the AWS IoT rules to stream data directlyinto DynamoDB, this is a great and easy way to get data into an extremely fast lookup store for laterprocessing.

We need to create a DynamoDB table.

Open the DynamoDB dashboardLet's call this iotddbOur partition key can just be "SeqNumber" type StringAdd a sort key, a requirement for AWS IoT integrationThe sort key can be "SeqSort" type StringKeep all other defaults

Lab 8 - Streaming data to DynamoDB

Step 1

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

One of the great things with a NoSQL datastore is that we don't have to define our anything else for ourschema. Once it has finished being created move to step 2.

We are going to create a new rule for DynamoDB.

Open the AWS IoT dashboardClick Create Resource and pick Create RuleWe can call this rule iotDynamoDBOur Attribute will be the usual *Our From will be ddbChoose the DynamoDB ActionTablename is iotddbHashkey and Rangekey should populate as SeqNumber + SeqSortThe hash value will be ${SeqNumber}The range value will be ${SeqSort}

Step 2

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

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 ourHash and Range keys.

Your thingtest.js should look like the following:

Step 3

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

// 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

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

node thingtest.js

You should see 100 messages sent directly to DynamoDB. You can now open the DynamoDB console andclick your items tab on your table to verify your data arrived.

Bash

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