Compile and run of Librdkafka client under Linux

Source: Internet
Author: User
Tags stdin strtok

compile and run of Librdkafka client under Linux

Librdkafka is an open-source    The Kafka client /C + + implementation provides a Kafka producer and consumer interface.

Because of the project needs, I want to encapsulate the Kafka producer interface for others to call, so first install the LIBRDKAKFA, and then modify the demo to encapsulate a producer interface.

[i] installation Librdkafka

First on github download Librdkafka source code, after decompression to compile;

CD Librdkafka-master

chmod 777 Configure lds-gen.py

./configure

Make

Make install

   In make, if 64-bit Linux reports the following exception

   /bin/ld:librdkafka.lds:1: syntax error in VERSION script

   只要Makefile.config里面的WITH_LDS=y这一行注释掉就不会报错了。

[II] the producer interface of the package Librdkafka

#include <ctype.h>#include<signal.h>#include<string.h>#include<unistd.h>#include<stdlib.h>#include<syslog.h>#include<time.h>#include<sys/time.h>#include"librdkafka/rdkafka.h"  /*For Kafka driver*/Static intRun =1;Staticrd_kafka_t *rk;rd_kafka_topic_t*Rkt;intPartition =rd_kafka_partition_ua;rd_kafka_topic_conf_t*topic_conf;Static voidStop (intSIG) {Run=0; Fclose (stdin); /*abort fgets ()*/}Static voidSIG_USR1 (intSIG) {rd_kafka_dump (stdout, RK);}intInitproducer (Char*parameters) {    intARGC =1; Char**argv; Char*para; Char*delim =" "; Char*brokers ="localhost:9092"; Char*topic =NULL; intopt; rd_kafka_conf_t*conf; Charerrstr[ +]; Chartmp[ -]; Charcopyparameters[1024x768];    strcpy (copyparameters, parameters); Para=strtok (parameters, Delim); ARGC++;  while((para = Strtok (NULL, delim))! =NULL) {ARGC++; } argv= (Char**)malloc(argc*sizeof(Char*)); ARGC=0; ARGV[ARGC]="Initproducer"; Para=strtok (Copyparameters, Delim); ARGC++; ARGV[ARGC]=para;  while((para = Strtok (NULL, delim))! =NULL) {ARGC++; ARGV[ARGC]=para; } argc++; /*Kafka Configuration*/conf=rd_kafka_conf_new (); /*Quick Termination*/snprintf (TMP,sizeof(TMP),"%i", SIGIO); Rd_kafka_conf_set (conf,"internal.termination.signal", TMP, NULL,0); /*Topic Configuration*/topic_conf=rd_kafka_topic_conf_new ();  while(opt = getopt (argc, argv,"Pclt:p:b:z:qd:o:ex:as:")) != -1) {        Switch(opt) { Case 'T': Topic=Optarg;  Break;  Case 'P': Partition=atoi (OPTARG);  Break;  Case 'b': Brokers=Optarg;  Break; default: fprintf (stderr,"Percent Failed to init producer with error parameters\n"); }    }    if(Optind! = ARGC | |!topic) {Exit (1);    } signal (SIGINT, stop);    Signal (SIGUSR1, SIG_USR1); /*Create Kafka Handle*/    if(! (RK = Rd_kafka_new (rd_kafka_producer, conf, Errstr,sizeof(ERRSTR)))) {fprintf (stderr,"Percent Failed to create new producer:%s\n", ERRSTR); Exit (1);    } rd_kafka_set_log_level (RK, Log_debug); /*ADD Brokers*/    if(Rd_kafka_brokers_add (RK, brokers) = =0) {fprintf (stderr,"No Valid brokers specified\n"); Exit (1); }    /*Create Topic*/Rkt=rd_kafka_topic_new (RK, topic, topic_conf); Topic_conf= NULL;/*Now owned by Topic*/    return 1;}intFreeproducer () {/*Destroy Topic*/Rd_kafka_topic_destroy (RKT); /*Destroy the handle*/Rd_kafka_destroy (RK); if(topic_conf) Rd_kafka_topic_conf_destroy (topic_conf); /*Let background threads clean up and terminate cleanly.*/Run=5;  while(run-->0&& rd_kafka_wait_destroyed ( +) == -1) printf ("waiting for Librdkafka to decommission\n"); if(Run <=0) Rd_kafka_dump (stdout, RK); return 1;}intMain (intargcChar**argv) {    CharParameter[] ="- t Xx-http-keyword-log-b 10.10.6.101:9092,10.10.6.102:9092,10.10.6.104:9092"; Charbuf[1024x768];    Initproducer (parameter);  while(Run && fgets (buf,sizeof(BUF), stdin)) {        if(Rd_kafka_produce (Rkt, partition, Rd_kafka_msg_f_copy, buf, strlen (BUF), NULL,0, NULL) = =-1) {fprintf (stderr,"Failed to produce to topic%s partition%i:%s\n", Rd_kafka_topic_name (RKT), partition, Rd_kafka_err2str (Rd_kafka_last_error ())); }Else{fprintf (stderr,"Sent%zd bytes to topic%s partition%i\n", strlen (BUF), Rd_kafka_topic_name (RKT), partition);    }} freeproducer (); return 0;}

[Three] compile run

Compile the time to add-lrdkafka -lz -lpthread -lrt这些选项:gcc myProducer.c -o myProducer -lrdkafka -lz -lpthread -lrt

The error is reported at compile time while loading share library librdkafak.so.1, this is because make is Librdkafak.so.1 placed under/usr/local/lib, in the Linux default shared library path/lib and/usr/lib can not be found, just execute the following two sentences:

echo "/usr/local/lib" >>/etc/ld.so.conf
Ldconfig

Run./myproducer, which continuously reads the typed string from the terminal and then sends it to Kafka, which is able to consume viewing data through Kafka's own console consumer.

Compile and run of Librdkafka client under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.