Giter Club home page Giter Club logo

mwkafka's Introduction

基于librdkafka封装

可以指定分区生产和消费,可以指定offset消费

使用例子

//消费回调

void consum_on_msg(void* pInvoker, const MessagePtr& pMsg) {

printf("date: %s\n", pMsg->data.c_str()); printf("status: %d\n", pMsg->status); printf("errcode: %d\n", pMsg->errcode); printf("errmsg: %s\n", pMsg->errmsg.c_str()); printf("partition: %d\n", pMsg->partition); printf("offset: %ld\n", pMsg->offset); printf("\n");

}

void test_consumer() { mwKafka_Consumer consume(brokers, group_id);

consume.AddCallBack(CONSUMED, consum_on_msg);//添加消费回调 consume.AddCallBack(ONERROR, consum_on_err);//添加错误回调

//bool bRet = consume.Subscribe({ { "TEST_08",1,1000},{"TEST_08",2,5000 } },true);//开始订阅,指定分区 和>offset bool bRet = consume.Subscribe({ "TEST_08" });//开始订阅

if (!bRet) { printf("%s\n", "Subscribe err"); } while (true) { consume.Poll(500); //循环消费所有 }

}

//生产

void producer_on_msg(void* pInvoker, const MessagePtr& pMsg) { /printf("date: %s\n", pMsg->data.c_str()); printf("status: %d\n", pMsg->status); printf("errcode: %d\n", pMsg->errcode); printf("errmsg: %s\n", pMsg->errmsg.c_str()); printf("partition: %d\n", pMsg->partition); printf("offset: %ld\n", pMsg->offset); printf("\n");/

printf("%ld\n", ++_offset); }

void sigle_producer()//单线程提交 { mwKafka_Producer producer(brokers, true); producer.AddCallBack(DELIVERED, producer_on_msg);

producer.AddTopics( topic_nameg);

const char* text = u8"阿萨德接电话爱仕达按实际活动即可拉伸活动空间来说都阿萨德好可怜爱仕达卢卡斯";

int pos = 100000;

while (pos-- > 0) { std::string _text = text + std::to_string(pos); producer.Produce(topic_nameg, _text); }

while (true) { producer.Poll(500); }

}

mwkafka's People

Contributors

ylaoda avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.