用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.

120 lines
3.6KB

  1. import numpy as np
  2. import base64
  3. import io,os
  4. import requests
  5. import time,json
  6. import string,random
  7. import glob,string,sys
  8. import oss2,copy
  9. from kafka import KafkaProducer, KafkaConsumer
  10. from kafka.errors import kafka_errors
  11. msg_dict_off={
  12. "msg_id":"bblvgyntTsZCamqjuLArkiSYIbKXEeWx",#消息ID标识
  13. "biz_id":"hehuzhang",#业务标识
  14. "mod_id":"ai",#模型标识
  15. "status":"running",#任务状态
  16. "type":str(1),#数据类型:1图片 2视频
  17. "error":str(9999),#错误信息
  18. "results":[#问题结果
  19. {
  20. "original_url":"",#原图地址
  21. "sign_url":"",#AI标记地址
  22. "category_id":"",#分类标识
  23. "description":"",#问题描述
  24. "time":"",#时间戳
  25. }
  26. ]
  27. }
  28. def test5(par):
  29. indir,outdir,logdir,jsonDir = par['indir'],par['outdir'],par['logdir'],par['jsonDir']
  30. videoBakDir,ossPar,vodPar,kafkaPar = par['videoBakDir'], par['ossPar'],par['vodPar'],par['kafkaPar']
  31. '''producer = KafkaProducer(
  32. bootstrap_servers=kafkaPar['boostServer'],#tencent yun
  33. value_serializer=lambda v: v.encode('utf-8'),
  34. #request_timeout_ms=3,
  35. #connections_max_idle_ms=60000
  36. )'''
  37. producer = KafkaProducer(
  38. bootstrap_servers=['101.132.127.1:19092'],
  39. key_serializer=lambda k: json.dumps(k).encode(),
  40. value_serializer=lambda v: json.dumps(v).encode())
  41. #while True:
  42. for i in range(5):
  43. for j in range(10):
  44. msg = copy.deepcopy(msg_dict_off)
  45. biz_id = 'i-%d-j-%d'%(i,j)
  46. print(biz_id,msg['biz_id'])
  47. msg['biz_id'] = biz_id
  48. msg = json.dumps(msg, ensure_ascii=False)
  49. '''future = producer.send(
  50. kafkaPar['topic'],
  51. msg
  52. )'''
  53. future = producer.send(
  54. 'alg-task-results',
  55. key='count_num', # 同一个key值,会被送至同一个分区
  56. value=biz_id) # 向分区1发送消息
  57. try:
  58. record_metadata = future.get(timeout=30)
  59. print('-'*10,biz_id,' send')
  60. except Exception as e:
  61. print('#'*10,biz_id,' error:',str(e))
  62. for ii in range(30):
  63. time.sleep(10)
  64. print('sleep %d s'%(ii*10))
  65. for j in range(10,20):
  66. msg = copy.deepcopy(msg_dict_off)
  67. biz_id = 'i-%d-j%d'%(i,j)
  68. msg['biz_id'] = biz_id
  69. msg = json.dumps(msg, ensure_ascii=False)
  70. future = producer.send(
  71. 'alg-task-results',
  72. key='count_num', # 同一个key值,会被送至同一个分区
  73. value=biz_id) # 向分区1发送消息
  74. try:
  75. record_metadata = future.get(timeout=30)
  76. print('-'*10,biz_id,' send')
  77. except Exception as e:
  78. print('#'*10,biz_id,' error:',str(e))
  79. if __name__=='__main__':
  80. masterFile="conf/send_oss_debug.json"
  81. assert os.path.exists(masterFile)
  82. with open(masterFile,'r') as fp:
  83. par=json.load(fp)
  84. test5(par)