如何在Kafka中发布字典? [英] How to publish dictionary in Kafka?

查看:113
本文介绍了如何在Kafka中发布字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发布有关kafka主题的回复.此响应是从mongodb获取的.

I am trying to publish a response in a kafka topic. This response is fetched from a mongodb.

from kafka import KafkaProducer
from kafka.errors import KafkaError
import json
import pymongo
from pymongo import MongoClient
import sys
import datetime

try:
    client = MongoClient('mongodb://A.B.C.D:27017/prod-production')
    db = client["prod-production"]
except Exception as e:
    print("Error occurred while connecting to DB")
    print(e)
producer = KafkaProducer(bootstrap_servers=['localhost:9092'])
producer = KafkaProducer(retries=5)
print("Initial time:")
print(datetime.datetime.now())
count = 1
for response in db.Response.find():
    if count >= 20:
        producer.flush()
        sys.exit()
    count += 1
    print(count)
    producer.send('example-topic', bytes(response))
print("Final time")
print(datetime.datetime.now())

我遇到以下错误:

Traceback (most recent call last):   File "producer.py", line 28, in <module>
    producer.send('collect-production-response', bytes(response)) TypeError: 'str' object cannot be interpreted as an integer

但是,在python2中,不会发生此错误.

However, in python2, this error does not occur.

推荐答案

我遇到了这个问题,并通过以下两种方法解决了这个问题.

I was faced with this problem and I solved it with the two following method.

1- producer.send('example-topic', bytes(str(response), 'utf-8'))

2- producer.send('example-topic', str(response))

这篇关于如何在Kafka中发布字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆