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

时间:2019-04-22 17:58:29

标签: python azure azure-eventhub

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

  

CreatePartitionSender(String)创建一个PartitionSender,它可以   直接将EventData发布到特定的EventHub分区。

但是,我在Python中找不到相同的东西。

有什么办法吗?

2 个答案:

答案 0 :(得分:0)

我不确定,但是使用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)

这是 Init

def __init__(self, client, target, partition=None, send_timeout=60, keep_alive=None, auto_reconnect=True):
        """
        Instantiate an EventHub event Sender handler.
        :param client: The parent EventHubClient.
        :type client: ~azure.eventhub.client.EventHubClient.
        :param target: The URI of the EventHub to send to.
        :type target: str
        :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
        :param send_timeout: The timeout in seconds for an individual event to be sent from the time that it is
         queued. Default value is 60 seconds. If set to 0, there will be no timeout.
        :type send_timeout: int
        :param keep_alive: The time interval in seconds between pinging the connection to keep it alive during
         periods of inactivity. The default value is None, i.e. no keep alive pings.
        :type keep_alive: int
        :param auto_reconnect: Whether to automatically reconnect the sender if a retryable error occurs.
         Default value is `True`.
        :type auto_reconnect: bool
        """
        self.running = False
        self.client = client
        self.target = target
        self.partition = partition
        self.timeout = send_timeout
        self.redirected = None
        self.error = None
        self.keep_alive = keep_alive
        self.auto_reconnect = auto_reconnect
        self.retry_policy = errors.ErrorPolicy(max_retries=3, on_error=_error_handler)
        self.reconnect_backoff = 1
        self.name = "EHSender-{}".format(uuid.uuid4())
        if partition:
            self.target += "/Partitions/" + partition
            self.name += "-partition{}".format(partition)
        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._outcome = None
        self._condition = None

我相信,下面的功能仅会创建一个分区发送器

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

希望有帮助。

答案 1 :(得分:0)

有两种方法可以将数据发送到Azure事件中心,即HTTP REST API和AMQP 1.0协议。

对于使用HTTP REST API或Azure EventHub Python Client Library,只有[(({'beauty': True}, 'pos'), ({'book': True}, 'neu'), ({'bad': True}, 'neg')), (({'good': True}, 'pos'), None, ({'sick': True}, 'neg')), (({'happy': True}, 'pos'), None, ({'lazy': True}, 'neg'))] 参数支持将新事件发送到事件中心中的指定分区,如下两个。

  1. REST API Send partition event在端点partitionId中需要partitionId参数,并且它是唯一一个支持发送分区功能的REST API。

  2. https://{servicebusNamespace}.servicebus.windows.net/{eventHubPath}/partitions/{partitionId}/messages的源代码注释解释了partition参数,如下所示。

    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 值将事件发送到指定的EventHub分区,除非在Python中使用AMQP 1.0。要使用AMQP 1.0,请查看官方文档AMQP 1.0 in Azure Service Bus and Event Hubs protocol guide,然后在页面上搜索单词partitionKey,结果如下。

enter image description here