0
votes

I have created an event hub in Azure portal with 2 partitions( 0 and 1). As there is no concept of topics in event hubs like Service bus. I am trying to store different data in partition 0 and partition 1 using

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.

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?

1

1 Answers

0
votes

For your title question, as @PeterBons said, there is not anything similar to Azure Service Bus Topic in EventHubs.

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). However, as you see, the second argument is PartitionKey, not PartitionId. And the offical API reference said as below from here, it's not correct for your code to store the data by partition.

Multiple PartitionKey's could be mapped to one Partition. EventHubs service uses a proprietary Hash algorithm to map the PartitionKey to a PartitionId. Using this type of Send (Sending using a specific partitionKey), could sometimes result in partitions which are not evenly distributed.

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.

For 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:

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. 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.

Hope it helps. Any concern, please feel free to let me know.