I started this series about IoT Hub communication to show how easy it is to get a long way. But we reached a point where simple telemetry communication is not the issue anymore, we want to make it stable, production ready and configurable.

In the previous blogs we used some fixed values in StreamAnalytics, just to generate some extra values (baselumen). Now we want to make this configurable.

This is part 6 of a series of blogs about IoT Hub communication using UWP:

  • Part 1: A very simple complex IoT Hub example
  • Part 2: Using the Visual Studio 2015 IoT Hub Extension and IoT Hub Device explorer
  • Part 3: Adding the power of BI to your IoT Hub
  • Part 4: Closing the Windows IoT Core feedback loop using Azure Functions
  • Part 5: PowerBI showing telemetry of past ten minutes, always!
  • Part 6: Add more flexibility to StreamAnalytics

StreamAnalytics supports reference data. And the solution is pretty simple: a blob with reference data (json or csv) must be provided in some Azure Storage and this is defined as an input.

This reference data can be combined with your telemetry using a T-SQL like ‘join’.

And to make it more interesting, it’s even possible to provide multiple blobs, each relative to a certain timestamp. So in time, StreamAnalytics can switch to a newer version of the same reference data.

This means we do not have to shut down our StreamAnalytics, just to alter reference data.

So let’s create a StreamAnalytics and add some reference data.

Create SteamAnalytics and simulate input

By now you must be familiar with how to create a StreamAnalytics. Create an EventHub and add it as output named ehoutput. And create an IoTHub as input named hubinput.

The query we want to use in out StreamAnalytics is this:

SELECT
devicename,
CAST(time as datetime) as time,
CAST(temperature as float) as temperature,
400 as baselumen,
CAST(lumen as float) as lumen
INTO
ehoutput
FROM
hubinput

First, we want to test the query using som test data.

So here is some test telemetry data save it as telemetry.json:

{"devicename":"DeviceOne","time":"2016-06-19T12:04:58.9011255","temperature":12.0,"lumen":51.0}
{"devicename":"DeviceOne","time":"2016-06-20T12:05:15.0404616","temperature":17.0,"lumen":10.0}
{"devicename":"DeviceOne","time":"2016-06-21T12:06:08.7120292","temperature":15.0,"lumen":675.0}
{"devicename":"DeviceOne","time":"2016-06-22T12:06:15.2415370","temperature":12.0,"lumen":71.0}
{"devicename":"DeviceOne","time":"2016-06-23T12:06:20.1060242","temperature":10.0,"lumen":798.0}
{"devicename":"DeviceOne","time":"2016-06-24T12:06:25.1604532","temperature":10.0,"lumen":169.0}
{"devicename":"DeviceOne","time":"2016-06-25T12:06:37.8339626","temperature":22.0,"lumen":213.0}
{"devicename":"DeviceOne","time":"2016-06-26T12:06:51.8159658","temperature":11.0,"lumen":786.0}
{"devicename":"DeviceOne","time":"2016-06-27T12:06:53.7527827","temperature":17.0,"lumen":374.0}
{"devicename":"DeviceOne","time":"2016-06-28T12:06:55.7277836","temperature":22.0,"lumen":110.0}
{"devicename":"DeviceOne","time":"2016-06-29T12:06:57.4483099","temperature":24.0,"lumen":912.0}
{"devicename":"DeviceOne","time":"2016-06-30T12:06:58.9950923","temperature":22.0,"lumen":839.0}

Just below the query, there is a test option. Just ‘feed’ the query this file and see that the query executes:

paramtest01

Great, the query is working, We see a baselumen of 400 for each device.

Let’s look at using reference data:

Adding a reference to reference data

When you add an input, you are asked to choose between a data stream or reference data. Until now we have always chosen the first option, now we choose the latter:

paramtest02

Remember, we are just telling StreamAnalytics how reference data can be discovered. So we need some Azure storage account, a path pattern which describes where the data is stored:

paramtest03

Here we tell StreamAnalytics the data will be available at ref/YYYY/MM/DD/HH/ref.json:

paramtest04

And the format of the reference data will be passed as json:

paramtest05

So now the administration is done. Let’s add actual data:

Adding reference data in storage

For this demonstration, we will use a simple example. We tell StreamAnalytics which device uses which baselumen. Note that the baselumen for this device is 600:

{"devicename":"DeviceOne","baselumen":600.0}

Note: in this demonstration, I only use one device so that one is added. Please add more devices on separate lines if needed.

Save this reference data as ref.json. We now want to upload this file into the blob storage. Unfortunately, the Azure portal has no abilities to actually add or delete blobs in a storage container. You can see the container but not touch it:

paramtest06

There are several free and free blob storage explorers. But in this example, I use Visual Studio. Within the Service Explorer view the container can be opened and an Upload Blob button is shown.

So select the ref.json. StreamAnalytics must be able to access it like https://%5BXYZ%5D.blob.core.windows.net/refcontainer/ref/2016/06/01/00/ref.json. All telemetry arriving after may 31 2016, will use this reference data. So we have to store it in a path like ‘ref/2016/06/01/00’:

paramtest07

So no we have a file with reference data in blob storage:

paramtest08

Just for the fun of it, let’s check it out in the Azure portal:

paramtest09

Not that’s a lot of blades!

Altering the query to reference the data

Ok, we have reference data so let’s reference it in a new version of the query:

SELECT
H.devicename,
CAST(H.time as datetime) as time,
CAST(H.temperature as float) as temperature,
R.baselumen as baselumen,
CAST(H.lumen as float) as lumen
INTO
ehoutput
FROM
hubinput H
JOIN refinput R
ON R.devicename = H.devicename

We join the incoming telemetry with the reference data based on the name of the device the telemetry is coming from. The baselumen column is replaced by a reference data column reference.

Testing the Stream Analytics query

Before we submit real telemetry, let’s test the query by hand. Because a second input is defined, we have to provide two input files:

paramtest11

Now check the test output. The telemetry of device ‘DeviceOne’ is enriched with a baselumen of 600.

paramtest12

Let’s go for the real deal. Let’s pass real telemetry.

Passing real telemetry through StreamAnalytics from device

Just like in previous parts of this series, we use an IoTHub for telemetry input. I use for this demonstration an Azure Function as output (See Part 4 for more details). Why? Azure Function provides logging by default. So after

So after sending some telemetry from ‘DeviceOne’ we see the telemetry including the expected baselumen with value 600!

paramtest13

So it’s quite simple to provide reference data.

Using updated reference data, upload new file

But what if a few days later, or even a few hours later, the reference data changes?

Let’s add a second file:

{"devicename":"DeviceOne","baselumen":700.0}

The update has an altered baselumen, now 700.

Save it, with the same filename but a slightly changed path, so it will become active twenty hours later:

paramtest14

So now we have two files active:

paramtest15

And again, after passing some telemetry we get updated telemetry with baselumen 700:

paramtest16

So we have seen how reference data can be added and used in StreamAnalytics. And we have proven that updated reference data will be used once the start date  of the data is reached.

Advertenties