用kafka接收消息
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

92 líneas
3.5KB

  1. from kafka import KafkaProducer, KafkaConsumer
  2. from kafka.errors import kafka_errors
  3. import traceback
  4. import json
  5. import time
  6. import random,string
  7. def producer_demo():
  8. cnt_online=1;cnt_offline=0
  9. Tecent=False;
  10. #topic_on='thsw';topic_off='thsw2';
  11. server=['212.129.223.66:19092'];
  12. #server=['101.132.127.1:19092']
  13. topic_on='alg-online-tasks';topic_off='alg-offline-tasks'
  14. # 假设生产的消息为键值对(不是一定要键值对),且序列化方式为json
  15. producer = KafkaProducer(
  16. bootstrap_servers=server,#tencent yun
  17. value_serializer=lambda v: v.encode('utf-8'))
  18. # 发送三条消息
  19. if not Tecent:
  20. #pull_channel = "rtmp://live.play.t-aaron.com/live/THSA"
  21. #push_channel = 'rtmp://live.push.t-aaron.com/live/THSB'
  22. #pull_channel = 'rtmp://live.play.t-aaron.com/live/THSAa_hd'
  23. pull_channel = 'http://live.play.t-aaron.com/live/THSAa_hd.m3u8'
  24. push_channel = "rtmp://live.push.t-aaron.com/live/THSBa"
  25. else:
  26. pull_channel = "rtmp://demoplay.yunhengzhizao.cn/live/THSA_HD5M"
  27. push_channel = "rtmp://127.0.0.1:1935/live/test"
  28. #pull_channel = 'rtmp://live.play.t-aaron.com/live/THSAa'
  29. #push_channel = 'rtmp://127.0.0.1:1975/live/test'
  30. for i in range(0, cnt_online):
  31. time.sleep(0.0001)
  32. #''.join(random.sample(string.ascii_letters ,16) )
  33. msg_dict = {
  34. "biz_id": "hehuzhang",
  35. "mod_id": "ai",
  36. "msg_id": 'bb'+''.join(random.sample(string.ascii_letters ,30) ) ,
  37. #"pull_channel": "rtmp://live.play.t-aaron.com/live/THSA",
  38. #"push_channel": "rtmp://live.push.t-aaron.com/live/THSB",
  39. "pull_channel": pull_channel,
  40. "push_channel": push_channel,
  41. 'channel_code':'LC001',
  42. "results_base_dir": "XJRW202111291703"+str(random.randint(10,99)),
  43. }
  44. msg = json.dumps(msg_dict)
  45. #msg = msg_dict
  46. future = producer.send(
  47. topic_on,
  48. #key='count_num', # 同一个key值,会被送至同一个分区
  49. msg
  50. )
  51. print('online send {}'.format(str(i)))
  52. try:
  53. future.get(timeout=10) # 监控是否发送成功
  54. except kafka_errors: # 发送失败抛出kafka_errors
  55. traceback.format_exc()
  56. for i in range(0, cnt_offline):
  57. msg_dict = {
  58. "biz_id":"hehuzhang",
  59. "mod_id":"ai",
  60. 'channel_code':'LC001',
  61. "msg_id":'bb'+''.join(random.sample(string.ascii_letters ,30) ) ,
  62. "offering_id":"http://vod.play.t-aaron.com/customerTrans/c49a2c620795d124f2ae4b10197b8d0e/303b7a58-17f3ef4494e-0004-f90c-f2c-7ec68.mp4",
  63. #"offering_id":"http://vod.play.t-aaron.com/customerTrans/c49a2c620795d124f2ae4b10197b8d0e/61921b16-1807435dfa3-0004-f90c-f2c-7ec68.mp4",
  64. "offering_type":"mp4",
  65. "results_base_dir": "XJRW202203171535"+str(random.randint(10,99)),
  66. }
  67. msg = json.dumps(msg_dict)
  68. #msg = msg_dict
  69. future = producer.send(topic_off,msg)
  70. print('offline send {}'.format(str(i)))
  71. try:
  72. future.get(timeout=10)
  73. except kafka_errors:
  74. traceback.format_exc()
  75. if __name__=='__main__':
  76. producer_demo()