This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Azure Event Hubs is a Big Data streaming platform and event ingestion service, capable of receiving and processing millions of events per second. Event Hubs can process and store events, data, or telemetry produced by distributed software and devices. Data sent to an event hub can be transformed and stored using any real-time analytics provider or batching/storage adapters. For detailed overview of Event Hubs, seeEvent Hubs overview andEvent Hubs features.
This tutorial describes how to send events to an event hub using a console application in C.
To complete this tutorial, you need the following:
In this section shows how to write a C app to send events to your event hub. The code uses the Proton AMQP library from theApache Qpid project. This is analogous to using Service Bus queues and topics with AMQP from C as shownin this sample. For more information, see theQpid Proton documentation.
From theQpid AMQP Messenger page, follow the instructions to install Qpid Proton, depending on your environment.
To compile the Proton library, install the following packages:
sudo apt-get install build-essential cmake uuid-dev openssl libssl-devDownload theQpid Proton library, and extract it, e.g.:
wget https://archive.apache.org/dist/qpid/proton/0.7/qpid-proton-0.7.tar.gztar xvfz qpid-proton-0.7.tar.gzCreate a build directory, compile and install:
cd qpid-proton-0.7mkdir buildcd buildcmake -DCMAKE_INSTALL_PREFIX=/usr ..sudo make installIn your work directory, create a new file calledsender.c with the following code. Remember to replace the values for your SAS key/name, event hub name, and namespace. You must also substitute a URL-encoded version of the key for theSendRule created earlier. You can URL-encode ithere.
#include "proton/message.h"#include "proton/messenger.h"#include <getopt.h>#include <proton/util.h>#include <sys/time.h>#include <stddef.h>#include <stdio.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <signal.h>volatile sig_atomic_t stop = 0;#define check(messenger) \ { \ if(pn_messenger_errno(messenger)) \ { \ printf("check\n"); \ die(__FILE__, __LINE__, pn_error_text(pn_messenger_error(messenger))); \ } \ }void interrupt_handler(int signum){ if(signum == SIGINT){ stop = 1; }}pn_timestamp_t time_now(void){ struct timeval now; if (gettimeofday(&now, NULL)) pn_fatal("gettimeofday failed\n"); return ((pn_timestamp_t)now.tv_sec) * 1000 + (now.tv_usec / 1000);}void die(const char *file, int line, const char *message){ printf("Dead\n"); fprintf(stderr, "%s:%i: %s\n", file, line, message); exit(1);}int sendMessage(pn_messenger_t * messenger) { char * address = (char *) "amqps://{SAS Key Name}:{SAS key}@{namespace name}.servicebus.windows.net/{event hub name}"; char * msgtext = (char *) "Hello from C!"; pn_message_t * message; pn_data_t * body; message = pn_message(); pn_message_set_address(message, address); pn_message_set_content_type(message, (char*) "application/octect-stream"); pn_message_set_inferred(message, true); body = pn_message_body(message); pn_data_put_binary(body, pn_bytes(strlen(msgtext), msgtext)); pn_messenger_put(messenger, message); check(messenger); pn_messenger_send(messenger, 1); check(messenger); pn_message_free(message);}int main(int argc, char** argv) { printf("Press Ctrl-C to stop the sender process\n"); signal(SIGINT, interrupt_handler); pn_messenger_t *messenger = pn_messenger(NULL); pn_messenger_set_outgoing_window(messenger, 1); pn_messenger_start(messenger); while(!stop) { sendMessage(messenger); printf("Sent message\n"); sleep(1); } // release messenger resources pn_messenger_stop(messenger); pn_messenger_free(messenger); return 0;}Compile the file, assuminggcc:
gcc sender.c -o sender -lqpid-protonNote
This code uses an outgoing window of 1 to force the messages out as soon as possible. It is recommended that your application try to batch messages to increase throughput. See theQpid AMQP Messenger page for information about how to use the Qpid Proton library in this and other environments, and from platforms for which bindings are provided (currently Perl, PHP, Python, and Ruby).
Run the application to send messages to the event hub.
Congratulations! You have now sent messages to an event hub.
Read the following articles:
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?