简体   繁体   English

Azure事件中心中是否有类似于Azure Service Bus主题的内容?

[英]Is there anything similar to Azure Service Bus Topics in Azure Event hubs?

I have created an event hub in Azure portal with 2 partitions( 0 and 1). 我已经在具有2个分区(0和1)的Azure门户中创建了一个事件中心。 As there is no concept of topics in event hubs like Service bus. 因为在事件中心(如Service bus)中没有主题的概念。 I am trying to store different data in partition 0 and partition 1 using 我正在尝试使用以下方式将不同的数据存储在分区0和分区1中

ehClient = EventHubClient.createFromConnectionStringSync(eventHubConnectionString.toString());
byte[] payload = "Storing data in partion 0".getBytes("UTF-8");
/** Storing data in partion 0*/
EventData data = new EventData(payload);
ehClient .send(data, "0");

Even though I am trying to store the data in partition 0 it is by default getting stored in partition 1. 即使我试图将数据存储在分区0中,默认情况下也会存储在分区1中。

My recieiver logic is: 我的接收者逻辑是:

eventHubClient = EventHubClient.create(Constant.EVENTHUB_SASKEYNAME,
            Constant.EVENTHUB_SASKEY, Constant.EVENTHUB_NAMESPACE, Constant.EVENTHUB_NAME);

EventHubConsumerGroup eventHubConsumerGroup = eventHubClient.getConsumerGroup("$Default");
eventHubReceiver = eventHubConsumerGroup.createReceiver("0", null, -1);

while (true) {
    message = eventHubReceiver.receive(-1);

    if (null != message)
        System.out.println("The message that is delivered is : " + message.getPayload());
    else
        System.out.println("No message in the hub");
}

Is it the right way to store the data in partitions? 这是在分区中存储数据的正确方法吗? Can we use partitions as equivalent to Azure Service bus topics? 我们可以使用与Azure Service bus主题等效的分区吗?

For your title question, as @PeterBons said, there is not anything similar to Azure Service Bus Topic in EventHubs. 对于您的标题问题,正如@PeterBons所说,Eve​​ntHubs中没有类似于Azure Service Bus Topic的东西。

According to your description & codes, you want to send event data to the specified partition as you want via using the method EventHubClient.send(EventData, PartitionKey) . 根据您的描述和代码,您想要通过使用EventHubClient.send(EventData, PartitionKey)方法将事件数据发送到指定的分区。 However, as you see, the second argument is PartitionKey , not PartitionId . 但是,如您所见,第二个参数是PartitionKey ,而不是PartitionId And the offical API reference said as below from here , it's not correct for your code to store the data by partition. 官方的API参考从此处如下所述,您的代码按分区存储数据是不正确的。

Multiple PartitionKey's could be mapped to one Partition. 多个PartitionKey可以映射到一个分区。 EventHubs service uses a proprietary Hash algorithm to map the PartitionKey to a PartitionId. EventHubs服务使用专有的哈希算法将PartitionKey映射到PartitionId。 Using this type of Send (Sending using a specific partitionKey), could sometimes result in partitions which are not evenly distributed. 使用这种类型的发送(使用特定的partitionKey发送)有时可能会导致分区分布不均。

Please refer to the offical documents Publishing Events with the Java client for Azure Event Hubs & Consuming Events with the Java client for Azure Event Hubs to create PartitionSender & PartitionReceiver to send/receive event data to/from the specified partition, as below. 请参考正式文档, 其中,使用Java客户端发布事件以用于Azure事件中心 ,使用消费事件使用Java客户端来用于Azure事件中心,以创建PartitionSenderPartitionReceiver来向/从指定分区发送/接收事件数据,如下所示。

For PartitionSender : 对于PartitionSender

String partitionId = "0";
EventHubClient ehClient = EventHubClient.createFromConnectionString(str).get();
EventHubSender sender = ehClient.createPartitionSender(partitionId).get();
EventData sendEvent = new EventData(payloadBytes);
sender.send(sendEvent).get();

For PartitionReceiver : 对于PartitionReceiver

String partitionId = "0"; // API to get PartitionIds will be released soon
PartitionReceiver receiver = ehClient.createReceiver(
            EventHubClient.DefaultConsumerGroupName, 
            partitionId, 
            PartitionReceiver.StartOfStream,
            false).get();

I don't know why you want to use partitions as equivalent to Azure Service bus topics. 我不知道为什么要使用等同于Azure Service总线主题的分区。 Just per my experience, the workaround way to simulate the behavior of Azure Service Bus Topic is that add the property like topic in Event Data using JSON format, and filter & dispatch the data within topic property when receiving. 只要按我的经验,解决办法的方式来模拟Azure的服务总线主题的行为是添加属性类似topic使用JSON格式的事件数据中,和过滤及内调度数据topic在接收时财产。

Hope it helps. 希望能帮助到你。 Any concern, please feel free to let me know. 如有任何疑问,请随时告诉我。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM