使用模板通过事件中心创建一个命名空间并启用捕获

本文介绍如何使用 Azure 资源管理器模板创建包含一个事件中心实例的事件中心命名空间,并在事件中心启用捕获功能。 本文介绍如何定义要部署的资源以及如何定义执行部署时指定的参数。 可将此模板用于自己的部署,或自定义此模板以满足要求。

本文还介绍如何根据所选目标,指定将事件捕获到 Azure 存储 Blob 或 Azure Data Lake Store 中。

有关创建模板的详细信息,请参阅创作 Azure 资源管理器模板。 有关要在模板中使用的 JSON 语法和属性,请参阅 Microsoft.EventHub 资源类型

如需完整模板,请选择以下 GitHub 链接:

注意

若要查看最新模板,请访问 Azure 快速入门模板库并搜索事件中心。

将部署什么内容?

借助此模板,可以部署包含事件中心的事件中心命名空间并启用事件中心捕获。 通过事件中心捕获,可以在所选的指定时间或大小间隔内,将事件中心的流式处理数据自动传送到 Azure Blob 存储或 Azure Data Lake Store。 选择以下按钮以启用事件中心捕获到 Azure 存储:

Deploy to Azure

选择以下按钮以启用事件中心捕获到 Azure Data Lake Store:

Deploy to Azure

参数

使用 Azure 资源管理器,可以定义在部署模板时想要指定的值的参数。 模板包含名为 Parameters 的部分,其中包含所有参数值。 应该为这些值定义一个参数,这些值因要部署的项目或要部署到的环境而异。 不要为始终保持不变的值定义参数。 每个参数值可在模板中用来定义所部署的资源。

模板定义以下参数。

eventHubNamespaceName

要创建的事件中心命名空间的名称。

"eventHubNamespaceName":{
     "type":"string",
     "metadata":{
         "description":"Name of the EventHub namespace"
      }
}

eventHubName

在事件中心命名空间中创建的事件中心的名称。

"eventHubName":{
    "type":"string",
    "metadata":{
        "description":"Name of the event hub"
    }
}

messageRetentionInDays

事件中心内消息的保留天数。

"messageRetentionInDays":{
    "type":"int",
    "defaultValue": 1,
    "minValue":"1",
    "maxValue":"7",
    "metadata":{
       "description":"How long to retain the data in event hub"
     }
 }

partitionCount

事件中心内创建的分区数。

"partitionCount":{
    "type":"int",
    "defaultValue":2,
    "minValue":2,
    "maxValue":32,
    "metadata":{
        "description":"Number of partitions chosen"
    }
 }

captureEnabled

在事件中心启用捕获。

"captureEnabled":{
    "type":"string",
    "defaultValue":"true",
    "allowedValues": [
    "false",
    "true"],
    "metadata":{
        "description":"Enable or disable the Capture for your event hub"
    }
 }

captureEncodingFormat

指定用于序列化事件数据的编码格式。

"captureEncodingFormat":{
    "type":"string",
    "defaultValue":"Avro",
    "allowedValues":[
    "Avro"],
    "metadata":{
        "description":"The encoding format in which Capture serializes the EventData"
    }
}

captureTime

事件中心捕获开始捕获数据的时间间隔。

"captureTime":{
    "type":"int",
    "defaultValue":300,
    "minValue":60,
    "maxValue":900,
    "metadata":{
         "description":"The time window in seconds for the capture"
    }
}

captureSize

捕获开始捕获数据的大小间隔。

"captureSize":{
    "type":"int",
    "defaultValue":314572800,
    "minValue":10485760,
    "maxValue":524288000,
    "metadata":{
        "description":"The size window in bytes for capture"
    }
}

captureNameFormat

事件中心捕获用来编写 Avro 文件的名称格式。 注意,捕获名称格式必须包含 {Namespace}{EventHub}{PartitionId}{Year}{Month}{Day}{Hour}{Minute}{Second} 字段。 这些字段可以按任意顺序排列,可以带或不带分隔符。

"captureNameFormat": {
      "type": "string",
      "defaultValue": "{Namespace}/{EventHub}/{PartitionId}/{Year}/{Month}/{Day}/{Hour}/{Minute}/{Second}",
      "metadata": {
        "description": "A Capture Name Format must contain {Namespace}, {EventHub}, {PartitionId}, {Year}, {Month}, {Day}, {Hour}, {Minute} and {Second} fields. These can be arranged in any order with or without delimeters. E.g.  Prod_{EventHub}/{Namespace}\\{PartitionId}_{Year}_{Month}/{Day}/{Hour}/{Minute}/{Second}"
      }
    }

apiVersion

模板的 API 版本。

 "apiVersion":{
    "type":"string",
    "defaultValue":"2017-04-01",
    "metadata":{
        "description":"ApiVersion used by the template"
    }
 }

如果选择 Azure 存储作为目标,请使用以下参数。

destinationStorageAccountResourceId

捕获需要 Azure 存储帐户资源 ID,以启用到所需存储帐户的捕获。

 "destinationStorageAccountResourceId":{
    "type":"string",
    "metadata":{
        "description":"Your existing Storage account resource ID where you want the blobs be captured"
    }
 }

blobContainerName

用于捕获事件数据的 Blob 容器。

 "blobContainerName":{
    "type":"string",
    "metadata":{
        "description":"Your existing storage container in which you want the blobs captured"
    }
}

Azure 存储用作目标

创建包含一个事件中心的 EventHub 类型的命名空间,并启用“捕获到 Azure Blob 存储”。

"resources":[
      {
         "apiVersion":"[variables('ehVersion')]",
         "name":"[parameters('eventHubNamespaceName')]",
         "type":"Microsoft.EventHub/Namespaces",
         "location":"[variables('location')]",
         "sku":{
            "name":"Standard",
            "tier":"Standard"
         },
         "resources": [
    {
      "apiVersion": "2017-04-01",
      "name": "[parameters('eventHubNamespaceName')]",
      "type": "Microsoft.EventHub/Namespaces",
      "location": "[resourceGroup().location]",
      "sku": {
        "name": "Standard"
      },
      "properties": {
        "isAutoInflateEnabled": "true",
        "maximumThroughputUnits": "7"
      },
      "resources": [
        {
          "apiVersion": "2017-04-01",
          "name": "[parameters('eventHubName')]",
          "type": "EventHubs",
          "dependsOn": [
            "[concat('Microsoft.EventHub/namespaces/', parameters('eventHubNamespaceName'))]"
          ],
          "properties": {
            "messageRetentionInDays": "[parameters('messageRetentionInDays')]",
            "partitionCount": "[parameters('partitionCount')]",
            "captureDescription": {
              "enabled": "true",
              "skipEmptyArchives": false,
              "encoding": "[parameters('captureEncodingFormat')]",
              "intervalInSeconds": "[parameters('captureTime')]",
              "sizeLimitInBytes": "[parameters('captureSize')]",
              "destination": {
                "name": "EventHubArchive.AzureBlockBlob",
                "properties": {
                  "storageAccountResourceId": "[parameters('destinationStorageAccountResourceId')]",
                  "blobContainer": "[parameters('blobContainerName')]",
                  "archiveNameFormat": "[parameters('captureNameFormat')]"
                }
              }
            }
          }

        }
      ]
    }
  ]

运行部署的命令

要将资源部署到 Azure,必须登录到 Azure 帐户,并且必须使用 Azure 资源管理器模块。 若要了解配合使用 Azure 资源管理器和 Azure PowerShell 或 Azure CLI 的相关信息,请参阅:

以下示例假定帐户中已具有一个指定名称的资源组。

PowerShell

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

部署模板,允许事件中心捕获将事件捕获到 Azure 存储中:

New-AzResourceGroupDeployment -ResourceGroupName \<resource-group-name\> -TemplateFile https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.eventhub/eventhubs-create-namespace-and-enable-capture/azuredeploy.json

Azure CLI

Azure Blob 存储(作为目标):

az deployment group create \<my-resource-group\> \<my-deployment-name\> --template-uri [https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.eventhub/eventhubs-create-namespace-and-enable-capture/azuredeploy.json][]

后续步骤

也可通过 Azure 门户配置事件中心捕获。 有关详细信息,请参阅通过 Azure 门户启用事件中心捕获

访问以下链接可以了解有关事件中心的详细信息: