chatgpt大模型
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.

74 lines
2.9 KiB

  1. #coding:utf8
  2. import traceback
  3. from pykafka import KafkaClient
  4. # from pykafka import partitioners
  5. # from pykafka.simpleconsumer import OwnedPartition, OffsetType
  6. import json
  7. from tqdm import tqdm
  8. # from kafka import KafkaProducer
  9. from pykafka.simpleconsumer import OwnedPartition, OffsetType
  10. def send_kafka(data,logging):
  11. try:
  12. producer = None
  13. # client = KafkaClient(hosts='172.26.28.30:9092', socket_timeout_ms=10 * 1000)
  14. topic = 'analyze'
  15. # producer = client.topics[topic].get_sync_producer(**{'max_request_size': 3000012 * 5})
  16. #producer = client.topics[topic].get_producer(sync=True)
  17. client = KafkaClient(hosts='172.26.28.30:9092', socket_timeout_ms=10 * 1000)
  18. # topic = client.topics['analyze']
  19. producer = client.topics[topic].get_producer()
  20. data1=json.dumps(data,ensure_ascii=False)
  21. producer.produce(bytes(data1, encoding='utf-8'))
  22. # kafkaProduce(topic,bytes(data1, encoding='utf-8'))
  23. logging.info("数据推入kafka!")
  24. except Exception as e:
  25. logging.info(traceback.format_exc())
  26. logging.info('写入kafka失败')
  27. # def kafkaProduce(topic,resultData):
  28. # producer = KafkaProducer(bootstrap_servers = '{}'.format("172.26.28.30:9092"))
  29. # topics = topic.split(',')
  30. # for tc in topics:
  31. # future = producer.send(tc,resultData)
  32. # producer.flush()
  33. def consumer():
  34. # topic = 'ais_caiji_kg_210'.encode('utf-8')
  35. # client = KafkaClient(hosts='172.16.3.153:9092,172.16.3.154:9092,172.16.3.155:9092')
  36. # topic = 'test_mysql_topic'.encode('utf-8')
  37. # client = KafkaClient(hosts='localhost:9092')
  38. # topic = client.topics[topic]
  39. # consumer = topic.get_simple_consumer(consumer_group='test1',
  40. # auto_commit_enable=True, # 去重消费
  41. # auto_commit_interval_ms=1000,
  42. # # consumer_id='test1', # 消费者ID
  43. # reset_offset_on_start=True,
  44. # # auto_offset_reset=OffsetType.LATEST,
  45. # consumer_timeout_ms=100000)
  46. # c = 0
  47. # for msg in consumer:
  48. # c += 1
  49. # if msg:
  50. # val = msg.value.decode('utf-8')
  51. # print(c,val)
  52. # client = KafkaClient(hosts='localhost:9092')
  53. # topic = client.topics['test_mysql_topic']
  54. client = KafkaClient(hosts='172.26.28.30:9092')
  55. topic = client.topics['analyze']
  56. consumer = topic.get_simple_consumer(consumer_group='my_consumer_group',
  57. auto_offset_reset=OffsetType.LATEST,
  58. reset_offset_on_start=True)
  59. # 消费数据
  60. for message in consumer:
  61. if message is not None:
  62. print(message.offset, message.value.decode())
  63. if __name__=="__main__":
  64. # send_kafka()
  65. consumer()