Shadi
Shadi

Reputation: 313

how to send a pandas dataframe to azure service bus

I have a Postgres database. for example, I wanna read 10 last row data on a table and send it to the Azure service bus. In the below code I read a sample table and save it. After that I wanna sent to the Azure service bus, I receive:

TypeError: ServiceBusMessage body must be a string, bytes, or None. Got instead: <class 'pandas.core.frame.DataFrame'>

What is the solution to send table data to the Azure service bus? My code is:

import os
import psycopg2
import pandas as pd
from azure.servicebus import ServiceBusClient, ServiceBusMessage


conndb = psycopg2.connect(
    database="testDB", user="postgres", password="****", host="127.0.0.1", port="5432"
)
connstr = "Endpoint=****"
queue_name = '****'


data = pd.read_sql_query('select * from cricketers', conndb)
# print(data)

with ServiceBusClient.from_connection_string(connstr) as client:
    with client.get_queue_sender(queue_name) as sender:
        single_message = ServiceBusMessage(data)
        sender.send_messages(single_message)

Upvotes: 1

Views: 494

Answers (1)

Steve Johnson
Steve Johnson

Reputation: 8660

Just post an answer to end this question:

I convert to string with dataframe.to_string(), and the problem solved

Upvotes: 2

Related Questions