Guest User

Untitled

a guest
Nov 28th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. Msg offset: <azure.eventhub.common.Offset object at 0x102fc4e10>
  2. Msg seq: 0
  3. Msg body: 0
  4.  
  5. Received 1 messages in 0.11292386054992676 seconds
  6.  
  7. # pip install azure-eventhub
  8.  
  9. import logging
  10. import time
  11. from azure.eventhub import EventHubClient, Receiver, Offset
  12.  
  13. logger = logging.getLogger("azure")
  14.  
  15. # URL of the event hub, amqps://<mynamespace>.servicebus.windows.net/myeventhub
  16. ADDRESS = "amqps://chc-eh-ns.servicebus.windows.net/chc-eh"
  17.  
  18. # Access tokens for event hub namespace, from Azure portal for namespace
  19. USER = "RootManageSharedAccessKey"
  20. KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXX"
  21.  
  22. # Additional setup to receive events
  23. CONSUMER_GROUP = "$default" # our view of the event hub, useful when there is more than one consumer at same time
  24. PARTITION = "0" # which stream within event hub
  25. OFFSET = Offset("-1") # get all msgs in event hub. msgs are never removed, they just expire per event hub settings
  26. PREFETCH = 100 # not sure exactly what this does ??
  27.  
  28. # Initialize variables
  29. total = 0
  30. last_sn = -1
  31. last_offset = -1
  32.  
  33. client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
  34. try:
  35. receiver = client.add_receiver(CONSUMER_GROUP, PARTITION, prefetch=PREFETCH, offset=OFFSET)
  36. client.run()
  37. start_time = time.time()
  38. for event_data in receiver.receive(timeout=100):
  39. last_offset = event_data.offset
  40. last_sn = event_data.sequence_number
  41. print("Msg offset: " + str(last_offset))
  42. print("Msg seq: " + str(last_sn))
  43. print("Msg body: " + event_data.body_as_str())
  44. total += 1
  45.  
  46. end_time = time.time()
  47. client.stop()
  48. run_time = end_time - start_time
  49. print("nReceived {} messages in {} seconds".format(total, run_time))
  50.  
  51. except KeyboardInterrupt:
  52. pass
  53.  
  54. finally:
  55. client.stop()
  56.  
  57. # pip install azure-eventhub
  58.  
  59. import time
  60. from azure.eventhub import EventHubClient, Offset
  61.  
  62. # URL of the event hub, amqps://<mynamespace>.servicebus.windows.net/myeventhub
  63. ADDRESS = "amqps://chc-eh-ns.servicebus.windows.net/chc-eh"
  64.  
  65. # Access tokens for event hub namespace, from Azure portal for namespace
  66. USER = "RootManageSharedAccessKey"
  67. KEY = "XXXXXXXXXXXX"
  68.  
  69. # Additional setup to receive events
  70. CONSUMER_GROUP = "$default" # our view of the event hub, useful when there is more than one consumer at same time
  71. PARTITION = "0" # which stream within event hub
  72. OFFSET = Offset("-1") # get all msgs in event hub. msgs are never removed, they just expire per event hub settings
  73. PREFETCH = 100 # batch size ??
  74.  
  75. # Initialize variables
  76. total = 0
  77. last_sn = -1
  78. last_offset = -1
  79.  
  80. client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
  81. try:
  82. receiver = client.add_receiver(CONSUMER_GROUP, PARTITION, prefetch=PREFETCH, offset=OFFSET)
  83. client.run()
  84. start_time = time.time()
  85. batch = receiver.receive(timeout=5000)
  86. while batch:
  87. for event_data in batch:
  88. last_offset = event_data.offset
  89. last_sn = event_data.sequence_number
  90. print("Msg offset: " + str(last_offset))
  91. print("Msg seq: " + str(last_sn))
  92. print("Msg body: " + event_data.body_as_str())
  93. total += 1
  94. batch = receiver.receive(timeout=5000)
  95. end_time = time.time()
  96. client.stop()
  97. run_time = end_time - start_time
  98. print("nReceived {} messages in {} seconds".format(total, run_time))
  99.  
  100. except KeyboardInterrupt:
  101. pass
  102.  
  103. finally:
  104. client.stop()
Add Comment
Please, Sign In to add comment