@azure/eventhubs-checkpointstore-blob
1.0.1 • Public • PublishedAzure Event Hubs Checkpoint Store library for Javascript using Storage Blobs
An Azure Blob storage based solution to store checkpoints and to aid in load balancing when usingEventHubConsumerClient
from the@azure/event-hubs library
Source code |Package (npm) |API Reference Documentation |Samples
Getting started
Install the package
Install the Azure Event Hubs Checkpoint Store Blob library using npm
npm install @azure/eventhubs-checkpointstore-blob
Prerequisites: You must have anAzure subscription, anEvent Hubs Namespace to use this package, and aStorage account
If you are using this package in a Node.js application, then use Node.js 8.x or higher.
Configure Typescript
TypeScript users need to have Node type definitions installed:
npm install @types/node
You also need to enablecompilerOptions.allowSyntheticDefaultImports
in your tsconfig.json. Note that if you have enabledcompilerOptions.esModuleInterop
,allowSyntheticDefaultImports
is enabled by default. SeeTypeScript's compiler options handbook for more information.
Key concepts
Scale: Create multiple consumers, with each consumer taking ownership of reading from a few Event Hubs partitions.
Load balance: Applications that support load balancing consist of one or more instances of
EventHubConsumerClient
which have been configured to consume events from the same Event Hub and consumer groupand the sameCheckpointStore
.They balance the workload across different instances by distributing the partitions to be processed among themselves.Checkpointing: It is a process by which readers mark or commit their position within a partition event sequence. Checkpointing is the responsibility of the consumer andoccurs on a per-partition basis within a consumer group. This responsibility means that for each consumer group, each partition reader must keep track of its current positionin the event stream, and can inform the service when it considers the data stream complete.
If a reader disconnects from a partition, when it reconnects it begins reading at the checkpoint that was previously submitted by the last reader of that partition in that consumer group.When the reader connects, it passes the offset to the event hub to specify the location at which to start reading. In this way, you can use checkpointing to both mark events as "complete" by downstream applications,and to provide resiliency if a failover between readers running on different machines occurs. It is possible to return to older data by specifying a lower offset from this checkpointing process.Through this mechanism, checkpointing enables both failover resiliency and event stream replay.
ABlobCheckpointStoreis a class that implements key methods required by the EventHubConsumerClient to balance load and update checkpoints.
Examples
CheckpointStore
using Azure Blob Storage
Create aUse the below code snippet to create aCheckpointStore
. You will need to provide the connection string to your storage account.
import{ContainerClient}from"@azure/storage-blob",import{BlobCheckpointStore}from"@azure/eventhubs-checkpointstore-blob"constcontainerClient=newContainerClient("storage-connection-string","container-name");if(!containerClient.exists()){awaitcontainerClient.create();// This can be skipped if the container already exists}constcheckpointStore=newBlobCheckpointStore(containerClient);
Checkpoint events using Azure Blob storage
To checkpoint events received using Azure Blob Storage, you will need to pass an objectthat is compatible with theSubscriptionEventHandlersinterface along with code to call theupdateCheckpoint()
method.
In this example,SubscriptionHandlers
implementsSubscriptionEventHandlers and also handles checkpointing.
import{ContainerClient}from"@azure/storage-blob";import{BlobCheckpointStore}from"@azure/eventhubs-checkpointstore-blob";import{EventHubConsumerClient}from"@azure/event-hubs";constconsumerGroup="consumer-group-name";constconnectionString="event-hub-connectionstring";constcontainerClient=newContainerClient("storage-connection-string","container-name");if(!(awaitcontainerClient.exists())){awaitcontainerClient.create();// This can be skipped if the container already exists}constcheckpointStore=newBlobCheckpointStore(containerClient);classSubscriptionHandlers{asyncprocessEvents(event,context){// custom logic for processing events goes here// Checkpointing will allow your service to restart and pick// up from where it left off.//// You'll want to balance how often you checkpoint with the// performance of your underlying checkpoint store.awaitcontext.updateCheckpoint(event);}asyncprocessError(err,context){// handle any errors that occur during the course of// this subscriptionconsole.log(`Errors in subscription:${err}`);}}constconsumerClient=newEventHubConsumerClient(consumerGroup,connectionString,checkpointStore);constsubscription=consumerClient.subscribe(newSubscriptionHandlers());// events will now flow into the handlers defined above// to stop the subscription:subscription.close();
Troubleshooting
Enable logs
You can set theAZURE_LOG_LEVEL
environment variable to one of the following values to enable logging tostderr
:
- verbose
- info
- warning
- error
You can also set the log level programatically by importing the@azure/logger package and calling thesetLogLevel
function with one of the log level values.
When setting a log level either programatically or via theAZURE_LOG_LEVEL
environment variable,any logs that are written using a log level equal to or less than the one you choose will be emitted.For example, when you set the log level toinfo
, the logs that are written for levelswarning
anderror
are also emitted.This SDK follows the Azure SDK for TypeScriptguidelineswhen determining which level to log to.
You can alternatively set theDEBUG
environment variable to get logs when using this library.This can be useful if you also want to emit logs from the dependenciesrhea-promise
andrhea
as well.
Note: AZURE_LOG_LEVEL, if set, takes precedence over DEBUG.Do not specify anyazure
libraries via DEBUG when also specifyingAZURE_LOG_LEVEL or calling setLogLevel.
You can set the following environment variable to get the debug logs when using this library.
- Getting only info level debug logs from the Eventhubs Checkpointstore Blob.
export DEBUG=azure:eventhubs-checkpointstore-blob:info
Logging to a file
Enable logging as shown above and then run your test script as follows:
Logging statements from your test script go to
out.log
and logging statements from the sdk go todebug.log
.node your-test-script.js> out.log2>debug.log
Logging statements from your test script and the sdk go to the same file
out.log
by redirecting stderr to stdout (&1), and then redirect stdout to a file:node your-test-script.js>out.log2>&1
Logging statements from your test script and the sdk go to the same file
out.log
.node your-test-script.js&> out.log
Next Steps
Please take a look at thesamplesdirectory for detailed example.
Contributing
If you'd like to contribute to this library, please read thecontributing guide to learn more about how to build and test the code.
Package Sidebar
Install
npm i @azure/eventhubs-checkpointstore-blob@1.0.1
Version
1.0.1
License
MIT
Unpacked Size
88.3 kB
Total Files
15