代码之家  ›  专栏  ›  技术社区  ›  hasherBaba

如何在卡夫卡出版词典?

  •  1
  • hasherBaba  · 技术社区  · 6 年前

    我想发表一篇关于卡夫卡主题的回复。此响应是从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中,不会发生此错误。

    1 回复  |  直到 5 年前
        1
  •  1
  •   Mohammad Rahmati    6 年前

    我面对这个问题,我用以下两种方法解决了它。

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

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