Java Azure事件中心中是否有类似于Azure服务总线的主题?
我已经在Azure portal中创建了一个具有2个分区(0和1)的事件中心。因为在事件中心(如服务总线)中没有主题的概念。我正在尝试使用存储分区0和分区1中的不同数据Java Azure事件中心中是否有类似于Azure服务总线的主题?,java,azure,Java,Azure,我已经在Azure portal中创建了一个具有2个分区(0和1)的事件中心。因为在事件中心(如服务总线)中没有主题的概念。我正在尝试使用存储分区0和分区1中的不同数据 ehClient = EventHubClient.createFromConnectionStringSync(eventHubConnectionString.toString()); byte[] payload = "Storing data in partion 0".getBytes("UTF-8"); /** St
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服务总线主题的等价物吗?对于您的标题问题,正如@PeterBons所说,EventHubs中没有类似于Azure服务总线主题的内容 根据您的描述和代码,您希望通过使用方法
EventHubClient.send(EventData,PartitionKey)
将事件数据发送到指定的分区。但是,正如您所看到的,第二个参数是PartitionKey
,而不是PartitionId
。官方API参考如下所述,您的代码按分区存储数据是不正确的
多个PartitionKey可以映射到一个分区。EventHubs服务使用专有的哈希算法将PartitionKey映射到PartitionId。使用这种类型的发送(使用特定的partitionKey发送)有时可能会导致分区分布不均匀
请参阅官方文档&创建PartitionSender
和PartitionReceiver
以向指定分区发送/接收事件数据,如下所示
对于分区发送者:
String partitionId = "0";
EventHubClient ehClient = EventHubClient.createFromConnectionString(str).get();
EventHubSender sender = ehClient.createPartitionSender(partitionId).get();
EventData sendEvent = new EventData(payloadBytes);
sender.send(sendEvent).get();
String partitionId = "0"; // API to get PartitionIds will be released soon
PartitionReceiver receiver = ehClient.createReceiver(
EventHubClient.DefaultConsumerGroupName,
partitionId,
PartitionReceiver.StartOfStream,
false).get();
对于分区接收器:
String partitionId = "0";
EventHubClient ehClient = EventHubClient.createFromConnectionString(str).get();
EventHubSender sender = ehClient.createPartitionSender(partitionId).get();
EventData sendEvent = new EventData(payloadBytes);
sender.send(sendEvent).get();
String partitionId = "0"; // API to get PartitionIds will be released soon
PartitionReceiver receiver = ehClient.createReceiver(
EventHubClient.DefaultConsumerGroupName,
partitionId,
PartitionReceiver.StartOfStream,
false).get();
我不知道你为什么要使用分区作为Azure服务总线主题的等价物。根据我的经验,模拟Azure Service Bus Topic行为的变通方法是使用JSON格式在事件数据中添加类似Topic
的属性,并在接收时过滤和分派Topic
属性中的数据
希望能有帮助。如有任何问题,请随时通知我。否,请参阅