Azure python sdk服务总线接收消息

问题描述 投票:0回答:2

我对 azure python servicebus 有点困惑。

我有一个监听特定消息的服务总线主题和订阅,我有接收这些消息的代码,然后它们将由 aws comprehend 处理。

按照微软文档,接收消息的基本代码可以工作,我可以打印它,但是当我将相同的逻辑与理解集成时,它失败了。

这是示例,这是来自 Microsoft 文档的代码:

with servicebus_client:
    # get the Queue Receiver object for the queue
    receiver = servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME, max_wait_time=5)
    with receiver:
        for msg in receiver:
            print("Received: " + str(msg))
            # complete the message so that the message is removed from the queue
            receiver.complete_message(msg)

输出是这样的

{"ModuleId":"123458", "Text":"This is amazing."}
Receive is done.

我的第一个想法是收到的消息是一个 Json 对象。所以我开始编写代码来从 json 输出读取数据,如下所示:

servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR)
with servicebus_client:
    receiver = servicebus_client.get_subscription_receiver(
        topic_name=TOPIC_NAME,
        subscription_name=SUBSCRIPTION_NAME
    )
    with receiver:
        received_msgs = receiver.receive_messages(max_message_count=10, max_wait_time=5)
        for msg in received_msgs:
            # print(str(msg))
            message = json.dumps(msg)
            text = message['Text']

            #passing the text to comprehend
            result_json= json.dumps(comprehend.detect_sentiment(Text=text, LanguageCode='en'), sort_keys=True, indent=4)
            result = json.loads(result_json) # converting json to python dictionary

            #extracting the sentiment value 
            sentiment = result["Sentiment"]

            #extracting the sentiment score
            if sentiment == "POSITIVE":
                value = round(result["SentimentScore"]["Positive"] * 100,2)

            elif sentiment == "NEGATIVE":
                value = round(result["SentimentScore"]["Negative"] * 100,2)

            elif sentiment == "NEUTRAL":
                value = round(result["SentimentScore"]["Neutral"] * 100,2)
                
            elif sentiment == "MIXED":
                value = round(result["SentimentScore"]["Mixed"] * 100,2)

            #store the text, sentiment and value in a dictionary and convert it tp JSON
            output={'Text':text,'Sentiment':sentiment, 'Value':value}
            output_json = json.dumps(output)

            print('Text: ',text,'\nSentiment: ',sentiment,'\nValue: ', value)

            print('In JSON format\n',output_json)

            receiver.complete_message(msg)

print("Receive is done.")

但是当我运行这个时,我收到以下错误:

TypeError: Object of type ServiceBusReceivedMessage is not JSON serializable

任何可以帮助我了解从接收返回的服务总线类型的人是否遇到过这种情况?

非常感谢大家

python-3.x azureservicebus azure-python-sdk
2个回答
2
投票

有没有人遇到过这种情况,谁能帮助我理解什么? 从接收返回的服务总线类型是什么?

接收到的消息类型为

ServiceBusReceivedMessage
,源自
ServiceBusMessage
。消息的内容可以从其
body
属性中获取。

你能尝试一下吗:

message = json.dumps(msg.body)

0
投票

当您将

msg
转换为字符串时,它将具有 JSON 格式。您不需要额外的转储。只需使用消息字符串调用
json.loads
函数:

for msg in received_msgs:
    print("Received: ", msg)
    message = json.loads(str(msg))
    print("Sentiment: ", message["Sentiment"])
© www.soinside.com 2019 - 2024. All rights reserved.