Skip to content

Deep Dive: Event Hubs – Part 2: Creation of Event Hubs

Last updated on January 19, 2017

This is the second post in “Deep Dive: Event Hubs” Series. Please click on this link below if you like to visit the first post

Introduction to Event Hubs

In this post, we shall see the about creation of the event hubs. There are multiple ways to create event hubs:

  • Using the management portal
  • Using PowerShell scripts.
  • Using tools like Azure management studio/Service bus explorer.

We shall walkthrough each method. Let’s start with using the management portal. Let’s us navigate to management portal for creating the event hub. If you don’t have a subscription, Use the one month free subscription offer from Microsoft click here to know more.

Follow the URL to access the Management portal: https://portal.azure.com . Event hubs are placed in the following path: “New > Internet of Things > Event Hubs”. Once you navigate there, a new blade with title “Create Namespace” opens. Fill the details in the blade as required.

Once the Service bus namespace is created, Next step is to create the event hub itself Navigate to the Namespace blade as shown below:

 

Click on the “Event Hubs” under entities, A new blade will show up, Click on the event hub,

 

Fill in the details once the new blade opens,

 

Next step is to create a Shared access signature URL to access the Event hubs, as shown below:

 

Creating the Event Hubs using PowerShell script:

The following script will help you to create the event hub. I have added comments inline to help you understand better. Please note that the following script uses “Microsoft.ServiceBus.dll” which can be downloaded from this URL: https://www.nuget.org/packages/WindowsAzure.ServiceBus/

#—————————————————
# Step 1: List down all the Variables that you need

#—————————————————

$SBNamespace=“SampleSBNamespace”
$Location=“East US”
$Path=“MyEventHub”
$PartitionCount=12
$MessageRetentionInDays=7
$UserMetadata=$null
$ConsumerGroupName=“ConsumerGroup”

#————————————————————————
# Step 2: Adding the Adding the Microsoft.ServiceBus.dll into the context

#————————————————————————

Write-Output “Adding the Microsoft.ServiceBus.dll.”

$scriptPath=“C:\Packages\Microsoft.ServiceBus.dll” # provide the path where you have the dll. In my case, I have it in C:/Packages/
$assembly=Get-ChildItem $scriptPath -Include “Microsoft.ServiceBus.dll” -Recurse
Add-Type -Path $assembly.FullName

Write-Output “The /Microsoft.ServiceBus.dll/ assembly has been successfully added to the script.”

#———————————————–

# Step 3: Connecting to the desired subscription

#———————————————–

# Logging into Azure Account

Add-AzureAccount

# Now Select the subscription that in which you want to create the Event Hub
Select-AzureSubscription -SubscriptionName “[Your subscription]”
# gets the Subscription that is set (Just to verify that the correct subscription is set)

Get-AzureSubscription -Default


#——————————————–
# Step 4: Creating the Service Bus Namespace

#——————————————–

Write-Output “Creating the /$SBNamespace/ namespace in the /$Location/ region”

New-AzureSBNamespace -Name $SBNamespace -Location $Location -CreateACSNamespace $false -NamespaceType EventHub
$CurrentNamespace=Get-AzureSBNamespace -Name $SBNamespace
Write-Host“The [$SBNamespace] namespace in the /$Location/ region has been successfully created.”
$sbr=Get-AzureSBAuthorizationRule -Namespac e$SBNamespace
# Create the NamespaceManager object to create the Event Hub

Write-Output“Creating a NamespaceManager object for the /$SBNamespace/ namespace”
$NamespaceManager=[Microsoft.ServiceBus.NamespaceManager]::CreateFromConnectionString($sbr.ConnectionString);

Write-Output “NamespaceManager object for the /$SBNamespace/ namespace has been successfully created.”

#——————————–
# Step 5: Creating the Event Hub

#——————————–

Write-Output “Creating the Event Hub /$Path/ in the Namespace /$Namespace/ | PartitionCount=/$PartitionCount/ | MessageRetentionInDays=/$MessageRetentionInDays/”

$EventHubDescription=New-Object -TypeName Microsoft.ServiceBus.Messaging.EventHubDescription -ArgumentList $Path

$EventHubDescription.PartitionCount = $PartitionCount

$EventHubDescription.MessageRetentionInDays = $MessageRetentionInDays

$EventHubDescription.UserMetadata = $UserMetadata

$EventHubDescription.Path = $Path

$NamespaceManager.CreateEventHubIfNotExists($EventHubDescription);

Write-Output “The [$Path] event hub in the /$Namespace/ namespace has been successfully created.”

#————————————

# Step 6: Creating the Consumer Group

#————————————

Write-Output “Creating the consumer group /$ConsumerGroupName/ for the /$Path/ event hub…”

$ConsumerGroupDescription= New-Object -TypeName Microsoft.ServiceBus.Messaging.ConsumerGroupDescription -ArgumentList$Path, $ConsumerGroupName
$ConsumerGroupDescription.UserMetadata = $ConsumerGroupUserMetadata
$NamespaceManager.CreateConsumerGroupIfNotExists($ConsumerGroupDescription);
Write-Output “The consumer group /$ConsumerGroupName/ has been successfully created.”

Also, MSDN has some automated script that helps you to create Event hubs and Service bus components. Please follow this link: https://code.msdn.microsoft.com/Service-Bus-PowerShell-a46b7059

Okay, Instead of using the above mentioned method, you may like to use Azure RM template to deploy the Event Hubs. For this method, you need to create the “Deployment Template” json file. The Deployment template can be created as shown below:

{
“$schema”:“http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#”,

“contentVersion”:“1.0.0.0”,

“parameters”:{

“namespaceName”:{

“type”:“string”,

“metadata”:{

“description”:“Name of the EventHub namespace”

}},
“eventHubName”:{

“type”:“string”,

“metadata”:{

“description”:“Name of the Event Hub”
}},
“consumerGroupName”:{

“type”:“string”,

“metadata”:{

“description”:“Name of the Consumer Group”
} }

},


“variables”:{

“location”:“[resourceGroup().location]”,

“apiVersion”:“2015-08-01”,

“defaultSASKeyName”:“RootManageSharedAccessKey”,

“authRuleResourceId”:“[resourceId(‘Microsoft.EventHub/namespaces/authorizationRules’, parameters(‘namespaceName’), variables(‘defaultSASKeyName’))]”

},
“resources”:[

{
“apiVersion”:“2015-08-01”,

“name”:“[parameters(‘namespaceName’)]”,

“type”:“Microsoft.EventHub/Namespaces”,

“location”:“[variables(‘location’)]”,

“sku”:{

“name”:“Standard”,

“tier”:“Standard”

},
“resources”:[

{
“apiVersion”:“2015-08-01”,

“name”:“[parameters(‘eventHubName’)]”,

“type”:“EventHubs”,

“dependsOn”:[
“[concat(‘Microsoft.EventHub/namespaces/’, parameters(‘namespaceName’))]”

],

“properties”:{
“path”:“[parameters(‘eventHubName’)]”

},
“resources”:[

{
“apiVersion”:“2015-08-01”,

“name”:“[parameters(‘consumerGroupName’)]”,

“type”:“ConsumerGroups”,

“dependsOn”:[

“[parameters(‘eventHubName’)]”

],

“properties”:{}}]}]}],

“outputs”:{
“NamespaceConnectionString”:{

“type”:“string”,

“value”:“[listkeys(variables(‘authRuleResourceId’), variables(‘apiVersion’)).primaryConnectionString]”

},
“SharedAccessPolicyPrimaryKey”:{

“type”:“string”,

“value”:“[listkeys(variables(‘authRuleResourceId’), variables(‘apiVersion’)).primaryKey]”

}}}

You can save the template as json file locally. Once you have the file ready. You can run the following PowerShell,

#—————————————-
# Step 1 – Connecting to the Subscription
#—————————————-
Add-AzureRmAccount
Set-AzureRmContext-SubscriptionID <ProvidetheSubscriptionID>

#—————————————-
# Step 2 – Creating the Resource Group
#—————————————-

New-AzureRmResourceGroup -Name SampleResourceGroup -Location “West US”

#—————————————-
# Step 3 – Executing the RM Template
#—————————————-

New-AzureRmResourceGroupDeployment -NameExampleDeployment -ResourceGroupName SampleResourceGroup -TemplateFile “C:\Packages\DeployEventHubs.json” # Provide the path of the file.

When prompted you can supply the values as shown below.

If you prefer to provide the value through a file, then you can save the following file as json

{
“$schema”: “http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#”,

“contentVersion”: “1.0.0.0”,

“parameters”: {

“namespaceName”: {
“value”: “NewSample”},

“eventHubName”: {“value”: “MyEventHub”}

“consumerGroupName”: { “value”: “ConsumerGroup”}}}

And execute the following PowerShell script

#—————————————-
# Step 1 – Connecting to the Subscription
#—————————————-
Add-AzureRmAccount
Set-AzureRmContext -SubscriptionID <ProvidetheSubscriptionID>

#—————————————-
# Step 2 – Creating the Resource Group
#—————————————-

New-AzureRmResourceGroup -Name SampleResourceGroup -Location “West US”

#——————————————————–

# Step 3 – Executing the RM Template with Parameters file.

#———————————————————

New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName SampleResourceGroup -TemplateFile “C:\Packages\DeployEventHubs.json” -TemplateParameterFile “C:\Packages\DeployEventHubs.Parameters.json” # Provide the path of the files.

You can achieve the same by simply clicking the following link:

Deploy to Azure

 

The above Command uses RM template present in this link

Coming to the Third option, these are some tools which you can use to create service bus and event hubs

At this point, I am hoping that you would have created the Event Hubs utilizing one of the above mentioned methods. In the next Blog post we shall discuss about “Message protocols in Event Hubs”

References:

https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-resource-manager-namespace-event-hub

Published inAzureEvent HubsIoTPowerShellProgrammingSoftware Development

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *