2017-02-20 80 views
0

我已經在2個分區(0和1)的Azure門戶中創建了一個事件中心。由於在服務總線等事件中心沒有任何主題概念。我試圖存儲在分區0和分區1不同的數據使用Azure Event Hub中是否有類似於Azure Service Bus Topics的內容?

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.

我recieiver邏輯是:

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服務總線主題嗎?

+0

沒有,看http://stackoverflow.com/questions/42255265/how-are-different-events-handled-in-azure-event-hubs –

回答

0

對於你的標題問題,如@PeterBons說,沒有任何東西EventHubs類似Azure的服務總線主題。

根據您的描述&代碼,您想通過使用方法EventHubClient.send(EventData, PartitionKey)將事件數據發送到指定的分區。但是,如您所見,第二個參數是PartitionKey,而不是PartitionId。官方的API參考文獻如下所述,從here開始,對於您的代碼來說,按分區存儲數據是不正確的。

多個PartitionKey可以映射到一個分區。 EventHubs服務使用專有散列算法將PartitionKey映射到PartitionId。使用這種類型的發送(使用特定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的服務總線主題。根據我的經驗,模擬Azure Service Bus Topic的行爲的解決方法是使用JSON格式在事件數據中添加topic等屬性,並在接收時過濾&在topic屬性中分派數據。

希望它有幫助。任何問題,請隨時讓我知道。

相關問題