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

时间:2017-02-20 19:22:23

标签: java azure

我在Azure门户中创建了一个带有2个分区(0和1)的事件中心。由于服务总线等事件中心没有主题概念。我试图使用

在分区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");

即使我试图将数据存储在分区0中,它默认存储在分区1中。

我的接收者逻辑是:

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");
}

这是将数据存储在分区中的正确方法吗?我们可以使用分区作为Azure Service总线主题的等价物吗?

1 个答案:

答案 0 :(得分:0)

对于标题问题,正如@PeterBons所说,在EventHubs中没有类似于Azure Service Bus Topic的内容。

根据你的描述&代码,您希望通过使用方法EventHubClient.send(EventData, PartitionKey)将事件数据发送到指定的分区。但是,如您所见,第二个参数是PartitionKey,而不是PartitionId。而官方API参考如下所述here,您的代码通过分区存储数据不正确。

  

可以将多个PartitionKey映射到一个分区。 EventHubs服务使用专有的哈希算法将PartitionKey映射到PartitionId。使用这种类型的Send(使用特定的partitionKey发送)有时会导致分区不均匀分布。

请参阅官方文件Publishing Events with the Java client for Azure Event Hubs& Consuming Events with the Java client for Azure Event Hubs创建PartitionSender& PartitionReceiver向/从指定分区发送/接收事件数据,如下所示。

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();

PartitionReceiver

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

我不知道为什么要将分区用作Azure Service总线主题的等效内容。根据我的经验,模拟Azure Service Bus主题行为的变通方法是使用JSON格式在事件数据中添加topic之类的属性,并过滤&在接收时,在topic属性内调度数据。

希望它有所帮助。如有任何疑虑,请随时告诉我。