使用事件中心向 Azure 时序见解 Gen1 环境发送事件Send events to an Azure Time Series Insights Gen1 environment by using an event hub

本文介绍如何在 Azure 事件中心中创建和配置事件中心。This article explains how to create and configure an event hub in Azure Event Hubs. 其中还介绍了如何运行示例应用程序将事件从事件中心推送到 Azure 时序见解。It also describes how to run a sample application to push events to Azure Time Series Insights from Event Hubs. 如果你已经有了一个事件中心,其中的事件采用 JSON 格式,则可跳过本教程,在 Azure 时序见解中查看你的环境。If you have an existing event hub with events in JSON format, skip this tutorial and view your environment in Azure Time Series Insights.

配置事件中心Configure an event hub

  1. 若要了解如何创建事件中心,请阅读事件中心文档To learn how to create an event hub, read the Event Hubs documentation.

  2. 在搜索框中,搜索“事件中心”。In the search box, search for Event Hubs. 在返回的列表中选择“事件中心”。In the returned list, select Event Hubs.

  3. 选择事件中心。Select your event hub.

  4. 在创建事件中心时,实际上要创建事件中心命名空间。When you create an event hub, you're creating an event hub namespace. 如果尚未在命名空间中创建事件中心,请在菜单中的“实体”下创建事件中心。If you haven't yet created an event hub within the namespace, on the menu, under Entities, create an event hub.

    事件中心列表List of event hubs

  5. 创建事件中心后,请在事件中心列表中选择它。After you create an event hub, select it in the list of event hubs.

  6. 在菜单中的“实体”下,选择“事件中心” 。On the menu, under Entities, select Event Hubs.

  7. 选择事件中心的名称对其进行配置。Select the name of the event hub to configure it.

  8. 在“概述”下选择“使用者组”,然后选择“使用者组” 。Under Overview, select Consumer groups, and then select Consumer Group.

    创建使用者组Create a consumer group

  9. 请确保创建一个专门由 Azure 时序见解事件源使用的使用者组。Make sure you create a consumer group that's used exclusively by your Azure Time Series Insights event source.

    重要

    请确保该使用者组没有被任何其他服务(例如 Azure 流分析作业或另一个 Azure 时序见解环境)使用。Make sure this consumer group isn't used by any other service, such as an Azure Stream Analytics job or another Azure Time Series Insights environment. 如果使用者组由其他服务使用,则此环境和其他服务的读取操作会受到负面影响。If the consumer group is used by the other services, read operations are negatively affected both for this environment and for other services. 如果使用 $Default 作为使用者组,则其他读者可能会重复使用使用者组。If you use $Default as the consumer group, other readers might potentially reuse your consumer group.

  10. 在菜单中的“设置”下,选择“共享访问策略”,然后选择“添加” 。On the menu, under Settings, select Shared access policies, and then select Add.

    选择“共享访问策略”,然后选择“添加”按钮Select Shared access policies, and then select the Add button

  11. 在“添加新的共享访问策略”窗格中,创建名为“MySendPolicy”的共享访问 。In the Add new shared access policy pane, create a shared access named MySendPolicy. 将使用此共享访问策略在本文后面的 C# 示例中发送事件。You use this shared access policy to send events in the C# examples later in this article.

    在“策略名称”框中输入 MySendPolicyIn the Policy name box, enter MySendPolicy

  12. 在“声明”下选择“发送”复选框 。Under Claim, select the Send check box.

添加 Azure 时序见解实例Add an Azure Time Series Insights instance

在 Azure 时序见解 Gen2 中,可以使用时序模型 (TSM) 将上下文数据添加到传入遥测中。In Azure Time Series Insights Gen2, you can add contextual data to incoming telemetry using the Time Series Model (TSM). 在 TSM 中,标记或信号被称为“实例”,你可以将上下文数据存储在“实例字段”中 。In TSM, your tags or signals are referred to as instances, and you can store contextual data in instance fields. 使用时间序列 ID 在查询时加入数据。The data is joined at query time by using a Time Series ID. 在本文后面使用的示例 windmills 项目的“时间序列 ID”是 idThe Time Series ID for the sample windmills project that we use later in this article is id. 若要详细了解如何在实例字段中存储数据,请参阅时序模型概述。To learn more about storing data in instance fields read the Time Series Model overview.

创建 Azure 时序见解事件源Create an Azure Time Series Insights event source

  1. 如果尚未创建事件源,请完成步骤以创建事件源If you haven't created an event source, complete the steps to create an event source.

  2. timeSeriesId 设置一个值。Set a value for timeSeriesId. 若要详细了解时序 ID,请阅读时序模型To learn more about Time Series ID, read Time Series Models.

将事件推送到 windmills 的示例Push events to windmills sample

  1. 在搜索栏中搜索“事件中心”。In the search bar, search for Event Hubs. 在返回的列表中选择“事件中心”。In the returned list, select Event Hubs.

  2. 选择事件中心实例。Select your event hub instance.

  3. 转到“共享访问策略” > “MySendPolicy”。Go to Shared Access Policies > MySendPolicy. 复制“连接字符串 - 主密钥”的值。Copy the value for Connection string-primary key.

    复制主密钥连接字符串的值Copy the value for the primary key connection string

  4. 转到 https://tsiclientsample.azurewebsites.net/windFarmGen.htmlGo to https://tsiclientsample.azurewebsites.net/windFarmGen.html. 该 URL 创建并运行风车模拟设备。The URL creates and runs simulated windmill devices.

  5. 在网页上的“事件中心连接字符串”框中,粘贴在 windmill 输入字段中复制的连接字符串。In the Event Hub Connection String box on the webpage, paste the connection string that you copied in the windmill input field.

    将主密钥连接字符串粘贴到“事件中心连接字符串”框中Paste the primary key connection string in the Event Hub Connection String box

  6. 选择“单击可启动”。Select Click to start.

    提示

    风车模拟器还会创建可以作为有效负载与 Azure 时序见解 GA 查询 API 配合使用的 JSON。The windmill simulator also creates JSON you can use as a payload with the Azure Time Series Insights GA Query APIs.

    备注

    模拟器将继续发送数据,直到浏览器选项卡关闭。The simulator will continue to send data until the browser tab is closed.

  7. 返回到 Azure 门户中的事件中心。Go back to your event hub in the Azure portal. 在“概述”页上,显示事件中心收到的新事件。On the Overview page, the new events received by the event hub are displayed.

    显示事件中心指标的事件中心“概述”页An event hub Overview page that shows metrics for the event hub

支持的 JSON 形状Supported JSON shapes

示例一Example one

  • 输入:一个简单的 JSON 对象。Input: A simple JSON object.

    {
        "id":"device1",
        "timestamp":"2016-01-08T01:08:00Z"
    }
    
  • 输出:一个事件。Output: One event.

    idid timestamptimestamp
    device1device1 2016-01-08T01:08:00Z2016-01-08T01:08:00Z

示例二Example two

  • 输入:包含两个 JSON 对象的 JSON 数组。Input: A JSON array with two JSON objects. 每个 JSON 对象都转换为事件。Each JSON object is converted to an event.

    [
        {
            "id":"device1",
            "timestamp":"2016-01-08T01:08:00Z"
        },
        {
            "id":"device2",
            "timestamp":"2016-01-17T01:17:00Z"
        }
    ]
    
  • 输出:两个事件。Output: Two events.

    idid timestamptimestamp
    device1device1 2016-01-08T01:08:00Z2016-01-08T01:08:00Z
    device2device2 2016-01-08T01:17:00Z2016-01-08T01:17:00Z

示例三Example three

  • 输入:具有嵌套 JSON 数组(其中包含两个 JSON 对象)的 JSON 对象。Input: A JSON object with a nested JSON array that contains two JSON objects.

    {
        "location":"chinaeast2",
        "events":[
            {
                "id":"device1",
                "timestamp":"2016-01-08T01:08:00Z"
            },
            {
                "id":"device2",
                "timestamp":"2016-01-17T01:17:00Z"
            }
        ]
    }
    
  • 输出:两个事件。Output: Two events. “location”属性复制到每个事件。The property location is copied over to each event.

    locationlocation events.idevents.id events.timestampevents.timestamp
    chinaeast2chinaeast2 device1device1 2016-01-08T01:08:00Z2016-01-08T01:08:00Z
    chinaeast2chinaeast2 device2device2 2016-01-08T01:17:00Z2016-01-08T01:17:00Z

示例四Example four

  • 输入:具有嵌套 JSON 数组(其中包含两个 JSON 对象)的 JSON 对象。Input: A JSON object with a nested JSON array that contains two JSON objects. 此输入表明复杂 JSON 对象可以表示全局属性。This input demonstrates that global properties can be represented by the complex JSON object.

    {
        "location":"chinaeast2",
        "manufacturer":{
            "name":"manufacturer1",
            "location":"chinaeast2"
        },
        "events":[
            {
                "id":"device1",
                "timestamp":"2016-01-08T01:08:00Z",
                "data":{
                    "type":"pressure",
                    "units":"psi",
                    "value":108.09
                }
            },
            {
                "id":"device2",
                "timestamp":"2016-01-17T01:17:00Z",
                "data":{
                    "type":"vibration",
                    "units":"abs G",
                    "value":217.09
                }
            }
        ]
    }
    
  • 输出:两个事件。Output: Two events.

    locationlocation manufacturer.namemanufacturer.name manufacturer.locationmanufacturer.location events.idevents.id events.timestampevents.timestamp events.data.typeevents.data.type events.data.unitsevents.data.units events.data.valueevents.data.value
    chinaeast2chinaeast2 manufacturer1manufacturer1 chinaeast2chinaeast2 device1device1 2016-01-08T01:08:00Z2016-01-08T01:08:00Z 压力pressure psipsi 108.09108.09
    chinaeast2chinaeast2 manufacturer1manufacturer1 chinaeast2chinaeast2 device2device2 2016-01-08T01:17:00Z2016-01-08T01:17:00Z 振动vibration abs Gabs G 217.09217.09

后续步骤Next steps