简体   繁体   English

在 Azure 函数 Python SDK 中,如何获取给定命名空间的主题数?

[英]In the Azure functions Python SDK, how do I get the number of topics for a given namespace?

I'm using Python 3.8 with azure-mgmt-servicebus= v. 1.0.0.我正在使用 Python 3.8 和 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)

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.我的“num_topics”始终返回零,尽管事实上我已经验证了我的连接正在建立(我可以使用相同的连接创建一个主题)并且我可以在 Azure 门户中看到给定信息的许多主题。 I'm thinking I'm not using the API properly but am unsure where things are falling apart.我在想我没有正确使用 API 但我不确定哪里出了问题。 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:检查门户中的主题,结果是正确的:

在此处输入图像描述

Update:更新:

If you don't want to use the loop, you could convert the topics to a list, then use the len() function.如果不想使用循环,可以将topics转换为列表,然后使用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))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Azure 的 Python 服务总线 SDK 中,如何查询命名空间的共享访问策略? - In Azure's Python service bus SDK, how do I query the shared access policies of a namespace? 如何获取 python 中的数字 `(0 + infj)`? - How do I get the number `(0 + infj)` in python? 如何使用 Python SDK 为我在 Azure 数据湖中创建的文件设置到期日期? - How do I set an expiration date on a file I create in the Azure Data lake using the Python SDK? 使用 azure Python SDK,如何使用 ServiceBusManagementClient 发送有关主题的消息? - With the azure Python SDK, how do I use the ServiceBusManagementClient to send a message on a topic? 如何创建/连接到 Docker Azure 服务总线(使用 Python SDK)? - How do I create/connect to a Docker Azure service bus (using Python SDK)? 在 Azure 函数中使用 cosmosdb python SDK - Using cosmosdb python SDK in Azure functions 使用 Azure ServiceBus 7.0 SDK,如何使用 ServiceBusManagementClient class 获得对 ServiceBusClient 的引用? - With the Azure ServiceBus 7.0 SDK, how do I get a reference to a ServiceBusClient using a ServiceBusManagementClient class? 如何在 python CLI 应用程序中获取 Azure 授权代码? - How do I get the Azure authorization code in a python CLI application? 如何使用 Cloud Logging Python SDK 获取给定接收器的 writerIdentity - How to get writerIdentity for a given Sink using Cloud Logging Python SDK 给定4个函数,如何在Python中制作2x2直方图? - Given 4 functions, how do you make a 2x2 histogram in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM