快速入门:使用 Azure CLI 创建 Azure 流分析作业Quickstart: Create an Azure Stream Analytics job using the Azure CLI

在本快速入门中,将使用 Azure CLI 定义一个流分析作业,以便筛选温度读数高于 27 的实时传感器消息。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. 流分析作业会从 IoT 中心读取数据,对数据进行转换,然后将数据写回到 Blob 存储中的容器。Your Stream Analytics job will read data from IoT Hub, transform the data, and write the data back to a container in blob storage. 在本快速入门中使用的输入数据由 Raspberry Pi 联机模拟器生成。The input data used in this quickstart is generated by a Raspberry Pi online simulator.

开始之前Before you begin

  • 如果没有 Azure 订阅,请创建一个试用帐户If you don't have an Azure subscription, create a Trial.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

准备环境Prepare your environment

  1. 登录。Sign in.

    如果使用的是 CLI 的本地安装,请使用 az login 命令登录。Sign in using the az login command if you're using a local install of the CLI.

    az login
    

    遵循终端中显示的步骤完成身份验证过程。Follow the steps displayed in your terminal to complete the authentication process.

  2. 安装 Azure CLI 扩展。Install the Azure CLI extension.

    使用 Azure CLI 的扩展引用时,必须先安装该扩展。When working with extension references for the Azure CLI, you must first install the extension. 借助 Azure CLI 扩展,可访问尚未在核心 CLI 中提供的试验性和预发布的命令。Azure CLI extensions give you access to experimental and pre-release commands that have not yet shipped as part of the core CLI. 若要详细了解包含更新和卸载的扩展,请参阅使用 Azure CLI 的扩展To learn more about extensions including updating and uninstalling, see Use extensions with Azure CLI.

    运行以下命令,安装流分析扩展Install the extension for Stream Analytics by running the following command:

    az extension add --name stream-analytics
    

    运行以下命令,安装 Azure IoT 扩展Install the extension for Azure IoT by running the following command:

    az extension add --name azure-iot
    
  3. 创建资源组。Create a resource group.

    必须将所有 Azure 资源部署到资源组。All Azure resources must be deployed into a resource group. 使用资源组可以组织和管理相关的 Azure 资源。Resource groups allow you to organize and manage related Azure resources.

    对于本快速入门,请使用以下 az group create 命令在 chinanorth 位置创建名为 streamanalyticsrg 的资源组 :For this quickstart, create a resource group named streamanalyticsrg in the chinanorth location with the following az group create command:

    az group create --name streamanalyticsrg --location chinanorth
    

对输入数据进行准备Prepare the input data

在定义流分析作业之前,请准备用于作业输入的数据。Before you define the Stream Analytics job, prepare the data that's used for the job's input.

以下 Azure CLI 代码块是准备作业所需的输入数据的命令。The following Azure CLI code blocks are commands that prepare the input data required by the job. 查看介绍代码的部分。Review the sections to understand the code.

  1. 使用 az iot hub create 命令创建 IoT 中心。Create an IoT Hub using the az iot hub create command. 此示例创建名为 MyASAIoTHub 的 IoT 中心。This example creates an IoT Hub called MyASAIoTHub. 由于 IoT 中心名称是唯一的,因此需使用你自己的 IoT 中心名称。Because IoT Hub names are unique, you need to come up with your own IoT Hub name. 将 SKU 设置为 F1 即可使用免费层,前提是它在订阅中可用。Set the SKU to F1 to use the free tier if it is available with your subscription. 否则,请选择下一个最低的层。If not, choose the next lowest tier.

    az iot hub create --name "MyASAIoTHub" --resource-group streamanalyticsrg --sku S1
    

    创建 IoT 中心以后,请使用 az iot hub show-connection-string 命令获取 IoT 中心连接字符串。Once the IoT hub has been created, get the IoT Hub connection string using the az iot hub show-connection-string command. 复制整个连接字符串并将其保存。这样,在将 IoT 中心作为输入添加到流分析作业时,就可以使用该字符串。Copy the entire connection string and save it for when you add the IoT Hub as input to your Stream Analytics job.

    az iot hub show-connection-string --hub-name "MyASAIoTHub"
    
  2. 使用 az iothub device-identity create 命令将设备添加到 IoT 中心。Add a device to IoT Hub using the az iothub device-identity create command. 此示例创建名为 MyASAIoTDevice 的设备。This example creates a device called MyASAIoTDevice.

    az iot hub device-identity create --hub-name "MyASAIoTHub" --device-id "MyASAIoTDevice"
    
  3. 使用 az iot hub device-identity show-connection-string 命令获取设备连接字符串。Get the device connection string using the az iot hub device-identity show-connection-string command. 复制整个连接字符串并将其保存。这样,在创建 Raspberry Pi 模拟器时,就可以使用该字符串。Copy the entire connection string and save it for when you create the Raspberry Pi simulator.

    az iot hub device-identity show-connection-string --hub-name "MyASAIoTHub" --device-id "MyASAIoTDevice" --output table
    

    输出示例:Output example:

    HostName=MyASAIoTHub.azure-devices.net;DeviceId=MyASAIoTDevice;SharedAccessKey=a2mnUsg52+NIgYudxYYUNXI67r0JmNubmfVafojG8=
    

创建 Blob 存储帐户Create a blob storage account

以下 Azure CLI 代码块创建用于作业输出的 blob 存储帐户。The following Azure CLI code blocks create a blob storage account that's used for job output. 查看介绍代码的部分。Review the sections to understand the code.

  1. 使用 az storage account create 命令创建常规用途存储帐户。Create a general-purpose storage account with the az storage account create command. 常规用途的存储帐户可用于以下四种服务:Blob、文件、表和队列。The general-purpose storage account can be used for all four services: blobs, files, tables, and queues.

    请务必将尖括号中的占位符值替换为你自己的值:Remember to replace placeholder values in angle brackets with your own values:

    az storage account create \
        --name <storage-account> \
        --resource-group streamanalyticsrg \
        --location chinanorth \
        --sku Standard_LRS \
        --encryption-services blob
    
  1. 通过运行 az storage account keys list 命令获取存储帐户的密钥。Get the key for your storage account by running the az storage account keys list command. 保存此密钥以便在下一步中使用。Save this key to use in the next step.

    az storage account keys list -g streamanalyticsrg -n <storage-account>
    
  2. 可以使用 az storage container create 命令创建用于存储 blob 的容器。Create a container for storing blobs with the az storage container create command. 使用存储帐户密钥来授权操作创建容器。You use the storage account key to authorize the operation to create the container. 有关使用 Azure CLI 授权数据操作的详细信息,请参阅使用 Azure CLI 授权访问 blob 或队列数据For more information about authorizing data operations with Azure CLI, see Authorize access to blob or queue data with Azure CLI.

    az storage container create \
        --account-name <storage-account> \
        --name sample-container \
        --account-key <key>
        --auth-mode key
    

创建流分析作业Create a Stream Analytics job

以下 Azure CLI 代码块创建流分析作业。The following Azure CLI code blocks create a Stream Analytics job. 查看介绍代码的部分Review the sections to understand the code

  1. 使用 az stream-analytics job create 命令创建流分析作业。Create a Stream Analytics job with the az stream-analytics job create command.
az stream-analytics job create \
    --resource-group streamanalyticsrg 
    --name streamanalyticsjob \
    --location chinanorth \
    --output-error-policy "Drop" \
    --events-outoforder-policy "Drop" \
    --events-outoforder-max-delay 5 \
    --events-late-arrival-max-delay 16 \     
    --data-locale "en-US"

配置作业输入Configure input to the job

使用 az stream-analytics input cmdlet 将输入添加到作业。Add an input to your job by using the az stream-analytics input cmdlet. 此 cmdlet 使用作业名称、作业输入名称、资源组名称和作业输入定义作为参数。This cmdlet takes the job name, job input name, resource group name, and the job input definition as parameters. 作业输入定义是一个 JSON 文件,其中包含配置作业的输入所需的属性。The job input definition is a JSON file that contains the properties required to configure the job’s input. 在此示例中,需将 IoT 中心创建为输入。In this example, you'll create an IoT Hub as an input.

在本地计算机上创建名为 datasource.json 的文件,并向其添加以下 JSON 数据。On your local machine, create a file named datasource.json and add the following JSON data to it. 确保将 sharedAccessPolicyKey 的值替换为在上一部分保存的 IoT 中心设备连接字符串的 SharedAccessKey 部分。Make sure to replace the value for sharedAccessPolicyKey with the SharedAccessKey portion of the IoT Hub connection string you saved in a previous section.

{
    "type": "Microsoft.Devices/IotHubs",
    "properties": {
        "iotHubNamespace": "iothub",
        "sharedAccessPolicyName": "iothubowner",
        "sharedAccessPolicyKey": "sharedAccessPolicyKey=",
        "consumerGroupName": "sdkconsumergroup",
        "endpoint": "messages/events"
    }
}

在本地计算机上创建名为 serialization.json 的文件,并向其添加以下 JSON 数据。On your local machine, create a file named serialization.json and add the following JSON data to it.

{
     "type": "Json",
     "properties": {
         "encoding": "UTF8"
     }
}

接下来运行 az stream-analytics input create cmdlet。Next, run the az stream-analytics input create cmdlet. 确保用存储作业输入定义 JSON 文件的路径替换 datasource 变量的值,用存储序列化 JSON 文件的路径替换 serialization 变量的值。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.

az stream-analytics input create 
    --resource-group streamanalyticsrg 
    --job-name streamanalyticsjob \
    --name asaiotinput \
    --type Stream \
    --datasource datasource.json \
    --serialization serialization.json

配置作业输出Configure output to the job

使用 az stream-analytics output create cmdlet 将输出添加到作业。Add an output to your job by using the az stream-analytics output create cmdlet. 此 cmdlet 使用作业名称、作业输出名称、资源组名称和作业输出定义作为参数。This cmdlet takes the job name, job output name, resource group name, and the job output definition as parameters. 作业输出定义是一个 JSON 文件,其中包含配置作业的输出所需的属性。The job output definition is a JSON file that contains the properties required to configure job’s output. 此示例使用 Blob 存储作为输出。This example uses blob storage as output.

在本地计算机上创建名为 datasink.json 的文件,并向其添加以下 JSON 数据。On your local machine, create a file named datasink.json, and add the following JSON data to it. 确保将 accountKey 的值替换为存储帐户的访问密钥,该密钥是存储在 $storageAccountKey 值中的值。Make sure to replace the value for accountKey with your storage account's access key that is the value stored in $storageAccountKey value.

{
    "type": "Microsoft.Storage/Blob",
    "properties": {
        "storageAccounts": [
            {
                "accountName": "<storage-account>",
                "accountKey": "accountKey=="
            }
        ],
        "container": "state",
        "pathPattern": "{date}/{time}",
        "dateFormat": "yyyy/MM/dd",
        "timeFormat": "HH"
    }
}

接下来运行 az stream-analytics output cmdlet。Next, run the az stream-analytics output cmdlet. 确保用存储作业输出定义 JSON 文件的路径替换 datasource 变量的值,用存储序列化 JSON 文件的路径替换 serialization 变量的值。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.

az stream-analytics output create 
    --resource-group streamanalyticsrg \
    --job-name streamanalyticsjob \
    --name asabloboutput \
    --datasource datasink.json \
    --serialization serialization.json

定义转换查询Define the transformation query

使用 az stream-analytics transformation create cmdlet 将转换添加到作业。Add a transformation your job by using the az stream-analytics transformation create cmdlet. 此 cmdlet 使用作业名称、作业转换名称、资源组名称和作业转换定义作为参数。This cmdlet takes the job name, job transformation name, resource group name, and the job transformation definition as parameters.

运行 az stream-analytics transformation create cmdlet。Run the az stream-analytics transformation create cmdlet.

az stream-analytics transformation create 
    --resource-group streamanalyticsrg \
    --job-name streamanalyticsjob \
    --name Transformation \
    --streaming-units "6" \
    --transformation-query "SELECT * INTO asabloboutput FROM asaiotinput HAVING Temperature > 27"

运行 IoT 模拟器Run the IoT simulator

  1. 打开 Raspberry Pi Azure IoT 联机模拟器Open the Raspberry Pi Azure IoT Online Simulator.

  2. 将第 15 行的占位符替换为在上一部分保存的整个 Azure IoT 中心设备连接字符串。Replace the placeholder in Line 15 with the entire Azure IoT Hub Device connection string you saved in a previous section.

  3. 单击 “运行”Click Run. 输出会显示传感器数据和发送到 IoT 中心的消息。The output should show the sensor data and messages that are being sent to your IoT Hub.

    Raspberry Pi Azure IoT 联机模拟器

启动流分析作业并检查输出Start the Stream Analytics job and check the output

使用 az stream-analytics job start cmdlet 启动作业。Start the job by using the az stream-analytics job start cmdlet. 此 cmdlet 使用作业名称、资源组名称、输出启动模式和启动时间作为参数。This cmdlet takes the job name, resource group name, output start mode, and start time as parameters. OutputStartMode 接受的值为 JobStartTimeCustomTimeLastOutputEventTimeOutputStartMode accepts values of JobStartTime, CustomTime, or LastOutputEventTime.

以下 cmdlet 在运行以后会返回 True 作为输出(如果作业启动)。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.

az stream-analytics job start 
    --resource-group streamanalyticsrg \
    --name streamanalyticsjob \
    --output-start-mode JobStartTime

清理资源Clean up resources

若不再需要资源组、流式处理作业以及所有相关资源,请将其删除。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. 如果不打算继续使用该作业,请运行以下 cmdlet,删除本快速入门创建的所有资源:If you aren't going to continue to use this job, delete all resources created by this quickstart by running the following cmdlet:

az group delete \
    --name streamanalyticsrg \
    --no-wait

后续步骤Next steps

在本快速入门中,你使用 Azure CLI 部署了一个简单的流分析作业。In this quickstart, you deployed a simple Stream Analytics job using Azure CLI. 你还可以使用 Azure 门户部署流分析作业。You can also deploy Stream Analytics jobs using the Azure portal.

若要了解如何配置其他输入源并执行实时检测,请继续阅读以下文章:To learn about configuring other input sources and performing real-time detection, continue to the following article: