python - 使用 Python 为 Kafka 加载 csv
问题描述
我正在尝试从 csv 文件中随机加载和分配主题(5 个主题)信息。这是我当前的代码。如何让这个工作?
pconf = {
'bootstrap.servers': brokers,
'partitioner': 'random',
'key.serializer': StringSerializer('utf_8')
}
producer = SerializingProducer(pconf)
now = datetime.now()
current_time = now.strftime("%d/%m/%Y %H:%M:%S")
f = open('data.csv', 'r')
with f:
reader = csv.reader(f)
for row in reader:
try:
for row in range(1000):
producer.produce(random.choice(topics), value=row)
sleep(5)
except KeyboardInterrupt:
sys.stderr.write('%% Aborted by user\n')
except BufferError:
sys.stderr.write('%% Local producer queue is full (%d messages awaiting delivery): try again\n' % len(p))
我收到错误:TypeError:需要一个类似字节的对象,而不是“int”
解决方案
推荐阅读
- json - 将 Json 字段从 SQL 返回到 ASP.NET Core API
- html - 为什么即使我指定了 DL 标签的宽度,它也会使用所有空间?
- c# - 如何将字符串转换为类型?
- json - Swift:将字典转换为 JSON 以用于多部分/表单数据
- c# - 我可以使用 Accord.Net 进行并发 cobyla 优化吗?
- python - `poetry lock` 和 `poetry update --lock` 有什么区别?
- list - 如果找不到其中一个包,如何防止 apt/apt-get 取消下载多个包
- javascript - Vega-Lite 工具提示未以 UTC 显示时间
- java - 单击 RecyclerView 后,当我想在新活动中显示详细信息时出现错误。使用 Firebase
- c++ - 与 CMake 链接后的“未定义引用”