Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

117 lines
4.2KB

  1. from kafka import KafkaProducer, KafkaConsumer
  2. from kafka.errors import kafka_errors
  3. import traceback
  4. import json
  5. import time
  6. def producer_demo():
  7. # 假设生产的消息为键值对(不是一定要键值对),且序列化方式为json
  8. producer = KafkaProducer(
  9. bootstrap_servers=['101.132.127.1:19092'],
  10. key_serializer=lambda k: json.dumps(k).encode(),
  11. value_serializer=lambda v: json.dumps(v).encode(),
  12. )
  13. # 发送三条消息
  14. for i in range(0, 3):
  15. future = producer.send(
  16. 'alg-task-results',
  17. key='count_num', # 同一个key值,会被送至同一个分区
  18. value=str(i)) # 向分区1发送消息
  19. print("send {}".format(str(i)))
  20. try:
  21. future.get(timeout=10) # 监控是否发送成功
  22. except Exception as e: # 发送失败抛出kafka_errors
  23. print(e)
  24. producer = KafkaProducer(
  25. bootstrap_servers=['101.132.127.1:19092'],
  26. key_serializer=lambda k: json.dumps(k).encode(),
  27. value_serializer=lambda v: json.dumps(v).encode(),
  28. )
  29. future = producer.send(
  30. 'alg-task-results',
  31. key='count_num', # 同一个key值,会被送至同一个分区
  32. value=str(i)) # 向分区1发送消息
  33. try:
  34. future.get() # 监控是否发送成功
  35. print("re send {}".format(str(i)))
  36. except Exception as e:
  37. print('resend error:',e)
  38. for ii in range(30):
  39. time.sleep(10)
  40. print('sleep %d s'%(ii*10))
  41. for i in range(0, 3):
  42. future = producer.send(
  43. 'alg-task-results',
  44. key='count_num', # 同一个key值,会被送至同一个分区
  45. value=str(i)) # 向分区1发送消息
  46. print("send {}".format(str(i)))
  47. try:
  48. future.get() # 监控是否发送成功
  49. except Exception as e: # 发送失败抛出kafka_errors
  50. print(e)
  51. producer = KafkaProducer(
  52. bootstrap_servers=['101.132.127.1:19092'],
  53. key_serializer=lambda k: json.dumps(k).encode(),
  54. value_serializer=lambda v: json.dumps(v).encode(),
  55. )
  56. future = producer.send(
  57. 'alg-task-results',
  58. key='count_num', # 同一个key值,会被送至同一个分区
  59. value=str(i)) # 向分区1发送消息
  60. try:
  61. future.get() # 监控是否发送成功
  62. print("re send {}".format(str(i)))
  63. except Exception as e:
  64. print('resend error:',e)
  65. def prod():
  66. producer = KafkaProducer(
  67. bootstrap_servers=['101.132.127.1:19092'],
  68. key_serializer=lambda k: json.dumps(k).encode(),
  69. metadata_max_age_ms=120000,
  70. value_serializer=lambda v: json.dumps(v).encode()
  71. )
  72. # 发送三条消息
  73. for i in range(0, 3):
  74. future = producer.send(
  75. 'alg-task-results',
  76. key='count_num', # 同一个key值,会被送至同一个分区
  77. value=str(i)) # 向分区1发送消息
  78. print("send {}".format(str(i)))
  79. try:
  80. future.get() # 监控是否发送成功
  81. except kafka_errors: # 发送失败抛出kafka_errors
  82. traceback.format_exc()
  83. for ii in range(30):
  84. time.sleep(10)
  85. print('sleep %d s'%(ii*10))
  86. for i in range(0, 3):
  87. future = producer.send(
  88. 'alg-task-results',
  89. key='count_num', # 同一个key值,会被送至同一个分区
  90. value=str(i)) # 向分区1发送消息
  91. print("send {}".format(str(i)))
  92. try:
  93. future.get() # 监控是否发送成功
  94. except Exception as e:
  95. print('resend error:',e)
  96. if __name__=='__main__':
  97. print('########demo1 2nd############')
  98. prod()
  99. print('########demo2 1st############')
  100. producer_demo()