#ifndef __KAFKA_H__
#define __KAFKA_H__
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/time.h>
#include <errno.h>
#include "rdkafka.h"
#ifdef __BIG_DRAGON_DEMO__
#else
#endif
class bigDragonPlatformMgr
{
public:
bigDragonPlatformMgr(){};
~bigDragonPlatformMgr(){}
int init(int partition, char *brokers, char *topic);
int sendData(const char* buf, const int buf_len);
void destroy();
private:
int m_partition;
//rd
rd_kafka_t* m_handler;
rd_kafka_conf_t* m_conf;
//topic
rd_kafka_topic_t* m_topic;
rd_kafka_topic_conf_t* m_topicConf;
};
#endif
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/time.h>
#include <errno.h>
#include <glog/logging.h>
#include "rdkafka.h"
#include "cafkac.h"
#ifdef __BIG_DRAGON_DEMO__
#else
#endif
const int __FAILED__ = -1;
const int __SUCCESS__ = 0;
static void logCallBack(const rd_kafka_t* rk, int level, const char* fac, const char* buf)
{
#ifdef __BIG_DRAGON_DEMO__
struct timeval tv;
gettimeofday(&tv, NULL);
fprintf(stderr, "%u.%03u RDKAFKA-%i-%s: %s: %s\n",
(int)tv.tv_sec, (int)(tv.tv_usec / 1000),
level, fac, rk ? rd_kafka_name(rk) : NULL, buf);
#else
LOG(INFO) << "fac :" << fac << " " << "rk:" << (rk ? rd_kafka_name(rk) : "NULL") << " > " << buf;
#endif
}
int bigDragonPlatformMgr::init(int partition, char *brokers, char *topic)
{
char tmp[16]={0};
char errstr[512]={0};
m_partition = partition;
m_conf = rd_kafka_conf_new();
rd_kafka_conf_set_log_cb(m_conf, logCallBack);
snprintf(tmp, sizeof(tmp), "%i", SIGIO);
rd_kafka_conf_set(m_conf, "internal.termination.signal", tmp, NULL, 0);
m_topicConf = rd_kafka_topic_conf_new();
m_handler = rd_kafka_new(RD_KAFKA_PRODUCER, m_conf, errstr, sizeof(errstr));
if (!m_handler)
{
#ifdef __BIG_DRAGON_DEMO__
fprintf(stderr, "*****Failed to create new producer: %s*******\n",errstr);
#else
LOG(ERROR) << errstr;
#endif
return __FAILED__;
}
rd_kafka_set_log_level(m_handler, LOG_DEBUG);
if (rd_kafka_brokers_add(m_handler, brokers) == 0)
{
#ifdef __BIG_DRAGON_DEMO__
fprintf(stderr, "****** No valid brokers specified********\n");
#else
LOG(ERROR) << "****** No valid brokers specified********\n";
#endif
return __FAILED__;
}
m_topic = rd_kafka_topic_new(m_handler, topic, m_topicConf);
return __SUCCESS__;
}
void bigDragonPlatformMgr::destroy()
{
rd_kafka_topic_destroy(m_topic);
rd_kafka_destroy(m_handler);
}
int bigDragonPlatformMgr::sendData(const char* buffer, const int buf_len)
{
int ret;
char errstr[512]={0};
if(NULL == buffer)
return 0;
ret = rd_kafka_produce(m_topic, m_partition, RD_KAFKA_MSG_F_COPY,
(void*)buffer, (size_t)buf_len, NULL, 0, NULL);
if(ret == -1)
{
#ifdef __BIG_DRAGON_DEMO__
fprintf(stderr,"****Failed to produce to topic %s partition %i: %s*****\n",
rd_kafka_topic_name(m_topic), m_partition,
rd_kafka_err2str(rd_kafka_errno2err(errno)));
#else
LOG(ERROR) << "m_topic " << rd_kafka_topic_name(m_topic);
LOG(ERROR) << "m_partition " << m_partition;
LOG(ERROR) << "rd_kafka_errno2err " << rd_kafka_err2str(rd_kafka_errno2err(errno));
#endif
rd_kafka_poll(m_handler, 0);
return __FAILED__;
}
#ifdef __BIG_DRAGON_DEMO__
fprintf(stderr, "***Sent %d bytes to topic:%s partition:%i*****\n",
buf_len, rd_kafka_topic_name(m_topic), m_partition);
#else
LOG(INFO) << "buf_len " << buf_len << " m_partition " << rd_kafka_topic_name(m_topic)<< " m_partition " << m_partition;
#endif
rd_kafka_poll(m_handler, 0);
return __SUCCESS__;
}
int main()
{
char test_data[100];
strcpy(test_data, "big dragon");
bigDragonPlatformMgr* producer = new bigDragonPlatformMgr;
if (__SUCCESS__ == producer->init(0, "10.0.1.211:9092", "test")) {
printf("producer init success\n");
} else {
printf("producer init failed\n");
return 0;
}
while (fgets(test_data, sizeof(test_data), stdin)) {
size_t len = strlen(test_data);
if (test_data[len - 1] == '\n')
test_data[--len] = '\0';
if (strcmp(test_data, "end") == 0)
break;
if (__SUCCESS__ == producer->sendData(test_data, strlen(test_data)))
printf("push data success %s\n", test_data);
else
printf("push data failed %s\n", test_data);
}
producer->destroy();
return 0;
}