用kafka接收消息
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.

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