|
| 1 | +--- |
| 2 | +title: Extend Azure IoT Central with custom analytics | Microsoft Docs |
| 3 | +description: As a solution developer, configure an IoT Central application to do custom analytics and visualizations. This solution uses Azure Databricks. |
| 4 | +author: dominicbetts |
| 5 | +ms.author: dobett |
| 6 | +ms.date: 05/21/2019 |
| 7 | +ms.topic: conceptual |
| 8 | +ms.service: iot-central |
| 9 | +services: iot-central |
| 10 | +ms.custom: mvc |
| 11 | +manager: philmea |
| 12 | +--- |
| 13 | + |
| 14 | +# Extend Azure IoT Central with custom analytics |
| 15 | + |
| 16 | +This how-to guide shows you, as a solution developer, how to extend your IoT Central application with custom analytics and visualizations. The example uses an [Azure Databricks](https://docs.microsoft.com/azure/azure-databricks/) workspace to analyze the IoT Central telemetry stream and to generate visualizations such as [box plots](https://wikipedia.org/wiki/Box_plot). |
| 17 | + |
| 18 | +This how-to guide shows you how to extend IoT Central beyond what it can already do with the [built-in analytics tools](howto-create-analytics.md). |
| 19 | + |
| 20 | +In this how-to guide, you learn how to: |
| 21 | + |
| 22 | +* Stream telemetry from an IoT Central application using *continuous data export*. |
| 23 | +* Create an Azure Databricks environment to analyze and plot device telemetry. |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +To complete the steps in this how-to guide, you need an active Azure subscription. |
| 28 | + |
| 29 | +If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 30 | + |
| 31 | +### IoT Central application |
| 32 | + |
| 33 | +Create an IoT Central application from the [Azure IoT Central - My applications](https://aka.ms/iotcentral) page with the following settings: |
| 34 | + |
| 35 | +| Setting | Value | |
| 36 | +| ------- | ----- | |
| 37 | +| Payment plan | Pay-As-You-Go | |
| 38 | +| Application template | Sample Contoso | |
| 39 | +| Application name | Accept the default or choose your own name | |
| 40 | +| URL | Accept the default or choose your own unique URL prefix | |
| 41 | +| Directory | Your Azure Active Directory tenant | |
| 42 | +| Azure subscription | Your Azure subscription | |
| 43 | +| Region | East US | |
| 44 | + |
| 45 | +The examples and screenshots in this article use the **East US** region. Choose a location close to you and make sure you create all your resources in the same region. |
| 46 | + |
| 47 | +### Resource group |
| 48 | + |
| 49 | +Use the [Azure portal to create a resource group](https://portal.azure.com/#create/Microsoft.ResourceGroup) called **IoTCentralAnalysis** to contain the other resources you create. Create your Azure resources in the same location as your IoT Central application. |
| 50 | + |
| 51 | +### Event Hubs namespace |
| 52 | + |
| 53 | +Use the [Azure portal to create an Event Hubs namespace](https://portal.azure.com/#create/Microsoft.EventHub) with the following settings: |
| 54 | + |
| 55 | +| Setting | Value | |
| 56 | +| ------- | ----- | |
| 57 | +| Name | Choose your namespace name | |
| 58 | +| Pricing tier | Basic | |
| 59 | +| Subscription | Your subscription | |
| 60 | +| Resource group | IoTCentralAnalysis | |
| 61 | +| Location | East US | |
| 62 | +| Throughput Units | 1 | |
| 63 | + |
| 64 | +### Azure Databricks workspace |
| 65 | + |
| 66 | +Use the [Azure portal to create an Azure Databricks Service](https://portal.azure.com/#create/Microsoft.Databricks) with the following settings: |
| 67 | + |
| 68 | +| Setting | Value | |
| 69 | +| ------- | ----- | |
| 70 | +| Workspace name | Choose your workspace name | |
| 71 | +| Subscription | Your subscription | |
| 72 | +| Resource group | IoTCentralAnalysis | |
| 73 | +| Location | East US | |
| 74 | +| Pricing Tier | Standard | |
| 75 | + |
| 76 | +When you've created the required resources, your **IoTCentralAnalysis** resource group looks like the following screenshot: |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +## Create an event hub |
| 81 | + |
| 82 | +You can configure an IoT Central application to continuously export telemetry to an event hub. In this section, you create an event hub to receive telemetry from your IoT Central application. The event hub delivers the telemetry to your Stream Analytics job for processing. |
| 83 | + |
| 84 | +1. In the Azure portal, navigate to your Event Hubs namespace and select **+ Event Hub**. |
| 85 | +1. Name your event hub **centralexport**, and select **Create**. |
| 86 | +1. In the list of event hubs in your namespace, select **centralexport**. Then choose **Shared access policies**. |
| 87 | +1. Select **+ Add**. Create a policy named **Listen** with the **Listen** claim. |
| 88 | +1. When the policy is ready, select it in the list, and then copy the **Connection string-primary key** value. |
| 89 | +1. Make a note of this connection string, you use it later when you configure your Databricks notebook to read from the event hub. |
| 90 | + |
| 91 | +Your Event Hubs namespace looks like the following screenshot: |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +## Configure export in IoT Central |
| 96 | + |
| 97 | +Navigate to the [IoT Central application](https://aka.ms/iotcentral) you created from the Contoso template. In this section, you configure the application to stream the telemetry from its simulated devices to your event hub. To configure the export: |
| 98 | + |
| 99 | +1. Navigate to the **Continuous Data Export** page, select **+ New**, and then **Azure Event Hubs**. |
| 100 | +1. Use the following settings to configure the export, then select **Save**: |
| 101 | + |
| 102 | + | Setting | Value | |
| 103 | + | ------- | ----- | |
| 104 | + | Display Name | Export to Event Hubs | |
| 105 | + | Enabled | On | |
| 106 | + | Event Hubs namespace | Your Event Hubs namespace name | |
| 107 | + | Event hub | centralexport | |
| 108 | + | Measurements | On | |
| 109 | + | Devices | Off | |
| 110 | + | Device Templates | Off | |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +Wait until the export status is **Running** before you continue. |
| 115 | + |
| 116 | +## Configure Databricks workspace |
| 117 | + |
| 118 | +In the Azure portal, navigate to your Azure Databricks service and select **Launch Workspace**. A new tab opens in your browser and signs you in to your workspace. |
| 119 | + |
| 120 | +### Create a cluster |
| 121 | + |
| 122 | +On the **Azure Databricks** page, under the list of common tasks, select **New Cluster**. |
| 123 | + |
| 124 | +Use the information in the following table to create your cluster: |
| 125 | + |
| 126 | +| Setting | Value | |
| 127 | +| ------- | ----- | |
| 128 | +| Cluster Name | centralanalysis | |
| 129 | +| Cluster Mode | Standard | |
| 130 | +| Databricks Runtime Version | 5.3 (Scala 2.11, Spark 2.4.0) | |
| 131 | +| Python Version | 3 | |
| 132 | +| Enable Autoscaling | No | |
| 133 | +| Terminate after minutes of inactivity | 30 | |
| 134 | +| Worker Type | Standard_DS3_v2 | |
| 135 | +| Workers | 1 | |
| 136 | +| Driver Type | Same as worker | |
| 137 | + |
| 138 | +Creating a cluster may take several minutes, wait for the cluster creation to complete before you continue. |
| 139 | + |
| 140 | +### Install libraries |
| 141 | + |
| 142 | +On the **Clusters** page, wait until the cluster state is **Running**. |
| 143 | + |
| 144 | +The following steps show you how to import the library your sample needs into the cluster: |
| 145 | + |
| 146 | +1. On the **Clusters** page, wait until the state of the **centralanalysis** interactive cluster is **Running**. |
| 147 | + |
| 148 | +1. Select the cluster and then choose the **Libraries** tab. |
| 149 | + |
| 150 | +1. On the **Libraries** tab, choose **Install New**. |
| 151 | + |
| 152 | +1. On the **Install Library** page, choose **Maven** as the library source. |
| 153 | + |
| 154 | +1. In the **Coordinates** textbox, enter the following value: `com.microsoft.azure:azure-eventhubs-spark_2.11:2.3.10` |
| 155 | + |
| 156 | +1. Choose **Install** to install the library on the cluster. |
| 157 | + |
| 158 | +1. The library status is now **Installed**: |
| 159 | + |
| 160 | +  |
| 161 | + |
| 162 | +### Import a Databricks notebook |
| 163 | + |
| 164 | +Use the following steps to import a Databricks notebook that contains the Python code to analyze and visualize your IoT Central telemetry: |
| 165 | + |
| 166 | +1. Navigate to the **Workspace** page in your Databricks environment. Select the dropdown next to your account name and then choose **Import**. |
| 167 | + |
| 168 | +1. Choose to import from a URL and enter the following address: [https://github.com/Azure-Samples/iot-central-docs-samples/blob/master/databricks/IoT%20Central%20Analysis.dbc?raw=true](https://github.com/Azure-Samples/iot-central-docs-samples/blob/master/databricks/IoT%20Central%20Analysis.dbc?raw=true) |
| 169 | + |
| 170 | +1. To import the notebook, choose **Import**. |
| 171 | + |
| 172 | +1. Select the **Workspace** to view the imported notebook: |
| 173 | + |
| 174 | +  |
| 175 | + |
| 176 | +1. Edit the code in the first Python cell to add the Event Hubs connection string you saved previously: |
| 177 | + |
| 178 | + ```python |
| 179 | + from pyspark.sql.functions import * |
| 180 | + from pyspark.sql.types import * |
| 181 | + |
| 182 | + ###### Event Hub Connection strings ###### |
| 183 | + telementryEventHubConfig = { |
| 184 | + 'eventhubs.connectionString' : '{your Event Hubs connection string}' |
| 185 | + } |
| 186 | + ``` |
| 187 | + |
| 188 | +## Run analysis |
| 189 | + |
| 190 | +To run the analysis, you must attach the notebook to the cluster: |
| 191 | + |
| 192 | +1. Select **Detached** and then select the **centralanalysis** cluster. |
| 193 | +1. If the cluster isn't running, start it. |
| 194 | +1. To start the notebook, select the run button. |
| 195 | + |
| 196 | +You may see an error in the last cell. If so, check the previous cells are running, wait a minute for some data to be written to storage, and then run the last cell again. |
| 197 | + |
| 198 | +### View smoothed data |
| 199 | + |
| 200 | +In the notebook, scroll down to cell 14 to see a plot of the rolling average humidity by device type. This plot continuously updates as streaming telemetry arrives: |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | +You can resize the chart in the notebook. |
| 205 | + |
| 206 | +### View box plots |
| 207 | + |
| 208 | +In the notebook, scroll down to cell 20 to see the [box plots](https://en.wikipedia.org/wiki/Box_plot). The box plots are based on static data so to update them you must rerun the cell: |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | +You can resize the plots in the notebook. |
| 213 | + |
| 214 | +## Tidy up |
| 215 | + |
| 216 | +To tidy up after this how-to and avoid unnecessary costs, delete the **IoTCentralAnalysis** resource group in the Azure portal. |
| 217 | + |
| 218 | +You can delete the IoT Central application from the **Management** page within the application. |
| 219 | + |
| 220 | +## Next steps |
| 221 | + |
| 222 | +In this how-to guide, you learned how to: |
| 223 | + |
| 224 | +* Stream telemetry from an IoT Central application using *continuous data export*. |
| 225 | +* Create an Azure Databricks environment to analyze and plot telemetry data. |
| 226 | + |
| 227 | +Now that you know how to create custom analytics, the suggested next step is to learn how to [Visualize and analyze your Azure IoT Central data in a Power BI dashboard](howto-connect-powerbi.md). |
0 commit comments