Skip to content

Commit

Permalink
[Event Hubs] updates readme links and samples (#4691)
Browse files Browse the repository at this point in the history
  • Loading branch information
chradek authored and ramya-rao-a committed Aug 6, 2019
1 parent 463e0d8 commit 404347b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
53 changes: 42 additions & 11 deletions sdk/eventhub/event-hubs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The Azure Event Hubs client library allows you to send and receive events in you

Install the Azure Event Hubs client library using npm

`npm install @azure/[email protected].1`
`npm install @azure/[email protected].2`

**Prerequisites**: You must have an [Azure subscription](https://azure.microsoft.com/free/) and a
[Event Hubs Namespace](https://docs.microsoft.com/en-us/azure/event-hubs/) to use this package.
Expand Down Expand Up @@ -96,14 +96,33 @@ const partitionIds = await client.getPartitionIds();

### Publish events to an Event Hub

In order to publish events, you'll need to create an `EventHubProducer`. Producers may be dedicated to a specific partition, or allow the Event Hubs service to decide which partition events should be published to. It is recommended to use automatic routing when the publishing of events needs to be highly available or when event data should be distributed evenly among the partitions. In the our example, we will take advantage of automatic routing.
In order to publish events, you'll need to create an `EventHubProducer`. Producers may be dedicated to a specific partition, or allow the Event Hubs service to decide which partition events should be published to. It is recommended to use automatic routing when the publishing of events needs to be highly available or when event data should be distributed evenly among the partitions. In the below examples, we will take advantage of automatic routing.

You can also use the [send](https://azure.github.io/azure-sdk-for-js/event-hubs/classes/eventhubproducer.html#send) method to send multiple events using a single call.
#### Send a single event or an array of events

Use the [send](https://azure.github.io/azure-sdk-for-js/event-hubs/classes/eventhubproducer.html#send) method to send a single event or multiple events using a single call.

```javascript
const client = new EventHubClient("connectionString", "eventHubName");
const producer = client.createProducer();
await producer.send({ body: "my-event-body" });
await producer.send([{ body: "foo" }, { body: "bar" }]);
```

#### Send a batch of events

Use the [createBatch](https://azure.github.io/azure-sdk-for-js/event-hubs/classes/eventhubproducer.html#createbatch) method to create
an `EventDataBatch` object which can then be sent using the [send](https://azure.github.io/azure-sdk-for-js/event-hubs/classes/eventhubproducer.html#send) method.
Events may be added to the `EventDataBatch` using the [tryAdd](https://azure.github.io/azure-sdk-for-js/event-hubs/classes/eventdatabatch.html#tryadd)
method until the maximum batch size limit in bytes has been reached.

```javascript
const client = new EventHubClient("connectionString", "eventHubName");
const producer = client.createProducer();
const eventDataBatch = await producer.createBatch();
let wasAdded = eventDataBatch.tryAdd({ body: "my-event-body" });
wasAdded = eventDataBatch.tryAdd({ body: "my-event-body-2" });
await producer.send(eventDataBatch);
```

The [Inspect an Event Hub](#inspect-an-event-hub) example shows how to get the list of partition ids should you wish to specify one for a producer.
Expand Down Expand Up @@ -184,26 +203,38 @@ for await (const events of consumer.getEventIterator()){

### Consume events using an Event Processor

Using an `EventHubConsumer` to consume events like in the previous examples puts the responsibility of storing the checkpoints (the last processed event) on the user. Checkpoints are important for restarting the task of processing events from the right position in a partition. Ideally, you would also want to run multiple programs targeting different partitions with some load balancing. This is where an `EventProcessor` can help.
Using an `EventHubConsumer` to consume events like in the previous examples puts the responsibility of storing the checkpoints (the last processed event) on the user. Checkpoints are important for restarting the task of processing events from the right position in a partition. Ideally, you would also want to run multiple programs targeting different partitions with some load balancing.
This is where an [EventProcessor](https://azure.github.io/azure-sdk-for-js/event-hubs/classes/eventprocessor.html) can help.

The `EventProcessor` will delegate the processing of events to a `PartitionProcessor` that you provide, allowing you to focus on business logic while the processor holds responsibility for managing the underlying consumer operations including checkpointing and load balancing.
The `EventProcessor` will delegate the processing of events to a [PartitionProcessor](https://azure.github.io/azure-sdk-for-js/event-hubs/interfaces/partitionprocessor.html)
that you provide, allowing you to focus on business logic while the processor holds responsibility for managing the underlying consumer
operations including checkpointing and load balancing.

While load balancing is a feature we will be adding in the next update, you can see how to use the `EventProcessor` in the below example, where we use an `InMemoryPartitionManager` that does checkpointing in memory.
While load balancing is a feature we will be adding in the next update, you can see how to use the `EventProcessor` in the below
example, where we use an [InMemoryPartitionManager](https://azure.github.io/azure-sdk-for-js/event-hubs/classes/inmemorypartitionmanager.html) that does checkpointing in memory.

```javascript
class SimplePartitionProcessor {
// Gets called once before the processing of events from current partition starts.
async initialize() { /* your code here */ }

async initialize() {
/* your code here */
}

// Gets called for each batch of events that are received.
// You may choose to use the checkpoint manager to update checkpoints.
async processEvents(events) { /* your code here */ }
async processEvents(events) {
/* your code here */
}

// Gets called for any error when receiving events.
async processError(error) { /* your code here */ }
async processError(error) {
/* your code here */
}

// Gets called when Event Processor stops processing events for current partition.
async close(reason) { /* your code here */ }
async close(reason) {
/* your code here */
}
}

const client = new EventHubClient("my-connection-string", "my-event-hub");
Expand Down
8 changes: 4 additions & 4 deletions sdk/eventhub/event-hubs/samples/sendEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function main(): Promise<void> {
// ];
console.log("Sending single event...");
const scientist = listOfScientists[0];
producer.send({ body: `${scientist.firstName} ${scientist.name}` });
await producer.send({ body: `${scientist.firstName} ${scientist.name}` });

console.log("Sending multiple events...");
const events: EventData[] = [];
Expand All @@ -53,16 +53,16 @@ async function main(): Promise<void> {
await producer.send(events);

console.log("Creating and sending a batch of events...");
const eventDatabatch = await producer.createBatch();
const eventDataBatch = await producer.createBatch();
for (let index = 0; index < listOfScientists.length; index++) {
const scientist = listOfScientists[index];
const isAdded = eventDatabatch.tryAdd({ body: `${scientist.firstName} ${scientist.name}` });
const isAdded = eventDataBatch.tryAdd({ body: `${scientist.firstName} ${scientist.name}` });
if (!isAdded) {
console.log(`Unable to add event ${index} to the batch`);
break;
}
}
await producer.send(eventDatabatch);
await producer.send(eventDataBatch);

await producer.close();
} finally {
Expand Down

0 comments on commit 404347b

Please sign in to comment.