AWS store MQTT message to DynamoDB

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I built a python script that sends MQTT message periodically.

This is the JSON string sent to the backend:

{"Id": "1234", "Ut": 1488395951, "Temp": 22.86, "Rh": 48.24} 

On backend side, I want to store the MQTT message into DynamoDB table.

I followed an Amazon tutorial and the data contained into the MQTT messages are stored into the table.

My problem regards the table columns.

The table has only 3 colums:

  • Id: Partition Key
  • Ut: Sort Key
  • Payload: contains the mqtt message.

It is possible to have columns for each key contained into MQTT message?

I would have this columns: - Id - Ut - Temp - Rh

Thanks for the help!

回答1:

Yes you can.

In DynamoDB you don't need to create what you call "columns". The only requirements for successful put operation is that you provide your primary attributes (in your case Id and Ut).

From the docs:

A map of attribute name/value pairs, one for each attribute. Only the primary key attributes are required; you can optionally provide other attribute name-value pairs for the item.

In python you can do something similar to this (may need to check the syntax and adjust column types):

import boto3 client = boto3.client('dynamodb') response = client.put_item(     TableName = 'Messages',     Item={         'Id': {             'S': '1234'         },         'Ut': {             'S': '1488395951'         },         'Temp': {             'S': '22.86'         },         'Rh': {             'S': '48.24'         }     } ) 


回答2:

Yes, definitely. You can create columns for whatever you want to store in a DynamoDB table, instead of or in addition to storing the full payload.

Following Step 3.1: Create a New Item from this tutorial, you would update your put_item code to include all your item details...

#Parse your JSON message and get out all your attributes id = message["Id"] ut = message["Ut"] temp = message["Temp"] rh = message["Rh"]  response = table.put_item(    Item={         'id': id,         'ut': ut,         'temp': temp,         'rh': rh     } ) 

Now if you look at your table in the DynamoDB console, you'll see new columns have been created for your additional attributes.



回答3:

I assume you're trying to come up with an IoT Gateway rule storing your message in a DynamoDB table. Unfortunately the IoT rules do not allow to store data in multiple columns - you can only store the payload (or a subset thereof) in a single column. There is currently no provision to extract properties from the payload into several different columns.

The only way around this (which I've found so far) is to call a lambda function through the IoT rules which runs code similar to what the other answers have shown.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!