use of com.microsoft.azure.eventhubs.PartitionSender in project samza by apache.
the class EventHubSystemProducer method init.
private void init() {
LOG.info("Initializing EventHubSystemProducer");
// Fetches the stream ids
List<String> streamIds = config.getStreams(systemName);
// partition count)
for (String streamId : streamIds) {
EventHubClientManager ehClient = eventHubClientManagerFactory.getEventHubClientManager(systemName, streamId, config);
perStreamEventHubClientManagers.put(streamId, ehClient);
ehClient.init();
}
// Create partition senders if required
if (PartitioningMethod.PARTITION_KEY_AS_PARTITION.equals(partitioningMethod)) {
// Create all partition senders
perStreamEventHubClientManagers.forEach((streamId, samzaEventHubClient) -> {
EventHubClient ehClient = samzaEventHubClient.getEventHubClient();
try {
Map<Integer, PartitionSender> partitionSenders = new HashMap<>();
long timeoutMs = config.getRuntimeInfoWaitTimeMS(systemName);
Integer numPartitions = ehClient.getRuntimeInformation().get(timeoutMs, TimeUnit.MILLISECONDS).getPartitionCount();
for (int i = 0; i < numPartitions; i++) {
String partitionId = String.valueOf(i);
EventHubClientManager perPartitionClientManager = createOrGetEventHubClientManagerForPartition(streamId, i);
PartitionSender partitionSender = perPartitionClientManager.getEventHubClient().createPartitionSender(partitionId).get(DEFAULT_CREATE_PARTITION_SENDER_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
partitionSenders.put(i, partitionSender);
}
streamPartitionSenders.put(streamId, partitionSenders);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
String msg = "Failed to fetch number of Event Hub partitions for partition sender creation";
throw new SamzaException(msg, e);
} catch (EventHubException | IllegalArgumentException e) {
String msg = "Creation of partition sender failed with exception";
throw new SamzaException(msg, e);
}
});
}
isInitialized = true;
LOG.info("EventHubSystemProducer initialized.");
}
use of com.microsoft.azure.eventhubs.PartitionSender in project samza by apache.
the class EventHubSystemProducer method sendToEventHub.
private CompletableFuture<Void> sendToEventHub(String streamId, EventData eventData, Object partitionKey, EventHubClient eventHubClient) {
if (PartitioningMethod.ROUND_ROBIN.equals(partitioningMethod)) {
return eventHubClient.send(eventData);
} else if (PartitioningMethod.EVENT_HUB_HASHING.equals(partitioningMethod)) {
if (partitionKey == null) {
throw new SamzaException("Partition key cannot be null for EventHub hashing");
}
return eventHubClient.send(eventData, convertPartitionKeyToString(partitionKey));
} else if (PartitioningMethod.PARTITION_KEY_AS_PARTITION.equals(partitioningMethod)) {
if (!(partitionKey instanceof Integer)) {
String msg = "Partition key should be of type Integer";
throw new SamzaException(msg);
}
Integer numPartition = streamPartitionSenders.get(streamId).size();
Integer destinationPartition = (Integer) partitionKey % numPartition;
PartitionSender sender = streamPartitionSenders.get(streamId).get(destinationPartition);
return sender.send(eventData);
} else {
throw new SamzaException("Unknown partitioning method " + partitioningMethod);
}
}
Aggregations