แก้ไข

แชร์ผ่าน


Quickstart: Create an event hub using Azure PowerShell

In this quickstart, you create an event hub using Azure PowerShell.

Prerequisites

An Azure account with an active subscription. Create an account for free.

Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code or command block. Selecting Try It doesn't automatically copy the code or command to Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Button to launch Azure Cloud Shell.
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Screenshot that shows the Cloud Shell button in the Azure portal

To use Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block (or command block) to copy the code or command.

  3. Paste the code or command into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code or command.

If you're using PowerShell locally, you must run the latest version of PowerShell to complete this quickstart. If you need to install or upgrade, see Install and Configure Azure PowerShell.

Create a resource group

Run the following command to create a resource group. A resource group is a logical collection of Azure resources. All resources are deployed and managed in a resource group.

If you're using Azure Cloud Shell, switch to PowerShell from Bash in the upper left corner. Select Copy to copy the command and paste it into the Cloud Shell, and run it.

The following example creates a resource group in the East US region. Replace myResourceGroup with the name of the resource group you want to use.

$rgName="myResourceGroup$(Get-Random)"
$region="eastus"
New-AzResourceGroup –Name $rgName –Location $region

You see the output similar to the following one. You see the name of the resource with the random number suffix.

ResourceGroupName : myResourceGroup1625872532
Location          : eastus
ProvisioningState : Succeeded
Tags              : 
ResourceId        : /subscriptions/0000000000-0000-0000-0000-0000000000000/resourceGroups/myResourceGroup1625872532

Create an Event Hubs namespace

Run the following command to create an Event Hubs namespace in the resource group. An Event Hubs namespace provides a unique fully qualified domain name in which you can create one or more event hubs. Update the value of the namespace if you like.

$namespaceName="myNamespace$(Get-Random)"
New-AzEventHubNamespace -ResourceGroupName $rgName -NamespaceName $namespaceName -Location $region

You see the output similar to the following one. You see the name of the namespace in the Name field.

Name                   : myNamespace143349827
Id                     : /subscriptions/0000000000-0000-0000-0000-00000000000000/resourceGroups/myResourceGroup162587253
                         2/providers/Microsoft.EventHub/namespaces/myNamespace143349827
ResourceGroupName      : myResourceGroup1625872532
Location               : East US
Sku                    : Name : Standard , Capacity : 1 , Tier : Standard
Tags                   : 
ProvisioningState      : Succeeded
Status                 : Active
CreatedAt              : 3/13/2023 10:22:54 PM
UpdatedAt              : 3/13/2023 10:23:41 PM
ServiceBusEndpoint     : https://myNamespace143349827.servicebus.windows.net:443/
Enabled                : True
KafkaEnabled           : True
IsAutoInflateEnabled   : False
MaximumThroughputUnits : 0
ZoneRedundant          : False
ClusterArmId           : 
DisableLocalAuth       : False
MinimumTlsVersion      : 1.2
KeySource              : 
Identity               : 
IdentityType           : 
IdentityId             : 
EncryptionConfig       :

Create an event hub

Now that you have an Event Hubs namespace, create an event hub within that namespace by running the following command.

$ehubName="myEventHub"
New-AzEventHub -ResourceGroupName $rgName -NamespaceName $namespaceName -EventHubName $ehubName

You see output similar to the following one.

ArchiveNameFormat            : 
BlobContainer                : 
CaptureEnabled               : 
CreatedAt                    : 3/13/2023 10:26:07 PM
DataLakeAccountName          : 
DataLakeFolderPath           : 
DataLakeSubscriptionId       : 
DestinationName              : 
Encoding                     : 
Id                           : /subscriptions/00000000000-0000-0000-0000-00000000000000/resourceGroups/myResourceGroup162
                               5872532/providers/Microsoft.EventHub/namespaces/myNamespace143349827/eventhubs/myEven
                               tHub
IntervalInSeconds            : 
Location                     : eastus
MessageRetentionInDays       : 7
Name                         : myEventHub
PartitionCount               : 4
PartitionId                  : {0, 1, 2, 3}
ResourceGroupName            : myResourceGroup1625872532
SizeLimitInBytes             : 
SkipEmptyArchive             : 
Status                       : Active
StorageAccountResourceId     : 
SystemDataCreatedAt          : 
SystemDataCreatedBy          : 
SystemDataCreatedByType      : 
SystemDataLastModifiedAt     : 
SystemDataLastModifiedBy     : 
SystemDataLastModifiedByType : 
Type                         : Microsoft.EventHub/namespaces/eventhubs
UpdatedAt                    : 3/13/2023 10:26:07 PM

Congratulations! You have used Azure PowerShell to create an Event Hubs namespace, and an event hub within that namespace.

Clean up resources

If you want to keep this event hub so that you can test sending and receiving events, ignore this section. Otherwise, run the following command to delete the resource group. This command deletes all the resources in the resource group and the resource group itself.

Remove-AzResourceGroup $rgName

Next steps

In this article, you created the Event Hubs namespace, and used sample applications to send and receive events from your event hub. For step-by-step instructions to send events to (or) receive events from an event hub, see the Send and receive events tutorials: