在Azure函数Python SDK中,如何获取给定命名空间的主题数? [英] In the Azure functions Python SDK, how do I get the number of topics for a given namespace?

查看:82
本文介绍了在Azure函数Python SDK中,如何获取给定命名空间的主题数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Python 3.8与azure-mgmt-servicebus = v.1.0.0一起使用.我想获取给定名称空间的主题数.我已经尝试了以下...

I'm using Python 3.8 with azure-mgmt-servicebus= v. 1.0.0. I would like to get the number of topics for a given namespace. I have tried the below ...

credential = ServicePrincipalCredentials(self._client_id, self._client_secret, tenant=self._tenant)
        sb_client = ServiceBusManagementClient(credential, self._subscription)
         ...
        topics = sb_client.topics.list_by_namespace(
                resource_group_name=self._resource_group_name,
                namespace_name=namespace
            )
            num_topics = 0
            while topics.current_page:
                num_topics += topics.current_page.count
                topics.next
            logging.info("num topics: %s", num_topics)

我的"num_topics"尽管我已经确认建立了连接(我可以使用相同的连接创建一个主题),并且在Azure门户中可以看到许多给定信息的主题,但始终返回零.我以为我没有正确使用API​​,但是不确定哪里会崩溃.如何获得给定名称空间的主题数?

My "num_topics" consistently comes back with zero, despite the fact I have verified that my connection is being made (I can create a topic with the same connection) and I can see many topics for the given information in the Azure portal. I'm thinking I'm not using the API properly but am unsure where things are falling apart. How do I get the number of topics for a given namespace?

推荐答案

如果要获取给定服务总线名称空间的主题数,则可以使用下面的代码.

If you want to get the number of the topics for a given service bus namespace, you could use the code below.

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.servicebus import ServiceBusManagementClient

subscription_id = "<subscription-id>"
rg_name = "<resource-group-name>"

tenant_id = "<tenant-id>"
client_id = "<client-id>"
client_secret = "<client-secret>"

credential = ServicePrincipalCredentials(client_id=client_id, secret=client_secret, tenant=tenant_id)
sb_client = ServiceBusManagementClient(credential, subscription_id)
topics = sb_client.topics.list_by_namespace(resource_group_name= rg_name, namespace_name= "servicebusname")

num_topics = 0
for topic in topics:
    num_topics += 1
print(num_topics)

检查门户中的主题,结果正确:

Check the topics in the portal, the result is correct:

更新:

如果不想使用循环,可以将 topics 转换为列表,然后使用 len()函数.

If you don't want to use the loop, you could convert the topics to a list, then use the len() function.

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.servicebus import ServiceBusManagementClient

subscription_id = "<subscription-id>"
rg_name = "<resource-group-name>"

tenant_id = "<tenant-id>"
client_id = "<client-id>"
client_secret = "<client-secret>"

credential = ServicePrincipalCredentials(client_id=client_id, secret=client_secret, tenant=tenant_id)
sb_client = ServiceBusManagementClient(credential, subscription_id)
topics = sb_client.topics.list_by_namespace(resource_group_name= rg_name, namespace_name= "servicebusname")

testlist = list(topics)
print(len(testlist))

这篇关于在Azure函数Python SDK中,如何获取给定命名空间的主题数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆