0% found this document useful (0 votes)
17 views

Lab 1 - Getting Started With Event Hubs

Uploaded by

Dilson Cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab 1 - Getting Started With Event Hubs

Uploaded by

Dilson Cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Real-Time Big Data Processing with

Azure
Lab 1 - Getting Started with Event Hubs

Overview
In this lab, you will create an Azure Event Hub and use it to collect event data from a client
application.

What You’ll Need


To complete the labs, you will need the following:

• A web browser
• A Microsoft account
• A Microsoft Azure subscription
• A Windows, Linux, or Mac OS X computer
• The lab files for this course

Note: To set up the required environment for the lab, follow the instructions in the Setup
document for this course. Specifically, you must have signed up for an Azure subscription and
installed Node.JS on your computer.

Exercise 1: Provision an Azure Event Hub


In this exercise, you will create an Azure event hub to which applications can submit event details.

Note: The Microsoft Azure portal is continually improved in response to customer feedback. The
steps in this exercise reflect the user interface of the Microsoft Azure portal at the time of writing,
but may not match the latest design of the portal exactly.

Create a Namespace
Before you can create event hubs, you must create an event hub namespace.

1. In a web browser, navigate to http://portal.azure.com, and if prompted, sign in using


the Microsoft account that is associated with your Azure subscription.
2. Create a New Resource Group Called RealTime<yourinitials>, if it is already created skip
this step
3. Click on the Resource Gorup named RealTime<yourinitials>
4. Click en create
5. In Search services and Marketplace type event hubs and press enter, click on Event Hubs,
then click create
6. In the Create Event Hubs Namespace blade, enter the following settings, and then click
Create:
• Subscription: Leave the default subscription selection
• Resuorce Group: Select (if not already selected) RealTime<yourinitials>
• Name: Enter a unique name (and make a note of it!)
• Select a Location Close to you (East US 2 for instance)
• Pricing tier: Basic
• Throughput Units: 1
7. Click Review + Create
8. Click Create
9. In the Azure portal, view Notifications to verify that deployment has started. Then wait
for the namespace to be deployed (this can take a few minutes.)

Add an Event Hub


Now that you have a namespace, you are ready to add an event hub.

1. In the Azure portal, browse to the event hubs you created earlier
2. Un the Navigation Pane (on your left search for Entities and click Event Hubs)
3. Click + Event Hub
• Name: Enter a unique name (and make a note of it!)
• Partition Count: 2
• Click Create
4. In the Azure portal, wait for the notification that the event hub has been created

Create a Shared Access Policy


Access to event hubs is controlled using shared access policies.

1. In the newly created Event Hubs Instance


2. In the blade for your event hub, click Shared access policies.
3. On the Shared access policies blade for your event hub, click Add. Then add a shared
access policy with the following settings:
• Policy name: DeviceAccess
• Claim: Select Send and Listen
4. Wait for the shared access policy to be created, then select it, and note that primary and
secondary connection strings have been created for it. Copy the Connection string-
primary key value to the clipboard - you will use it to connect to your event hub from a
simulated client device in the next exercise.

Exercise 2: Create a Client to Submit Events


In this exercise, you will create a simple client application that submits event details.

Download and install (if not present on your Machine)


https://nodejs.org/en/download/current/

Create a Node.JS Application to Submit Events


Now that you have created an event hub, you can submit events to it from devices and
applications. In this exercise, you will create a Node.JS application to simulate random device
readings.

1. Open a Node.JS console and navigate to the eventclient folder in the folder where you
extracted the lab files.
2. Enter the following command, and press RETURN to accept all the default options. This
creates a package.json file for your application:
npm init
3. Enter the following command to install the Azure Event Hubs package:
npm install azure-event-hubs
4. Use a text editor to edit the eventclient.js file in the eventclient folder.
5. Modify the script to set the connStr variable to reflect your shared access policy
connection string, as shown here:
var evthub = require('azure-event-hubs');

const connStr = '--your Connection string–primary key--';

var client =
evthub.EventHubClient.createFromConnectionString(connStr)
setInterval(function(){
dev = 'dev' + String(Math.floor((Math.random() * 10) + 1));
val = String(Math.random());
client.send({ body: {device: dev, reading: val}});
console.log(dev + ": " + val);
}, 500);

6. Save the script and close the file.


7. Execute the newly updated script using
node eventclient.js
8. You should see some values on your console
9. Leavit running

Exercise 3: Read Data from an Event Hub


In this exercise, you will create a simple client application that reads data from an event hub.

Create a Client Application to Read Messages from the Event Hub


Now you will create a second Node.JS client application to read events from your event hub.

1. Open a second Node.JS console, and navigate to the eventreader folder in the folder
where you extracted the lab files.
2. Enter the following command, and press RETURN to accept all the default options. This
creates a package.json file for your application:
npm init
3. Enter the following command to install the Azure IoT Hub package:
npm install azure-event-hubs
4. Use a text editor to edit the eventreader.js file in the eventreader folder.
5. Modify the script to set the connStr constant to reflect the shared access policy
connection string for your Event Hub, as shown here:

const { EventHubClient, EventPosition } = require('azure-event-


hubs');

const connStr = '--your Connection string–primary key--';

const client = EventHubClient.createFromConnectionString(connStr


);

async function main() {

const onError = (err) => {


console.log("An error occurred on the receiver ", err);
};

const onMessage = (eventData) => {


console.log(eventData.body.device + ':' +
eventData.body.reading);
};

var ids = await client.getPartitionIds();


for (let i = 0; i < ids.length; i++) {
client.receive(String(ids[i]), onMessage, onError,
{ eventPosition:
EventPosition.fromEnqueuedTime(Date.now()) });
}
}

main().catch((err) => {
console.log(err);
});
6. Save the script and close the file.
10. Execute the newly updated script using
node eventclient.js
11. You should see some values on your console
12. Leavit running

Read Events from the Event Hub


You can now run your second client application to read event data from the event hub

1. Ensure that your eventclient.js script is still running.


3. Observe the script running as it reads the device readings that are submitted to the event hub
by the eventclient.js script.

4. After both scripts have been running for a while, stop them by pressing CTRL+C in each of the
console windows. Then close the console windows.

Note: You will use the resources you created in this lab when performing the next lab, so do not
delete them. Ensure that all Node.js scripts are stopped to minimize ongoing resource usage
costs.

You might also like