This article is designed to help you quickly set up a simple Azure Stream Analytics job, with Azure Machine Learning integration. We will use a sentiment analytics Machine Learning model from the Cortana Intelligence Gallery to analyze streaming text data, and determine the sentiment score in real time. The information in this article can help you understand scenarios such as real-time sentiment analytics on streaming Twitter data, analyze records of customer chats with support staff, and evaluate comments on forums, blogs, and videos, in addition to many other real-time, predictive scoring scenarios.
This article offers a sample CSV file with text as input in Azure Blob storage, shown in the following image. The job applies the sentiment analytics model as a user-defined function (UDF) on the sample text data from the blob store. The end result is placed in the same blob store in another CSV file.
The following image demonstrates this configuration. For a more realistic scenario, you can replace Blob storage with streaming Twitter data from an Azure Event Hubs input. Additionally, you could build a Microsoft Power BI real-time visualization of the aggregate sentiment.
The prerequisites for completing the tasks that are demonstrated in this article are as follows:
- An active Azure subscription.
- A CSV file with some data in it. You can download the file shown in Figure 1 from GitHub, or you can create your own file. For this article, we assume that you use the one provided for download on GitHub.
At a high level, to complete the tasks demonstrated in this article, you'll do the following:
- Upload the CSV input file to Azure Blob storage.
- Add a sentiment analytics model from the Cortana Intelligence Gallery to your Azure Machine Learning workspace.
- Deploy this model as a web service in the Machine Learning workspace.
- Create a Stream Analytics job that calls this web service as a function, to determine sentiment for the text input.
- Start the Stream Analytics job and observe the output.
Create a storage blob and upload the CSV input file
For this step, you can use any CSV file, such as the one already specified as available for download on GitHub. Uploading the csv file is simple as it is an option included in creating a storage blob.
For our tutorial, create a new storage account by clicking New and then searching for 'storage account' and then selecting the resulting icon for storage account and providing details for the creation of the account. Provide a Name (azuresamldemosa in my example), create or use an existing Resource group and specify a Location (for location, it is important that all the resources created in this demo all use the same location if possible).
Once that is completed you can click on Blob service and create a blob container.
Then provide a Name for the container (azuresamldemoblob in my example) and verify the Access type is set to 'blob'.
Now we can populate the blob with our data. Select Files and then select the file on your local drive that you downloaded from GitHub. I selected Block blob and 4 MB as a size these should be fine for this demonstration. Then select Upload and the portal will create a blob with the text sample for you.
Now that the sample data is in a blob it is time to enable the sentiment analysis model in Cortana Intelligence Gallery.
Add the sentiment analytics model from the Cortana Intelligence Gallery
- Download the predictive sentiment analytics model from the Cortana Intelligence Gallery.
In Machine Learning Studio, select Open in Studio.
Sign in to go to the workspace. Select the location that best suits your own location.
- Click Run at the bottom of the page.
- After the process runs successfully, select Deploy Web Service.
- The sentiment analytics model is ready to use. To validate, select the Test button and provide text input, such as, “I love Microsoft.” The test should return a result similar to the following:
'Predictive Mini Twitter sentiment analysis Experiment' test returned ["4","0.715057671070099"]...
In the Apps column, select the link for Excel 2010 or earlier workbook to get your API key and the URL that you’ll need later to set up the Stream Analytics job. (This step is required only to use a Machine Learning model from another Azure account workspace. This article assumes this is the case, to address that scenario.)
Note the web service URL and access key from the downloaded Excel file, as shown below:
Create a Stream Analytics job that uses the Machine Learning model
- Go to the Azure portal.
Click New > Intelligence + analytics > Stream Analytics. Enter a name for your job in Job name, specify an existing resource group or create a new one as required, and enter the appropriate location for the job in the Location field.
After the job is created, on the Inputs tab, select Add an Input.
Select Add and then specify an Input alias, select Data stream, Blob Storage as the input, and then select Next.
On the Blob Storage Settings page of the wizard, provide the storage account blob container name you defined earlier when you uploaded the data. Click Next. For Event Serialization Format, select CSV. Accept the default values for the rest of the Serialization settings page. Click OK.
On the Outputs tab, select Add an Output.
Click Blob Storage, and then enter the same parameters, except for the container. The value for Input was configured to read from the container named “test” where the CSV file was uploaded. For Output, enter “testoutput”.
Validate that the output’s Serialization settings are set to CSV, and then select the OK button.
On the Functions tab, select Add a Machine Learning Function.
On the Machine Learning Web Service Settings page, locate the Machine Learning workspace, web service, and default endpoint. For this article, apply the settings manually to gain familiarity with configuring a web service for any workspace, as long as you know the URL and have the API key. Enter the endpoint URL and API key. Click OK. Note that the Function Alias is 'sentiment'.
On the Query tab, modify the query as follows:
WITH sentiment AS ( SELECT text, sentiment(text) as result from datainput ) Select text, result.[Scored Labels] Into testoutput From sentiment
Click Save to save the query.
Start the Stream Analytics job and observe the output
- Click Start at the top of the job page.
- On the Start Query Dialog, select Custom Time, and then select one day prior to when you uploaded the CSV to Blob storage. Click OK.
- Go to the Blob storage by using the tool you used to upload the CSV file, for example, Visual Studio.
- A few minutes after the job is started, the output container is created and a CSV file is uploaded to it.
Open the file in the default CSV editor. Something similar to the following should be displayed:
This article demonstrates how to create a Stream Analytics job that reads streaming text data and applies sentiment analytics to the data in real time. You can accomplish all of this without needing to worry about the intricacies of building a sentiment analytics model. This is one of the advantages of the Cortana Intelligence Suite.
You also can view Azure Machine Learning function-related metrics. To do this, select the Monitor tab. Three function-related metrics are displayed.
- Function Requests indicates the number of requests sent to a Machine Learning web service.
Function Events indicates the number of events in the request. By default, each request to a Machine Learning web service contains up to 1,000 events.