IoT messages have a new home !!
IoT messages have a new home !!
^
/o \
| # |
LL
Yes, you heard it right! Do you know that you can change the storage residence (schema/tables) of your IOT messages to the place you want!! This short blog shows you exactly how it can be achieved.
This article assumes that you have a basic idea of how to use SAP HANA IOT on both features of MMS & RDMS, in case not, please first go through one of a very good article on SCN given below –
http://scn.sap.com/community/developer-center/cloud-platform/blog/2015/06/09/using-the-sap-hcp-iot-services(http://www.slideshare.net/saphcp/sap-hcp-iot)
So, once you deploy your IoT MMS, you will see a default data binding & associated schema created automatically for you. Something like this –
As you see here, the DB/schema id is taken as <trialaccountid>trial.iotmms.web automatically.
And the iotmms application creates a default binding to this default data source like this –
So whenever you receive a message, the message will go into your “<trialaccount>.iotmms.web” schema in a table starting with “IOT_<messagetypeID>”. You may want to keep your IoT schema under check and would like to keep all your data at one place to your own database/schema instead of keeping it to the default.
The process of changing this starts very easy six step process, follow me –
Step1 – Stop the IOTMMS Application
Goto your cockpit and select “Java Application”, you will the application named “iotmms” on the right hand side (This application lists by itself when you deploy the “Message Management Service”).
Stop the application by clicking “Stop”
Click “Ok”
This will stop the “iotmms” application.
Step 2 – Delete the default data binding
Click on “Data source bindings” & then click on the (delete) icon
Delete the binding by clicking “OK” button
Step 3 – Create a new data binding.
Click “New Binding” to create a new binding.
Clicking on new binding will open up a dialog like this –
Keep the data source as “<default>”, select your MDC in the place of “DB/Schema ID” and your custom logon with the database user and it’s corresponding password.
Click “Save”
Clicking on “Save” will create your default binding for “iotmms” application.
(Prerequisite to this step – you shall have a MDC database running on your account & you also need a database user created, I’m assuming this to be “iotadmin” for the sake of easy understandability. Ensure you give this user enough rights to access your MDC database).
This step does half of our job!!
The java application named “iotmms” looks at this databinding and finds that now it needs to use a new DB/Schema ID instead of default and the user by which it needs to perform the action is “iotadmin”.
Step 4 – Create a table
Now is the time to build your new castle! Create a table under your application/project schema.
I am planning to move all my messages to schema named “almirah” and table called “shelf2016”, so here is the script to prepare my shelf’s structure.
CREATE column TABLE "almirah"."shelf2016"
(
"G_DEVICE" VARCHAR(255) CS_STRING,
"G_CREATED" VARCHAR(255) CS_STRING,
"C_MESSAGE" VARCHAR(255) CS_STRING,
"C_REPERCUSSION" VARCHAR(255) CS_STRING
)
Step 5 – Register the Service mapping
It’s time to define a new address to let the world know the messages shall come to my new address now! To define the same, open postman and provide the following –
- Request type – Choose “PUT” from the list of request types.
2. Requested resource – Put the below URL, don’t forget to substitute your hana accound id in the URL
https://iotmms<yourhanaaccountid>trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/processing/
Authorization – Choose “BasicAuth” in the type field, you will be prompted to assign the values of username & password, enter your hana trial account id and it’s corresponding password. Click “Update Request”.
3. Headers –
- Add “Content-type” as “application/json”
- Note that you don’t have to touch the “Authorization” key and value pair, remember we added authorization in step 3, the values you see here are coming from there! So, touch it not!
4. Body –
{
"deviceType": "52b4aebce8643885b93b",
"messageType": "a1bb5c4916575af30c53",
"processingServices": [
{
"name": "sql",
"properties": {
"mms.processing.sql.mappings": {
"table_name": "shelf2016",
"column_name":[
{
"field":"G_DEVICE",
"column":"G_DEVICE"
},
{
"field":"G_CREATED",
"column": "G_CREATED"
},
{
"field":"message",
"column":"C_MESSAGE"
},
{
"field":"repercussion",
"column": "C_REPERCUSSION"
}
]
},
"mms.processing.sql.schema_name": "almirah",
"mms.processing.sql.hana.table_type": "column"
}
}
]
}
Let’s read what’s in the stars now..:-p
Star 1 shows which devicetype is registering for a new address, the value of this attribute is the ID of the devicetype you want all the messages to go to new address.
Star 2 – shows you the message type for registration. As you know one device type can have many message types and hence specifying message type makes perfect sense to uniquely identify which all messages shall go to this new address.
Star 3 – tells the table in which new messages will go to
Star 4 – Tells which schema this table resides in.
So, stars are showing us the way…!!
Once you run the request by pressing “Send” button, you shall see a message like this which confirms that the registration is done and whenever a message of specified ID & device ID comes, it will go the schema and table defined in the request.
Cool! Time to Test !!
Step 6 – Resend a new message
Send a message from Postman or any rest client (including one in the cockpit) and see the messaging pouring in to the new schema & table..!
Did you see your friend just sent a message to help him on getting a new home..! Go ahead..sharing is the new caring..!! Enjoy!!