通过pip库的kafka来实现
from kafka import KafkaProducer
class SKafka():
def __init__(self, bootstrap_servers: str, api_version=None, is_json_schema: bool = False,
encoding: str = "utf-8", security_protocol: str = "PLAINTEXT", sasl_mechanism: str = None,
sasl_plain_username: str = None, sasl_plain_password: str = None):
self.is_json_schema = is_json_schema
self.encoding = encoding
self.producer = KafkaProducer(bootstrap_servers=bootstrap_servers,
api_version=api_version,
security_protocol=security_protocol,
sasl_mechanism=sasl_mechanism,
sasl_plain_username=sasl_plain_username,
sasl_plain_password=sasl_plain_password)
def sync_producer(self, topic, message):
for date in message:
date = date.encode()
self.producer.send(topic, date)
# print('save success,date:{}'.format(date))
def close_producer(self):
try:
self.producer.close()
print("connect close")
except Exception as e:
print(e)
pass
#kafka设置了密码,没有就不用设置那么多
if __name__ == '__main__':
kp = SKafka(bootstrap_servers='IP:9092',
api_version=(0, 10, 1),
security_protocol='SASL_PLAINTEXT',
sasl_mechanism='PLAIN',
sasl_plain_username='user',
sasl_plain_password='password')
kp.sync_producer(topic='test', message=messages)
kp.close_producer()