telegram 群组监控 / 群组功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.8 KiB

8 months ago
  1. from kafka import KafkaProducer, KafkaConsumer
  2. from config import logger
  3. class SKafka():
  4. def __init__(self, bootstrap_servers: str, api_version=None, is_json_schema: bool = False,encoding: str = "utf-8",
  5. security_protocol: str = "PLAINTEXT", sasl_mechanism: str = None,
  6. sasl_plain_username: str = None, sasl_plain_password: str = None):
  7. self.is_json_schema = is_json_schema
  8. self.encoding = encoding
  9. self.producer = KafkaProducer(bootstrap_servers=bootstrap_servers,api_version=api_version,
  10. security_protocol=security_protocol,sasl_mechanism=sasl_mechanism,
  11. sasl_plain_username=sasl_plain_username,sasl_plain_password=sasl_plain_password)
  12. def sync_producer(self, topic, message):
  13. count = 0
  14. for date in message:
  15. if date:
  16. date = date.encode()
  17. self.producer.send(topic, date)
  18. count += 1
  19. logger.info('push {} success,date length:{} vail date length: {} '.format(topic, len(message), count))
  20. # self.producer.flush()
  21. def close_producer(self):
  22. try:
  23. self.producer.close()
  24. print("connect close")
  25. except Exception as e:
  26. print(e)
  27. pass
  28. # kafka设置了密码,没有就不用设置那么多
  29. if __name__ == '__main__':
  30. # kp = SKafka(bootstrap_servers='172.18.1.119:9992')
  31. hosts = "172.18.1.119:9992" # 连接hosts
  32. topic = "dazhongdianping"
  33. # producer = KafkaProducer(bootstrap_servers='172.18.1.119:9992')
  34. consumer = KafkaConsumer('dazhongdianping', bootstrap_servers='172.18.1.119:9992')
  35. for message in consumer:
  36. # 解析并处理消息
  37. print(f"Received message: {message.value.decode('utf-8')}")