linux下rdkafka编译,Linux下librdkafka客户端的编译运行

#include #include#include#include#include#include#include#include#include"librdkafka/rdkafka.h" /*for Kafka driver*/

static int run = 1;static rd_kafka_t *rk;

rd_kafka_topic_t*rkt;int partition =RD_KAFKA_PARTITION_UA;

rd_kafka_topic_conf_t*topic_conf;static void stop (intsig) {

run= 0;

fclose(stdin);/*abort fgets()*/}static void sig_usr1 (intsig) {

rd_kafka_dump(stdout, rk);

}int initProducer (char *parameters) {int argc = 1;char **argv;char *para;char *delim = " ";char *brokers = "localhost:9092";char *topic =NULL;intopt;

rd_kafka_conf_t*conf;char errstr[512];char tmp[16];char copyParameters[1024];

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,"%% 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,"%% 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(1000) == -1)

printf("Waiting for librdkafka to decommission\n");if (run <= 0)

rd_kafka_dump(stdout, rk);return 1;

}int main (int argc, char **argv)

{char parameter[] = "-t XX-HTTP-KEYWORD-LOG -b 10.10.6.101:9092,10.10.6.102:9092,10.10.6.104:9092";char buf[1024];

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值