Search in sources :

Example 1 with PartitionSender

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.");
}
Also used : HashMap(java.util.HashMap) EventHubException(com.microsoft.azure.eventhubs.EventHubException) SamzaException(org.apache.samza.SamzaException) PartitionSender(com.microsoft.azure.eventhubs.PartitionSender) EventHubClient(com.microsoft.azure.eventhubs.EventHubClient) EventHubClientManager(org.apache.samza.system.eventhub.EventHubClientManager) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with PartitionSender

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);
    }
}
Also used : PartitionSender(com.microsoft.azure.eventhubs.PartitionSender) SamzaException(org.apache.samza.SamzaException)

Aggregations

PartitionSender (com.microsoft.azure.eventhubs.PartitionSender)2 SamzaException (org.apache.samza.SamzaException)2 EventHubClient (com.microsoft.azure.eventhubs.EventHubClient)1 EventHubException (com.microsoft.azure.eventhubs.EventHubException)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 EventHubClientManager (org.apache.samza.system.eventhub.EventHubClientManager)1