""" return AccessToken(self.token, self.expiry) # Target namespace and hub must also be specified. Consumer group is set to default unless required otherwise. FULLY_QUALIFIED_NAMESPACE = os.environ['EVENT_HUB_HOSTNAME'] EVENTHUB_NAME = os.environ['EVENT_HUB_NAME'] CONSUMER_GROUP = "$Default" # The following part creates a SAS token. Users can use any way to create a SAS token. SAS_POLICY = os.environ['EVENT_HUB_SAS_POLICY'] SAS_KEY = os.environ['EVENT_HUB_SAS_KEY'] uri = "sb://{}/{}".format(FULLY_QUALIFIED_NAMESPACE, EVENTHUB_NAME) token_ttl = 3000 # seconds sas_token = generate_sas_token(uri, SAS_POLICY, SAS_KEY, token_ttl) # end of creating a SAS token consumer_client = EventHubConsumerClient( fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE, eventhub_name=EVENTHUB_NAME, consumer_group=CONSUMER_GROUP, credential=CustomizedSASCredential(sas_token, time.time() + token_ttl), logging_enable=True) with consumer_client: consumer_client.receive( lambda pc, event: print(pc.partition_id, ":", event), starting_position=-1)
""" This method is automatically called when token is about to expire. """ return AccessToken(self.token, self.expiry) # Target namespace and hub must also be specified. Consumer group is set to default unless required otherwise. FULLY_QUALIFIED_NAMESPACE = os.environ['EVENT_HUB_HOSTNAME'] EVENTHUB_NAME = os.environ['EVENT_HUB_NAME'] CONSUMER_GROUP = "$Default" # The following part creates a SAS token. Users can use any way to create a SAS token. SAS_POLICY = os.environ['EVENT_HUB_SAS_POLICY'] SAS_KEY = os.environ['EVENT_HUB_SAS_KEY'] uri = "sb://{}/{}".format(FULLY_QUALIFIED_NAMESPACE, EVENTHUB_NAME) token_ttl = 3000 # seconds sas_token = generate_sas_token(uri, SAS_POLICY, SAS_KEY, token_ttl) # end of creating a SAS token consumer_client = EventHubConsumerClient( fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE, eventhub_name=EVENTHUB_NAME, consumer_group=CONSUMER_GROUP, credential=CustomizedSASCredential(sas_token, time.time() + token_ttl), logging_enable=True) with consumer_client: consumer_client.receive(on_event, starting_position=-1)
#read config file config = json.loads(open('config.json','r').read()) #App details and further config tenantID = config["tenantID"] clientID = config["clientID"] clientSecret = config["clientSecret"] ehaddress = config["eventhubNamespaceURI"] #needs to be in FQDN format without protocal prefix, e.g. "myeventhubnamespace.servicebus.windows.net" eventhub_name = "mitest" consumer_group = '$Default' #create identity identity = ClientSecretCredential(tenant_id=tenantID, client_id=clientID, client_secret=clientSecret ) #create client object for consumer ehclient = EventHubConsumerClient(credential= identity, fully_qualified_namespace=ehaddress, eventhub_name=eventhub_name, consumer_group = consumer_group) #funtion that needs to be implemented and passed as a parameter to receive() def on_event(partition_context, event): print("Received event from partition {}".format(partition_context.partition_id)) print(event.body_as_str()) partition_context.update_checkpoint(event) ehclient.receive( on_event=on_event, starting_position="-1", # "-1" is from the beginning of the partition. ) # receive events from specified partition: # client.receive(on_event=on_event, partition_id='0')