Send a message to Service Bus queue from synapse notebook

131 views Asked by At

We are loading data from a blob file and need to send messages to Service Bus. Trying to find if it is possible to send messages to service bus queue from Azure synapse notebook. If possible please share details regarding how to connect to service bus.

I could not find anything specific

1

There are 1 answers

0
RithwikBojja On

Send a message to Service Bus queue from synapse notebook

You can use below code to do the same:

from pyspark.sql import SparkSession
from azure.servicebus import ServiceBusClient, ServiceBusMessage

if __name__ == "__main__":
 
    rith_servicebus_conn_string = "Endpoint=sb://servicebusname.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=umaboZwpRa+sQ="
    rith_service_bus_queue = "rithwik"
    rith_message = "Hello Rithwik Bojja Service Bus!"
    with ServiceBusClient.from_connection_string(rith_servicebus_conn_string) as client:
        with client.get_queue_sender(rith_service_bus_queue) as sender:
            rith_msg = ServiceBusMessage(rith_message)
            sender.send_messages(rith_msg)
    print("Hello Rithwik Bojja!!!, Messages sent to Service Bus.")

Here,

rith_service_bus_queue = queue name

rith_servicebus_conn_string = connection string

Before using above code install this package:

!pip install azure-servicebus

enter image description here

Output:

enter image description here

In Service Bus Queue:

enter image description here