【问题标题】:how to send a pandas dataframe to azure service bus如何将熊猫数据帧发送到天蓝色服务总线
【发布时间】:2021-04-24 07:59:00
【问题描述】:

我有一个 Postgres 数据库。例如,我想读取表上的最后 10 行数据并将其发送到 Azure 服务总线。 在下面的代码中,我阅读了一个示例表并将其保存。之后我想发送到 Azure 服务总线,我收到:

TypeError:ServiceBusMessage 正文必须是字符串、字节或无。取而代之的是:

将表数据发送到 Azure 服务总线的解决方案是什么? 我的代码是:

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)

【问题讨论】:

  • 可能会在发送前将您的 DataFrame 转换为字节。 stackoverflow.com/questions/34666860/…
  • @AhmadAnis 谢谢,我用dataframe.to_string() 转换成字符串,问题解决了
  • @Shadi 很高兴知道您现在可以使用它。请在下面发布对您有用的答案,以便对社区中的其他人也有所帮助。

标签: python pandas postgresql azureservicebus


【解决方案1】:

只需发布一个答案即可结束此问题:

我用dataframe.to_string()转换成字符串,问题解决了

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 2012-06-14
    • 2019-05-10
    • 2020-09-22
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2019-03-24
    • 2019-04-03
    相关资源
    最近更新 更多