Skip to content

Commit 460bfc2

Browse files
committed
added event hubs samples
1 parent 959a131 commit 460bfc2

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

azure-event-hubs.ipynb

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"name": "SQL",
5+
"display_name": "SQL",
6+
"language": "sql"
7+
},
8+
"language_info": {
9+
"name": "sql",
10+
"version": ""
11+
}
12+
},
13+
"nbformat_minor": 2,
14+
"nbformat": 4,
15+
"cells": [
16+
{
17+
"cell_type": "markdown",
18+
"source": [
19+
"# Send an Event to an Azure Event Hub\r\n",
20+
"\r\n",
21+
"Make sure to have an Azure Event Hub deployed in Azure to run the following samples. If you need help in creating your first Azure Event Hubs, please take a look here: [Quickstart: Create an event hub using Azure portal](https://learn.microsoft.com/azure/event-hubs/event-hubs-create). \r\n",
22+
"\r\n",
23+
"Event Hubs REST endpoint are documented here: [Event Hubs service REST](https://learn.microsoft.com/en-us/rest/api/eventhub/event-hubs-runtime-rest). Specifically in the next samples the [Send Event](https://learn.microsoft.com/en-us/rest/api/eventhub/send-event) API will be used.\r\n",
24+
"\r\n",
25+
"In the next samples is assumed that there is an Azure Event Hubs deployed at `https://azure-event-hubs.azurewebsites.net/`, that has a Event Hubs Instance named `myeventhub`. To have the samples working in your environment make sure adjust URL and the Event Hub Instance name so that they will match yours."
26+
],
27+
"metadata": {
28+
"azdata_cell_guid": "3f79ad9b-7872-479a-9813-26e34e49ba4f"
29+
},
30+
"attachments": {}
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"source": [
35+
"## Send Events using SAS Token\n",
36+
"\n",
37+
"Only authenticated requests can send events to Event Hubs. One way to authenticate a request is to provide a Shared Access Signature token: \n",
38+
"- [Authorizing access to Event Hubs resources using Shared Access Signatures](https://learn.microsoft.com/en-us/azure/event-hubs/authorize-access-shared-access-signature)\n",
39+
"- [Generate SAS token](https://learn.microsoft.com/en-us/rest/api/eventhub/generate-sas-token). \n",
40+
"\n",
41+
"At the moment is not possible to generate a SAS token directly from Azure SQL database, but you can put the code for generating such a token in an Azure Function and call it from Azure SQL database using `sp_invoke_external_rest_point` as well.\n",
42+
"\n",
43+
"Once you have the token you can add it into a Database Scoped Credential:"
44+
],
45+
"metadata": {
46+
"azdata_cell_guid": "ad7d9205-5d90-47aa-916a-56f9378a9fcc"
47+
},
48+
"attachments": {}
49+
},
50+
{
51+
"cell_type": "code",
52+
"source": [
53+
"-- make sure a database master key exists\r\n",
54+
"if not exists(select * from sys.symmetric_keys where [name] = '##MS_DatabaseMasterKey##') begin\r\n",
55+
" create master key encryption by password = 'LONg_Pa$$_w0rd!'\r\n",
56+
"end\r\n",
57+
"\r\n",
58+
"-- create database scoped credential\r\n",
59+
"create database scoped credential [https://azure-event-hubs.servicebus.windows.net]\r\n",
60+
"with identity = 'HTTPEndpointHeaders', \r\n",
61+
"secret = '{\"Authorization\": \"SharedAccessSignature sr=azure-event-hubs.servicebus.windows.net%2fmyeventhub&sig=RVDJM1cSo71j73%2bWR0t7ZCZukIjMEvBn%2bWWqSlqkJeM%3d&se=1697310598&skn=RootManageSharedAccessKey\"}';"
62+
],
63+
"metadata": {
64+
"azdata_cell_guid": "3fdacfa3-b5ac-4c3a-8cd3-e77f287cd992",
65+
"language": "sql",
66+
"tags": []
67+
},
68+
"outputs": [],
69+
"execution_count": null
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"source": [
74+
"You can then send messages to Event Hubs using the \"Send Event\" API, which is available at `https://azure-event-hubs.servicebus.windows.net/from-sql/messages` :"
75+
],
76+
"metadata": {
77+
"azdata_cell_guid": "5e6e4469-209d-4946-9546-a6acd793b82a"
78+
},
79+
"attachments": {}
80+
},
81+
{
82+
"cell_type": "code",
83+
"source": [
84+
"declare @payload nvarchar(max) = '{\"UserId\": \"6C5E29A2-A5E7-449D-BD14-259D61ADC6BE\", \"FirstName\": \"John\", \"LastName\": \"Doe\"}';\r\n",
85+
"declare @headers nvarchar(4000) = N'{\"BrokerProperties\": \"' + string_escape('{\"PartitionKey\": \"6C5E29A2-A5E7-449D-BD14-259D61ADC6BE\"}', 'json') + '\"}'\r\n",
86+
"declare @ret int, @response nvarchar(max)\r\n",
87+
"\r\n",
88+
"exec @ret = sp_invoke_external_rest_endpoint \r\n",
89+
" @url = 'https://azure-event-hubs.servicebus.windows.net/myeventhub/messages',\r\n",
90+
" @headers = @headers,\r\n",
91+
" @payload = @payload,\r\n",
92+
"\t\t@credential = [https://azure-event-hubs.servicebus.windows.net],\r\n",
93+
" @response = @response output;\r\n",
94+
"\r\n",
95+
"select @response;"
96+
],
97+
"metadata": {
98+
"azdata_cell_guid": "a87411a4-84c5-493d-9a84-dea4eeffa98b",
99+
"language": "sql",
100+
"tags": []
101+
},
102+
"outputs": [],
103+
"execution_count": null
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"source": [
108+
"## Send Events using Managed Identities\n",
109+
"\n",
110+
"Follow the instructions here: [Enable Managed Identity in Azure SQL](..\\..\\..\\_git_owned\\azure-sql-db-invoke-external-rest-endpoints\\azure-sql-enable-msi.ipynb) to make sure you have Managed Identity enabled for your Azure SQL database, and then check how to grant to right permission on Event Hubs to the Azure SQL Manage Identity, followin the instructions here: [Grant permissions to a managed identity in Azure AD](https://learn.microsoft.com/azure/event-hubs/authenticate-managed-identity?tabs=latest#grant-permissions-to-a-managed-identity-in-azure-ad).\n",
111+
"\n",
112+
"Once that is done you just need to create a Database Scoped Credentials with the string `Managed Identity` as identity and `https://eventhubs.azure.net` as the `resourceid`:"
113+
],
114+
"metadata": {
115+
"azdata_cell_guid": "0afadd91-d62b-4d30-80d2-b7f0c14753ec"
116+
},
117+
"attachments": {}
118+
},
119+
{
120+
"cell_type": "code",
121+
"source": [
122+
"-- make sure a database master key exists\r\n",
123+
"if not exists(select * from sys.symmetric_keys where [name] = '##MS_DatabaseMasterKey##') begin\r\n",
124+
" create master key encryption by password = 'LONg_Pa$$_w0rd!'\r\n",
125+
"end\r\n",
126+
"\r\n",
127+
"-- create database scoped credential\r\n",
128+
"if exists(select * from sys.database_scoped_credentials where [name] = 'https://azure-event-hubs.servicebus.windows.net') begin\r\n",
129+
" drop database scoped credential [https://azure-event-hubs.servicebus.windows.net];\r\n",
130+
"end;\r\n",
131+
"create database scoped credential [https://azure-event-hubs.servicebus.windows.net]\r\n",
132+
"with identity = 'Managed Identity', \r\n",
133+
"secret = '{\"resourceid\": \"https://eventhubs.azure.net\" }';"
134+
],
135+
"metadata": {
136+
"azdata_cell_guid": "8a8775d3-73cf-4e7b-a0d4-82d707c611f8",
137+
"language": "sql"
138+
},
139+
"outputs": [],
140+
"execution_count": null
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"source": [
145+
"Once this is done you can send the message using the same code as before:"
146+
],
147+
"metadata": {
148+
"azdata_cell_guid": "59fb3012-317e-4aba-a59e-d4f97efea998"
149+
},
150+
"attachments": {}
151+
},
152+
{
153+
"cell_type": "code",
154+
"source": [
155+
"declare @payload nvarchar(max) = '{\"UserId\": \"6C5E29A2-A5E7-449D-BD14-259D61ADC6BE\", \"FirstName\": \"John\", \"LastName\": \"Doe\"}';\r\n",
156+
"declare @headers nvarchar(4000) = N'{\"BrokerProperties\": \"' + string_escape('{\"PartitionKey\": \"6C5E29A2-A5E7-449D-BD14-259D61ADC6BE\"}', 'json') + '\"}'\r\n",
157+
"declare @ret int, @response nvarchar(max)\r\n",
158+
"\r\n",
159+
"exec @ret = sp_invoke_external_rest_endpoint \r\n",
160+
" @url = 'https://azure-event-hubs.servicebus.windows.net/myeventhub/messages',\r\n",
161+
" @headers = @headers,\r\n",
162+
" @payload = @payload,\r\n",
163+
"\t\t@credential = [https://azure-event-hubs.servicebus.windows.net],\r\n",
164+
" @response = @response output;\r\n",
165+
"\r\n",
166+
"select @response;"
167+
],
168+
"metadata": {
169+
"azdata_cell_guid": "823b7c9d-0d13-4e45-914a-7c322db9bafe",
170+
"language": "sql"
171+
},
172+
"outputs": [],
173+
"execution_count": null
174+
}
175+
]
176+
}

azure-sql-enable-msi.ipynb

+42
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)