|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | +import json |
| 4 | +import os |
| 5 | +import typing |
| 6 | +import azure.functions as func |
| 7 | +from azure.eventhub import EventHubProducerClient, EventData |
| 8 | + |
| 9 | +app = func.FunctionApp() |
| 10 | + |
| 11 | + |
| 12 | +# This is an actual EventHub trigger which handles Eventhub events in batches. |
| 13 | +# It serializes multiple event data into a json and store it into a blob. |
| 14 | +@app.function_name(name="eventhub_multiple") |
| 15 | +@app.event_hub_message_trigger( |
| 16 | + arg_name="events", |
| 17 | + event_hub_name="python-worker-ci-eventhub-batch", |
| 18 | + connection="AzureWebJobsEventHubConnectionString", |
| 19 | + data_type="string", |
| 20 | + cardinality="many") |
| 21 | +@app.write_table(arg_name="$return", |
| 22 | + connection="AzureWebJobsStorage", |
| 23 | + table_name="EventHubBatchTest") |
| 24 | +def eventhub_multiple(events): |
| 25 | + table_entries = [] |
| 26 | + for event in events: |
| 27 | + json_entry = event.get_body() |
| 28 | + table_entry = json.loads(json_entry) |
| 29 | + table_entries.append(table_entry) |
| 30 | + |
| 31 | + table_json = json.dumps(table_entries) |
| 32 | + |
| 33 | + return table_json |
| 34 | + |
| 35 | + |
| 36 | +# An HttpTrigger to generating EventHub event from EventHub Output Binding |
| 37 | +@app.function_name(name="eventhub_output_batch") |
| 38 | +@app.write_event_hub_message(arg_name="$return", |
| 39 | + connection="AzureWebJobsEventHubConnectionString", |
| 40 | + event_hub_name="python-worker-ci-eventhub-batch") |
| 41 | +@app.route(route="eventhub_output_batch", binding_arg_name="out") |
| 42 | +def eventhub_output_batch(req: func.HttpRequest, out: func.Out[str]) -> str: |
| 43 | + events = req.get_body().decode('utf-8') |
| 44 | + out.set('hello') |
| 45 | + return events |
| 46 | + |
| 47 | + |
| 48 | +# Retrieve the event data from storage blob and return it as Http response |
| 49 | +@app.function_name(name="get_eventhub_batch_triggered") |
| 50 | +@app.route(route="get_eventhub_batch_triggered/{id}") |
| 51 | +@app.read_table(arg_name="testEntities", |
| 52 | + connection="AzureWebJobsStorage", |
| 53 | + table_name="EventHubBatchTest", |
| 54 | + partition_key="{id}") |
| 55 | +def get_eventhub_batch_triggered(req: func.HttpRequest, testEntities): |
| 56 | + return func.HttpResponse(status_code=200, body=testEntities) |
| 57 | + |
| 58 | + |
| 59 | +# Retrieve the event data from storage blob and return it as Http response |
| 60 | +@app.function_name(name="get_metadata_batch_triggered") |
| 61 | +@app.route(route="get_metadata_batch_triggered") |
| 62 | +@app.read_blob(arg_name="file", |
| 63 | + path="python-worker-tests/test-metadata-batch-triggered.txt", |
| 64 | + connection="AzureWebJobsStorage") |
| 65 | +def get_metadata_batch_triggered(req: func.HttpRequest, |
| 66 | + file: func.InputStream) -> str: |
| 67 | + return func.HttpResponse(body=file.read().decode('utf-8'), |
| 68 | + status_code=200, |
| 69 | + mimetype='application/json') |
| 70 | + |
| 71 | + |
| 72 | +# This is an actual EventHub trigger which handles Eventhub events in batches. |
| 73 | +# It serializes multiple event data into a json and store it into a blob. |
| 74 | +@app.function_name(name="metadata_multiple") |
| 75 | +@app.event_hub_message_trigger( |
| 76 | + arg_name="events", |
| 77 | + event_hub_name="python-worker-ci-eventhub-batch-metadata", |
| 78 | + connection="AzureWebJobsEventHubConnectionString", |
| 79 | + data_type="binary", |
| 80 | + cardinality="many") |
| 81 | +@app.write_blob(arg_name="$return", |
| 82 | + path="python-worker-tests/test-metadata-batch-triggered.txt", |
| 83 | + connection="AzureWebJobsStorage") |
| 84 | +def metadata_multiple(events: typing.List[func.EventHubEvent]) -> bytes: |
| 85 | + event_list = [] |
| 86 | + for event in events: |
| 87 | + event_dict: typing.Mapping[str, typing.Any] = { |
| 88 | + 'body': event.get_body().decode('utf-8'), |
| 89 | + 'enqueued_time': event.enqueued_time.isoformat(), |
| 90 | + 'partition_key': event.partition_key, |
| 91 | + 'sequence_number': event.sequence_number, |
| 92 | + 'offset': event.offset, |
| 93 | + 'metadata': event.metadata |
| 94 | + } |
| 95 | + event_list.append(event_dict) |
| 96 | + |
| 97 | + return json.dumps(event_list) |
| 98 | + |
| 99 | + |
| 100 | +# An HttpTrigger to generating EventHub event from azure-eventhub SDK. |
| 101 | +# Events generated from azure-eventhub contain the full metadata. |
| 102 | +@app.function_name(name="metadata_output_batch") |
| 103 | +@app.route(route="metadata_output_batch") |
| 104 | +def main(req: func.HttpRequest): |
| 105 | + # Get event count from http request query parameter |
| 106 | + count = int(req.params.get('count', '1')) |
| 107 | + |
| 108 | + # Parse event metadata from http request |
| 109 | + json_string = req.get_body().decode('utf-8') |
| 110 | + event_dict = json.loads(json_string) |
| 111 | + |
| 112 | + # Create an EventHub Client and event batch |
| 113 | + client = EventHubProducerClient.from_connection_string( |
| 114 | + os.getenv('AzureWebJobsEventHubConnectionString'), |
| 115 | + eventhub_name='python-worker-ci-eventhub-batch-metadata') |
| 116 | + |
| 117 | + # Generate new event based on http request with full metadata |
| 118 | + event_data_batch = client.create_batch() |
| 119 | + random_number = int(event_dict.get('body', '0')) |
| 120 | + for i in range(count): |
| 121 | + event_data_batch.add(EventData(str(random_number + i))) |
| 122 | + |
| 123 | + # Send out event into event hub |
| 124 | + with client: |
| 125 | + client.send_batch(event_data_batch) |
| 126 | + |
| 127 | + return 'OK' |
0 commit comments