Golang 与Python 连接kafka

1. 安装

pythongolang
pip install kafka-python

git clone https://github.com/edenhill/librdkafka.git

cd  librdkafka

./configure --prefix=/usr

make && make install

profile:

      export PKG_CONFIG_PATH=/usr/lib/pkgconfig

go get -u github.com/confluentinc/confluent-kafka-go/kafka

编程Producer 连接

Python 编程                                                      Golang编程

from kafka import KafkaProducer     import github.com/confluentinc/confluent-kafka-go/kafka
p = KafkaProducer(                   p,err := kafka.NewProducer(&kafka.ConfigMap{
 bootstrap_servers="master:9092"             "bootstrap.servers": "master"
)                                    })

Producer发送消息:

     Python编程                                                               Golang编程(异步发送)

msg = "send msg"                                msg := "send msg"
topic = "job"                                   topic := "job"
p.send(topic, msg)                              diverchan := make(chan kafka.Event)
                                                p.Produce(&kafka.Message{
                                                  TopicPartition: kafka.TopicPartition{
                                                          Topic: &topic,
                                                          Partition:kafka.PartitionAny
                                                  
                                                  },
                                                  Value: []byte(msg)
                                                 }, diverchan)
                                                 e := <- diverchan
                                                 et := e.(*kafka.Message)
                                                 if et.TopicPartition.Error == nil {
                                                            "send successfully"
                                                  }
                                                  close(diverchan)

Consumer编程:

   Python编程                                                                      Golang编程

from kafka import KafkaConsumer                   c,er:=kafka.NewConsumer(&kafka.ConfigMap{
c = KafkaConsumer("job",                                    "bootstrap.servers": "master",
    bootstrap_servers=["master:9092"]                       "auto.offset.reset": "earliest"
)                                                  })  
for msg in c:                                      c.SubcribeTopics([]string{"job"},nil)
    print msg                                      while true:
                                                        data := c.ReadMessage(-1)
                                                        print data
                                                    c.Close()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值