快速入門:使用 Azure 入口網站建立串流分析作業Quickstart: Create a Stream Analytics job by using the Azure portal

本快速入門會示範如何開始建立串流分析作業。This quickstart shows you how to get started with creating a Stream Analytics job. 在本快速入門中,您會定義串流分析作業,以讀取即時串流資料並篩選出溫度大於 27 的訊息。In this quickstart, you define a Stream Analytics job that reads real-time streaming data and filters messages with a temperature 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

準備輸入資料Prepare the input data

定義串流分析作業前,您應先準備輸入資料。Before defining the Stream Analytics job, you should prepare the input data. 即時感應器資料會內嵌至 IoT 中樞,然後設定為作業輸入。The real-time sensor data is ingested to IoT Hub, which later configured as the job input. 為了準備作業所需的輸入資料,請完成下列步驟:To prepare the input data required by the job, complete the following steps:

  1. 登入 Azure 入口網站Sign in to the Azure portal.

  2. 選取 [建立資源] > [物聯網] > [IoT 中樞] 。Select Create a resource > Internet of Things > IoT Hub.

  3. 在 [IoT 中樞] 窗格中,輸入下列資訊︰In the IoT Hub pane, enter the following information:

    設定Setting 建議的值Suggested value 說明Description
    訂用帳戶Subscription <Your subscription> 選取您要使用的 Azure 訂用帳戶。Select the Azure subscription that you want to use.
    資源群組Resource group asaquickstart-resourcegroupasaquickstart-resourcegroup 選取 [新建] ,然後為您的帳戶輸入新的資源群組名稱。Select Create New and enter a new resource-group name for your account.
    區域Region <Select the region that is closest to your users> 選取您可以在其中裝載 IoT 中樞的地理位置。Select a geographic location where you can host your IoT Hub. 使用最靠近您的使用者的位置。Use the location that's closest to your users.
    IoT 中樞名稱IoT Hub Name MyASAIoTHubMyASAIoTHub 選取您的 IoT 中樞名稱。Select a name for your IoT Hub.

    建立 IoT 中樞

  4. 完成時,選取 [下一步: 設定大小與級別]。Select Next: Set size and scale.

  5. 選擇您的 定價與級別層Choose your Pricing and scale tier. 在本快速入門中,選取 [F1-免費] 層 (如果仍可用於您的訂用帳戶)。For this quickstart, select the F1 - Free tier if it's still available on your subscription. 如需詳細資訊,請參閱 IoT 中樞定價For more information, see IoT Hub pricing.

    調整 IoT 中樞的大小與級別

  6. 選取 [檢閱 + 建立] 。Select Review + create. 檢閱您的 IoT 中樞資訊,然後按一下 [建立] 。Review your IoT Hub information and click Create. 建立 IoT 中樞可能需要數分鐘。Your IoT Hub might take a few minutes to create. 您可以在 [通知] 窗格中監視進度。You can monitor the progress in the Notifications pane.

  7. 在您的 IoT 中樞導覽功能表中,按一下 [IoT 裝置] 之下的 [新增] 。In your IoT Hub navigation menu, click Add under IoT devices. 新增 [裝置識別碼] ,然後按一下 [儲存] 。Add a Device ID and click Save.

    將裝置新增至 IoT 中樞

  8. 建立裝置之後,請從 [IoT 裝置] 清單開啟裝置。Once the device is created, open the device from the IoT devices list. 複製 [連接字串 -- 主索引鍵] ,並將它儲存到記事本以供日後使用。Copy the Connection string -- primary key and save it to a notepad to use later.

    複製 IoT 中樞裝置連接字串

建立 Blob 儲存體Create blob storage

  1. 從 Azure 入口網站的左上角,選取 [建立資源] > [儲存體] > [儲存體帳戶] 。From the upper left-hand corner of the Azure portal, select Create a resource > Storage > Storage account.

  2. 在 [建立儲存體帳戶] 窗格中,輸入儲存體帳戶名稱、位置和資源。In the Create storage account pane, enter a storage account name, location, and resource group. 選擇相同的位置和資源群組作為您建立的 IoT 中樞。Choose the same location and resource group as the IoT Hub you created. 然後按一下 [檢閱 + 建立] 以建立帳戶。Then click Review + create to create the account.

    建立儲存體帳戶

  3. 建立儲存體帳戶後,選取 [概觀] 窗格上的 [Blob] 圖格。Once your storage account is created, select the Blobs tile on the Overview pane.

    儲存體帳戶概觀

  4. 在 [Blob 服務] 頁面中選取 [容器] ,然後為容器提供名稱 (例如 container1 )。From the Blob Service page, select Container and provide a name for your container, such as container1. 將 [公用存取層級] 保留為 [私人 (沒有匿名存取)] ,然後選取 [確定] 。Leave the Public access level as Private (no anonymous access) and select OK.

    建立 Blob 容器

建立串流分析作業Create a Stream Analytics job

  1. 登入 Azure 入口網站。Sign in to the Azure portal.

  2. 選取 Azure 入口網站左上角的 [建立資源] 。Select Create a resource in the upper left-hand corner of the Azure portal.

  3. 從結果清單中選取 [分析] > [串流分析作業]。Select Analytics > Stream Analytics job from the results list.

  4. 填寫串流分析作業頁面,並提供下列資訊:Fill out the Stream Analytics job page with the following information:

    設定Setting 建議的值Suggested value 說明Description
    作業名稱Job name MyASAJobMyASAJob 輸入用來識別您串流分析作業的名稱。Enter a name to identify your Stream Analytics job. 串流分析作業名稱只可包含英數字元、連字號與底線,且其長度必須介於 3 到 63 個字元之間。Stream Analytics job name can contain alphanumeric characters, hyphens, and underscores only and it must be between 3 and 63 characters long.
    訂用帳戶Subscription <Your subscription> 選取您要用於此作業的 Azure 訂用帳戶。Select the Azure subscription that you want to use for this job.
    資源群組Resource group asaquickstart-resourcegroupasaquickstart-resourcegroup 選取與您的 IoT 中樞相同的資源群組。Select the same resource group as your IoT Hub.
    位置Location <Select the region that is closest to your users> 選取您可以在其中裝載串流分析作業的地理位置。Select geographic location where you can host your Stream Analytics job. 使用最接近使用者的區域以提升效能並減少資料轉送成本。Use the location that's closest to your users for better performance and to reduce the data transfer cost.
    串流單位Streaming units 11 串流單位代表執行作業所需的計算資源。Streaming units represent the computing resources that are required to execute a job. 根據預設,此值設定為 1。By default, this value is set to 1. 若要深入了解如何調整串流單位,請參閱了解與調整串流單位一文。To learn about scaling streaming units, refer to understanding and adjusting streaming units article.
    裝載環境Hosting environment CloudCloud 串流分析作業可以部署到雲端或邊緣裝置。Stream Analytics jobs can be deployed to cloud or edge. 雲端部分可讓您部署到 Azure 雲端,邊緣裝置部分可讓您部署到 IoT Edge 裝置。Cloud allows you to deploy to Azure Cloud, and Edge allows you to deploy to an IoT Edge device.

    建立作業

  5. 核取 [釘選至儀表板] 方塊,以將作業放在您的儀表板上,然後選取 [建立]。Check the Pin to dashboard box to place your job on your dashboard and then select Create.

  6. 您應會看到「部署進行中...」通知顯示在瀏覽器視窗的右上方。You should see a Deployment in progress... notification displayed in the top right of your browser window.

設定作業輸入Configure job input

在本節中,您會將 IoT 中樞裝置輸入設定為串流分析作業。In this section, you will configure an IoT Hub device input to the Stream Analytics job. 使用在本快速入門的上一節中建立的 IoT 中樞。Use the IoT Hub you created in the previous section of the quickstart.

  1. 瀏覽至您的串流分析作業。Navigate to your Stream Analytics job.

  2. 選取 [輸入] > [新增串流輸入] > [IoT 中樞]。Select Inputs > Add Stream input > IoT Hub.

  3. 使用下列值填寫 [IoT 中樞] 頁面:Fill out the IoT Hub page with the following values:

    設定Setting 建議的值Suggested value 說明Description
    輸入別名Input alias IoTHubInputIoTHubInput 輸入名稱以識別作業的輸入。Enter a name to identify the job’s input.
    訂用帳戶Subscription <Your subscription> 選取您在其中建立儲存體帳戶的 Azure 訂用帳戶。Select the Azure subscription that has the storage account you created. 儲存體帳戶可以位在相同或不同的訂用帳戶中。The storage account can be in the same or in a different subscription. 此範例假設您已在相同的訂用帳戶中建立儲存體帳戶。This example assumes that you have created storage account in the same subscription.
    IoT 中樞IoT Hub MyASAIoTHubMyASAIoTHub 輸入在上一節中建立的 IoT 中樞名稱。Enter the name of the IoT Hub you created in the previous section.
  4. 其他選項保留為預設值,然後選取 [儲存] 以儲存設定。Leave other options to default values and select Save to save the settings.

    設定輸入資料

設定作業輸出Configure job output

  1. 瀏覽至您先前建立的串流分析作業。Navigate to the Stream Analytics job that you created earlier.

  2. 選取 [輸出] > [新增] > [Blob 儲存體]。Select Outputs > Add > Blob storage.

  3. 使用下列值填寫 [Blob 儲存體] 頁面:Fill out the Blob storage page with the following values:

    設定Setting 建議的值Suggested value 說明Description
    輸出別名Output alias BlobOutputBlobOutput 輸入名稱以識別作業的輸出。Enter a name to identify the job’s output.
    訂用帳戶Subscription <Your subscription> 選取您在其中建立儲存體帳戶的 Azure 訂用帳戶。Select the Azure subscription that has the storage account you created. 儲存體帳戶可以位在相同或不同的訂用帳戶中。The storage account can be in the same or in a different subscription. 此範例假設您已在相同的訂用帳戶中建立儲存體帳戶。This example assumes that you have created storage account in the same subscription.
    儲存體帳戶Storage account asaquickstartstorageasaquickstartstorage 選擇或輸入儲存體帳戶的名稱。Choose or enter the name of the storage account. 系統會自動偵測建立在相同訂用帳戶中的儲存體帳戶名稱。Storage account names are automatically detected if they are created in the same subscription.
    容器Container container1container1 選取您在儲存體帳戶中建立的現有容器。Select the existing container that you created in your storage account.
  4. 其他選項保留為預設值,然後選取 [儲存] 以儲存設定。Leave other options to default values and select Save to save the settings.

    設定輸出

定義轉換查詢Define the transformation query

  1. 瀏覽至您先前建立的串流分析作業。Navigate to the Stream Analytics job that you created earlier.

  2. 選取 [查詢]並更新查詢,如下所示:Select Query and update the query as follows:

    SELECT *
    INTO BlobOutput
    FROM IoTHubInput
    HAVING Temperature > 27
    
  3. 在此範例中,查詢會從 IoT 中樞讀取資料,並將資料複製到 Blob 中的新檔案。In this example, the query reads the data from IoT Hub and copies it to a new file in the blob. 選取 [儲存]。Select Save.

    設定工作轉換

執行 IoT 模擬器Run the IoT simulator

  1. 開啟 Raspberry Pi Azure IoT 線上模擬器Open the Raspberry Pi Azure IoT Online Simulator.

  2. 以您在上一節中儲存的 Azure IoT 中樞裝置連接字串取代行 15 中的預留位置。Replace the placeholder in Line 15 with the 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

  1. 回到作業概觀頁面,然後選取 [啟動]。Return to the job overview page and select Start.

  2. 在 [啟動作業] 之下,針對 [作業輸出開始時間] 欄位,選取 [現在]。Under Start job, select Now, for the Job output start time field. 然後,選取 [啟動] 以啟動作業。Then, select Start to start your job.

  3. 幾分鐘後,在入口網站中尋找您設定為作業輸出的儲存體帳戶和容器。After few minutes, in the portal, find the storage account & the container that you have configured as output for the job. 您現在可以在容器中看到輸出檔。You can now see the output file in the container. 第一次啟動作業需要幾分鐘的時間,作業一旦啟動後,即會在資料送達時繼續執行。The job takes a few minutes to start for the first time, after it is started, it will continue to run as the data arrives.

    已轉換的輸出

清除資源Clean up resources

若不再需要,請刪除資源群組、串流分析作業和所有相關資源。When no longer needed, delete the resource group, the Stream Analytics 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 stop it and restart it later when you need. 如果您將不繼續使用此作業,請使用下列步驟,刪除本快速入門所建立的所有資源:If you are not going to continue to use this job, delete all resources created by this quickstart by using the following steps:

  1. 從 Azure 入口網站的左側功能表中,選取 [資源群組] ,然後選取您所建立資源的名稱。From the left-hand menu in the Azure portal, select Resource groups and then select the name of the resource you created.

  2. 在資源群組頁面上,選取 [刪除] ,在文字方塊中輸入要刪除的資源名稱,然後選取 [刪除] 。On your resource group page, select Delete, type the name of the resource to delete in the text box, and then select Delete.

後續步驟Next steps

在本快速入門中,您已使用 Azure 入口網站部署了簡單的串流分析作業。In this quickstart, you deployed a simple Stream Analytics job using Azure portal. 您也可以使用 PowerShellVisual StudioVisual Studio Code 部署串流分析作業。You can also deploy Stream Analytics jobs using PowerShell, Visual Studio, and Visual Studio Code.

若要了解如何設定其他輸入來源及執行即時偵測,請前往下列文章:To learn about configuring other input sources and performing real-time detection, continue to the following article: