python操作kafka-python操作kafka(confluent_kafka 生产)

#!/usr/bin/python

# -*- coding:utf-8 -*-

from confluent_kafka import Producer

import json

import time

import sys

def delivery_report(err, msg):

""" Called once for each message produced to indicate delivery result.

Triggered by poll() or flush(). """

if err is not None:

print("Message delivery failed: {}".format(err))

else:

print("Message delivered to {} [{}]".format(msg.topic(), msg.partition()))

def c_kafka(topic, payloads, sleep_time=0, **conf):

gu = []

p = Producer(**conf)

for i in payloads:

gu.insert(0, "ping%s" % int(round(int(time.time() * 1000))))

p.poll(0)

p.produce(topic, json.dumps(i).encode("utf-8"), callback=delivery_report)

if sleep_time > 0:

p.flush()

time.sleep(int(sleep_time))

if sleep_time == 0:

你可能感兴趣的:(python操作kafka-python操作kafka(confluent_kafka 生产))