Skip to content

Commit c9064a6

Browse files
Merge pull request #228968 from spelluru/asafresh0228
ASA - Inputs & CLI quickstart articles
2 parents e7a87f5 + 9c8457e commit c9064a6

File tree

4 files changed

+89
-110
lines changed

4 files changed

+89
-110
lines changed
41.8 KB
Loading
41.8 KB
Loading

articles/stream-analytics/quick-create-azure-cli.md

Lines changed: 81 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ ms.reviewer: jasonh
99
ms.workload: big-data
1010
ms.topic: quickstart
1111
ms.custom: mvc, devx-track-azurecli, mode-api
12-
ms.date: 07/01/2020
12+
ms.date: 02/28/2023
1313
---
1414

1515
# Quickstart: Create an Azure Stream Analytics job using the Azure CLI
16-
17-
In this quickstart, you use the Azure CLI to define a Stream Analytics job that filters real-time sensor messages with a temperature reading greater than 27. Your Stream Analytics job will read data from IoT Hub, transform the data, and write the data back to a container in blob storage. The input data used in this quickstart is generated by a Raspberry Pi online simulator.
16+
In this quickstart, you will use Azure CLI to define a Stream Analytics job that filters real-time sensor messages with a temperature reading greater than 27. The Stream Analytics job reads data from IoT Hub, transforms the data, and writes the output data to a container in a blob storage. The input data used in this quickstart is generated by a Raspberry Pi online simulator.
1817

1918
## Before you begin
2019

@@ -24,40 +23,39 @@ In this quickstart, you use the Azure CLI to define a Stream Analytics job that
2423

2524
- Create a resource group. All Azure resources must be deployed into a resource group. Resource groups allow you to organize and manage related Azure resources.
2625

27-
For this quickstart, create a resource group named *streamanalyticsrg* in the *eastus* location with the following [az group create](/cli/azure/group#az-group-create) command:
26+
For this quickstart, create a resource group named **streamanalyticsrg** in the **eastus** location with the following [az group create](/cli/azure/group#az-group-create) command:
2827

2928
```azurecli
3029
az group create --name streamanalyticsrg --location eastus
3130
```
3231

3332
## Prepare the input data
3433

35-
Before you define the Stream Analytics job, prepare the data that's used for the job's input.
36-
37-
The following Azure CLI code blocks are commands that prepare the input data required by the job. Review the sections to understand the code.
34+
Before you define the Stream Analytics job, prepare the data that's used for the job's input. The following Azure CLI commands prepare the **input** data required by the job.
3835

39-
1. Create an IoT Hub using the [az iot hub create](/cli/azure/iot/hub#az-iot-hub-create) command. This example creates an IoT Hub called **MyASAIoTHub**. Because IoT Hub names are unique, you need to come up with your own IoT Hub name. Set the SKU to F1 to use the free tier if it is available with your subscription. If not, choose the next lowest tier.
36+
1. Create an IoT Hub using the [az iot hub create](/cli/azure/iot/hub#az-iot-hub-create) command. This example creates an IoT Hub called **MyASAIoTHub**. As IoT Hub names must be globally unique, you may have to change the name if it's already taken. Set the SKU to F1 to use the free tier if it's available with your subscription. If not, choose the next lowest tier.
4037

4138
```azurecli
42-
az iot hub create --name "MyASAIoTHub" --resource-group streamanalyticsrg --sku S1
39+
iotHubName=MyASAIoTHub
40+
az iot hub create --name $iotHubName --resource-group streamanalyticsrg --sku S1
4341
```
4442
45-
Once the IoT hub has been created, get the IoT Hub connection string using the [az iot hub show-connection-string](/cli/azure/iot/hub) command. Copy the entire connection string and save it for when you add the IoT Hub as input to your Stream Analytics job.
43+
Once the IoT hub has been created, get the IoT Hub connection string using the [az iot hub connection-string show](/cli/azure/iot/hub/connection-string#az-iot-hub-connection-string-show) command. Copy the entire connection string and save it. You use it while adding the IoT Hub as an input to your Stream Analytics job.
4644
4745
```azurecli
48-
az iot hub show-connection-string --hub-name "MyASAIoTHub"
46+
az iot hub connection-string show --hub-name $iotHubName
4947
```
5048
51-
2. Add a device to IoT Hub using the [az iothub device-identity create](/cli/azure/iot/hub/device-identity#az-iot-hub-device-identity-create) command. This example creates a device called **MyASAIoTDevice**.
49+
2. Add a device to IoT Hub using the [az iothub device-identity create](/cli/azure/iot/hub/device-identity#az-iot-hub-device-identity-create) command. This example creates a device called **MyASAIoTDevice**.
5250
5351
```azurecli
54-
az iot hub device-identity create --hub-name "MyASAIoTHub" --device-id "MyASAIoTDevice"
52+
az iot hub device-identity create --hub-name $iotHubName --device-id "MyASAIoTDevice"
5553
```
5654
5755
3. Get the device connection string using the [az iot hub device-identity connection-string show](/cli/azure/iot/hub/device-identity/connection-string#az-iot-hub-device-identity-connection-string-show) command. Copy the entire connection string and save it for when you create the Raspberry Pi simulator.
5856
5957
```azurecli
60-
az iot hub device-identity connection-string show --hub-name "MyASAIoTHub" --device-id "MyASAIoTDevice" --output table
58+
az iot hub device-identity connection-string show --hub-name $iotHubName --device-id "MyASAIoTDevice" --output table
6159
```
6260
6361
**Output example:**
@@ -68,161 +66,114 @@ The following Azure CLI code blocks are commands that prepare the input data req
6866
6967
## Create a blob storage account
7068
71-
The following Azure CLI code blocks create a blob storage account that's used for job output. Review the sections to understand the code.
69+
The following Azure CLI commands create a blob **storage account** that's used for job **output**.
7270
7371
1. Create a general-purpose storage account with the [az storage account create](/cli/azure/storage/account) command. The general-purpose storage account can be used for all four services: blobs, files, tables, and queues.
7472
75-
Remember to replace placeholder values in angle brackets with your own values:
76-
7773
```azurecli
74+
storageAccountName="asatutorialstorage$RANDOM"
7875
az storage account create \
79-
--name <storage-account> \
76+
--name $storageAccountName \
8077
--resource-group streamanalyticsrg \
8178
--location eastus \
8279
--sku Standard_ZRS \
8380
--encryption-services blob
8481
```
8582

86-
2. Get the key for your storage account by running the [az storage account keys list](/cli/azure/storage/account/keys) command. Save this key to use in the next step.
87-
88-
```azurecli
89-
az storage account keys list -g streamanalyticsrg -n <storage-account>
90-
```
83+
2. Get the key for your storage account by running the [az storage account keys list](/cli/azure/storage/account/keys) command.
9184

92-
3. Create a container for storing blobs with the [az storage container create](/cli/azure/storage/container) command. You use the storage account key to authorize the operation to create the container. For more information about authorizing data operations with Azure CLI, see [Authorize access to blob or queue data with Azure CLI](../storage/blobs/authorize-data-operations-cli.md).
85+
```azurecli
86+
key=$(az storage account keys list -g streamanalyticsrg -n $storageAccountName --query "[0].value" -o tsv)
87+
echo $key
88+
```
89+
90+
> [!IMPORTANT]
91+
> Note down the access key for the Azure storage account. You will use this key later in this quickstart.
92+
3. Create a container named `state` for storing blobs with the [az storage container create](/cli/azure/storage/container) command. You use the storage account key to authorize the operation to create the container. For more information about authorizing data operations with Azure CLI, see [Authorize access to blob or queue data with Azure CLI](../storage/blobs/authorize-data-operations-cli.md).
9393
9494
```azurecli
9595
az storage container create \
96-
--account-name <storage-account> \
97-
--name sample-container \
98-
--account-key <key>
96+
--account-name $storageAccountName \
97+
--name state \
98+
--account-key $key \
9999
--auth-mode key
100100
```
101101

102102
## Create a Stream Analytics job
103103

104-
The following Azure CLI code blocks create a Stream Analytics job. Review the sections to understand the code
105-
106-
1. Create a Stream Analytics job with the [az stream-analytics job create](/cli/azure/stream-analytics/job#az-stream-analytics-job-create) command.
104+
Create a Stream Analytics job with the [az stream-analytics job create](/cli/azure/stream-analytics/job#az-stream-analytics-job-create) command.
107105

108106
```azurecli
109107
az stream-analytics job create \
110-
--resource-group streamanalyticsrg
111-
--name streamanalyticsjob \
112-
--location eastus \
108+
--job-name "streamanalyticsjob" \
109+
--resource-group "streamanalyticsrg" \
110+
--location "eastus" \
113111
--output-error-policy "Drop" \
114-
--events-outoforder-policy "Drop" \
115-
--events-outoforder-max-delay 5 \
116-
--events-late-arrival-max-delay 16 \
112+
--out-of-order-policy "Drop" \
113+
--order-max-delay 5 \
114+
--arrival-max-delay 16 \
117115
--data-locale "en-US"
118116
```
119117

120118
## Configure input to the job
121119

122-
Add an input to your job by using the [az stream-analytics input](/cli/azure/stream-analytics/input#az-stream-analytics-input-create) cmdlet. This cmdlet takes the job name, job input name, resource group name, and the job input definition as parameters. The job input definition is a JSON file that contains the properties required to configure the job's input. In this example, you'll create an IoT Hub as an input.
123-
124-
On your local machine, create a file named `datasource.json` and add the following JSON data to it. Make sure to replace the value for `sharedAccessPolicyKey` with the `SharedAccessKey` portion of the IoT Hub connection string you saved in a previous section.
120+
Add an input to your job by using the [az stream-analytics input](/cli/azure/stream-analytics/input#az-stream-analytics-input-create) cmdlet. This cmdlet takes the job name, job input name, resource group name, and the input properties in JSON format as parameters. In this example, you'll create an IoT Hub as an input.
125121

126-
```json
127-
{
128-
"type": "Microsoft.Devices/IotHubs",
129-
"properties": {
130-
"iotHubNamespace": "iothub",
131-
"sharedAccessPolicyName": "iothubowner",
132-
"sharedAccessPolicyKey": "sharedAccessPolicyKey=",
133-
"consumerGroupName": "sdkconsumergroup",
134-
"endpoint": "messages/events"
135-
}
136-
}
137-
```
138-
139-
On your local machine, create a file named `serialization.json` and add the following JSON data to it.
140-
141-
```json
142-
{
143-
"type": "Json",
144-
"properties": {
145-
"encoding": "UTF8"
146-
}
147-
}
148-
```
149-
150-
Next, run the `az stream-analytics input create` cmdlet. Be sure to replace the value of `datasource` variable with the path where you've stored the job input definition JSON file, and the value of `serialization` variable with the path where you've stored the serialization JSON file.
122+
> [!IMPORTANT]
123+
> - Replace `IOT HUB ACCESS KEY` with the value of Shared Access Key in the IOT Hub connection string you saved. For example, if the IOT Hub connection string is: `HostName=MyASAIoTHub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=xxxxxxxxxxxxxx=`, the Shared Access Key value is `xxxxxxxxxxxxxx=`. While replacing the value, make sure that you don't delete `\` (escape) character for `"` (double quotes).
124+
> - Update the value of `iotHubNamespace` in the following command if you used a name other than `MyASAIoTHub`. Run `echo $iotHubName` to see the name of your IoT Hub.
151125
152126
```azurecli
153127
az stream-analytics input create \
154-
--resource-group streamanalyticsrg
155-
--job-name streamanalyticsjob \
156-
--name asaiotinput \
157-
--type Stream \
158-
--datasource datasource.json \
159-
--serialization serialization.json
128+
--properties "{\"type\":\"Stream\",\"datasource\":{\"type\":\"Microsoft.Devices/IotHubs\",\"properties\":{\"consumerGroupName\":\"\$Default\",\"endpoint\":\"messages/events\",\"iotHubNamespace\":\"MyASAIoTHub\",\"sharedAccessPolicyKey\":\"IOT HUB ACCESS KEY\",\"sharedAccessPolicyName\":\"iothubowner\"}},\"serialization\":{\"type\":\"Json\",\"encoding\":\"UTF8\"}}" \
129+
--input-name "asaiotinput" \
130+
--job-name "streamanalyticsjob" \
131+
--resource-group "streamanalyticsrg"
160132
```
161133

162134
## Configure output to the job
163135

164-
Add an output to your job by using the [az stream-analytics output create](/cli/azure/stream-analytics/output#az-stream-analytics-output-create) cmdlet. This cmdlet takes the job name, job output name, resource group name, and the job output definition as parameters. The job output definition is a JSON file that contains the properties required to configure job's output. This example uses blob storage as output.
136+
Add an output to your job by using the [az stream-analytics output create](/cli/azure/stream-analytics/output#az-stream-analytics-output-create) cmdlet. This cmdlet takes the job name, job output name, resource group name, data source in JSON format, and serialization type as parameters.
165137

166-
On your local machine, create a file named `datasink.json`, and add the following JSON data to it. Make sure to replace the value for `accountKey` with your storage account's access key that is the value stored in $storageAccountKey value.
167-
168-
```json
169-
{
170-
"type": "Microsoft.Storage/Blob",
171-
"properties": {
172-
"storageAccounts": [
173-
{
174-
"accountName": "<storage-account>",
175-
"accountKey": "accountKey=="
176-
}
177-
],
178-
"container": "state",
179-
"pathPattern": "{date}/{time}",
180-
"dateFormat": "yyyy/MM/dd",
181-
"timeFormat": "HH"
182-
}
183-
}
184-
```
185-
186-
Next, run the `az stream-analytics output` cmdlet. Be sure to replace the value of `datasource` variable with the path where you've stored the job output definition JSON file, and the value of `serialization` variable with the path where you've stored the serialization JSON file.
138+
> [!IMPORTANT]
139+
> Replace `STORAGEACCOUNTNAME>` with the name of your Azure Storage account and `STORAGEACCESSKEY>` with the access key for your storage account. If you didn't note down these values, run the following commands to get them: `echo $storageAccountName` and `echo $key`. While replacing the values, make sure that you don't delete `\` (escape) character for `"` (double quotes).
187140
188141
```azurecli
189142
az stream-analytics output create \
190-
--resource-group streamanalyticsrg \
191143
--job-name streamanalyticsjob \
192-
--name asabloboutput \
193-
--datasource datasink.json \
194-
--serialization serialization.json
144+
--datasource "{\"type\":\"Microsoft.Storage/Blob\",\"properties\":{\"container\":\"state\",\"dateFormat\":\"yyyy/MM/dd\",\"pathPattern\":\"{date}/{time}\",\"storageAccounts\":[{\"accountKey\":\"STORAGEACCESSKEY\",\"accountName\":\"STORAGEACCOUNTNAME\"}],\"timeFormat\":\"HH\"}}" \
145+
--serialization "{\"type\":\"Json\",\"properties\":{\"format\":\"Array\",\"encoding\":\"UTF8\"}}" \
146+
--output-name asabloboutput \
147+
--resource-group streamanalyticsrg
195148
```
196149

197150
## Define the transformation query
198151

199-
Add a transformation your job by using the [az stream-analytics transformation create](/cli/azure/stream-analytics/transformation#az-stream-analytics-transformation-create) cmdlet. This cmdlet takes the job name, job transformation name, resource group name, and the job transformation definition as parameters.
200-
201-
Run the `az stream-analytics transformation create` cmdlet.
152+
Add a transformation your job by using the [az stream-analytics transformation create](/cli/azure/stream-analytics/transformation#az-stream-analytics-transformation-create) cmdlet.
202153

203154
```azurecli
204155
az stream-analytics transformation create \
205156
--resource-group streamanalyticsrg \
206157
--job-name streamanalyticsjob \
207158
--name Transformation \
208159
--streaming-units "6" \
209-
--transformation-query "SELECT * INTO asabloboutput FROM asaiotinput WHERE Temperature > 27"
160+
--saql "SELECT * INTO asabloboutput FROM asaiotinput WHERE Temperature > 27"
210161
```
211162
## Run the IoT simulator
212163

213164
1. Open the [Raspberry Pi Azure IoT Online Simulator](https://azure-samples.github.io/raspberry-pi-web-simulator/).
214165

215-
2. Replace the placeholder in Line 15 with the entire Azure IoT Hub Device connection string you saved in a previous section.
166+
2. Replace the placeholder in line 15 with the entire Azure IoT Hub **Device connection string** (not IoT Hub connection string) you saved at the beginning of the quickstart.
216167

217-
3. Click **Run**. The output should show the sensor data and messages that are being sent to your IoT Hub.
168+
3. Select **Run**. The output should show the sensor data and messages that are being sent to your IoT Hub.
218169

219170
![Raspberry Pi Azure IoT Online Simulator](./media/stream-analytics-quick-create-powershell/ras-pi-connection-string.png)
220171

221172
## Start the Stream Analytics job and check the output
222173

223174
Start the job by using the [az stream-analytics job start](/cli/azure/stream-analytics/job#az-stream-analytics-job-start) cmdlet. This cmdlet takes the job name, resource group name, output start mode, and start time as parameters. `OutputStartMode` accepts values of `JobStartTime`, `CustomTime`, or `LastOutputEventTime`.
224175

225-
After you run the following cmdlet, it returns `True` as output if the job starts. In the storage container, an output folder is created with the transformed data.
176+
After you run the following cmdlet, it returns `True` as output if the job starts.
226177

227178
```azurecli
228179
az stream-analytics job start \
@@ -231,9 +182,34 @@ az stream-analytics job start \
231182
--output-start-mode JobStartTime
232183
```
233184

234-
## Clean up resources
185+
Give it a few minutes and then verify that an output file is created in the `state` blob container.
186+
187+
:::image type="content" source="./media/stream-analytics-quick-create-powershell/output-file-container.png" alt-text="Screenshot showing the output file in the State blob container.":::
188+
189+
Download and open the file to see several entries similar to the following one:
190+
191+
```json
192+
{
193+
"messageId": 229,
194+
"deviceId": "Raspberry Pi Web Client",
195+
"temperature": 31.85214010589595,
196+
"humidity": 60.278830289656284,
197+
"EventProcessedUtcTime": "2023-02-28T22:06:33.5567789Z",
198+
"PartitionId": 3,
199+
"EventEnqueuedUtcTime": "2023-02-28T22:05:49.6520000Z",
200+
"IoTHub": {
201+
"MessageId": null,
202+
"CorrelationId": null,
203+
"ConnectionDeviceId": "MyASAIoTDevice",
204+
"ConnectionDeviceGenerationId": "638132150746523845",
205+
"EnqueuedTime": "2023-02-28T22:05:49.6520000Z",
206+
"StreamId": null
207+
}
208+
}
209+
```
235210

236-
When no longer needed, delete the resource group, the streaming job, and all related resources. Deleting the job avoids billing the streaming units consumed by the job. If you're planning to use the job in future, you can skip deleting it, and stop the job for now. If you aren't going to continue to use this job, delete all resources created by this quickstart by running the following cmdlet:
211+
## Clean up resources
212+
Delete the resource group, which will delete all the resources in the resource group including Stream Analytics job, IoT Hub, and Azure Storage account.
237213

238214
```azurecli
239215
az group delete \

0 commit comments

Comments
 (0)