lab 8 - streaming data to dynamodb - amazon s3 · pdf filestream data from our iot client to a...

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

Upload: hadan

Post on 17-Feb-2018

222 views

Category:

Documents


1 download

TRANSCRIPT

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

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

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

// 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 andclick your items tab on your table to verify your data arrived.

Bash