如何在卡夫卡出版词典?

2024-04-20 05:36:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在卡夫卡的一个主题上发表一个回应。此响应是从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())

我得到以下错误:

^{pr2}$

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


Tags: producerkafkafromimportclientdatetimemongodbcount