如何将数据发送到EventHub设置PartitionId而不是PartitionKey(Python)

2024-09-22 14:39:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在microsoftdocs上看到,有一种方法可以通过设置PartitionId而不是PartitionKey(使用C#)来将数据发送到我想要的分区。在

CreatePartitionSender(String) Create a PartitionSender which can publish EventData's directly to a specific EventHub partition.

但是,我在Python中找不到相同的内容。在

有什么办法吗?在


Tags: 数据方法whichstringcreatepublishcan分区
2条回答

有两种方法可以将数据发送到Azure事件中心,即httprestapi和amqp1.0协议。在

对于使用httprestapi或Azure EventHub Python Client Library,只有partitionId参数支持将新事件发送到事件集线器中的指定分区,如下所示。在

  1. restapi^{}需要端点https://{servicebusNamespace}.servicebus.windows.net/{eventHubPath}/partitions/{partitionId}/messages中的partitionId参数,它是唯一一个支持发送分区功能的REST API。

  2. Sender.py的源代码注释解释了^{}参数,如下所示。在

    :param partition: The specific partition ID to send to. Default is None, in which case the service
     will assign to all partitions using round-robin.
    :type partition: str
    

所以实际上,除了在Python中使用amqp1.0之外,不能使用partitionKey值将事件发送到指定的EventHub分区。要使用amqp1.0,请参阅官方文档^{},并在页面上搜索partition-key,结果如下。在

enter image description here

我不太确定,但是使用python,这里是打开连接的方法

def open(self):
        """
        Open the Sender using the supplied conneciton.
        If the handler has previously been redirected, the redirect
        context will be used to create a new handler before opening it.
        :param connection: The underlying client shared connection.
        :type: connection: ~uamqp.connection.Connection
        """
        self.running = True
        if self.redirected:
            self.target = self.redirected.address
            self._handler = SendClient(
                self.target,
                auth=self.client.get_auth(),
                debug=self.client.debug,
                msg_timeout=self.timeout,
                error_policy=self.retry_policy,
                keep_alive_interval=self.keep_alive,
                client_name=self.name,
                properties=self.client.create_properties())
        self._handler.open()
        while not self._handler.client_ready():
            time.sleep(0.05)

这是初始化

^{pr2}$

我相信,下面的函数行只会创建一个分区发送器

if partition:
                self.target += "/Partitions/" + partition
                self.name += "-partition{}".format(partition)

参考

https://github.com/Azure/azure-event-hubs-python/blob/master/azure/eventhub/sender.py

希望有帮助。在

相关问题 更多 >