fabi54 commented on a change in pull request #2542: [GOBBLIN-672] add EventHub 
source
URL: https://github.com/apache/incubator-gobblin/pull/2542#discussion_r314376192
 
 

 ##########
 File path: 
gobblin-modules/gobblin-eventhub/src/main/java/org/apache/gobblin/eventhub/source/EventhubExtractor.java
 ##########
 @@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.gobblin.eventhub.source;
+
+import com.microsoft.azure.eventhubs.*;
+import org.apache.gobblin.configuration.ConfigurationKeys;
+import org.apache.gobblin.configuration.WorkUnitState;
+import org.apache.gobblin.source.extractor.DataRecordException;
+import org.apache.gobblin.source.extractor.Extractor;
+import org.apache.gobblin.source.extractor.extract.LongWatermark;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.time.Duration;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.Iterator;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
+/**
+ * Created by Jan on 23-Jan-19.
+ */
+public class EventhubExtractor implements Extractor<Void, EventData> {
+
+    public static final Logger LOG = 
LoggerFactory.getLogger(EventhubExtractor.class);
+
+    public static final String CONFIG_PREFIX = "source.azure.eventhubs.";
+    public static final String ENDPOINT_KEY = CONFIG_PREFIX + "endpoint";
+    public static final String EVENTHUB_NAME_KEY = CONFIG_PREFIX + "name";
+    public static final String SAS_KEY_NAME_KEY = CONFIG_PREFIX + 
"sas.key.name";
+    public static final String SAS_KEY_KEY = CONFIG_PREFIX + "sas.key";
+    public static final String CONSUMER_GROUP_KEY = CONFIG_PREFIX + 
"consumer.group";
+    public static final String MAX_EXTRACTION_DURATION_KEY = CONFIG_PREFIX + 
"max.extraction.duration";
+    public static final String MAX_EVENTS_IN_BATCH_KEY = CONFIG_PREFIX + 
"max.events.in.batch";
+    public static final int DEFAULT_MAX_EVENTS_IN_BATCH = 100;
+
+    private WorkUnitState workUnit;
+    private String highWatermark;
+    private PartitionReceiver receiver;
+    private EventHubClient ehClient;
+    private ScheduledExecutorService executorService;
+    private int eventsInBatch;
+    private LocalDateTime startTime; // for logging only
+    private Duration maxDuration; // for logging only
+    private LocalDateTime endTime;
+    private Iterator<EventData> iterator;
+    private int pulled;
+
+    private void logStatus() {
+        LOG.info(String.format("Current high watermark: %s, job duration so 
far: %s (of max %s), received events cummulative: %d",
+                highWatermark,
+                Duration.between(startTime, LocalDateTime.now()).toString(),
+                maxDuration.toString(),
+                pulled));
+    }
+
+    private Iterator<EventData> getEventIterator() throws EventHubException {
+        if (iterator == null || !iterator.hasNext()) {
+            logStatus();
+            iterator = null;
+            if (endTime != null && 
LocalDateTime.now(ZoneId.of("UTC")).isBefore(endTime)) {
+                LOG.info("Attempting to receive records from source.");
+                Iterable<EventData> received = 
receiver.receiveSync(eventsInBatch);
+                if (received != null) {
+                    iterator = received.iterator();
+                    LOG.info(String.format("Received batch size: %s", 
received.spliterator().getExactSizeIfKnown()));
+                } else {
+                    LOG.info("No records received from source.");
 
 Review comment:
   Iterator is reset to null at the beginning of the method, so null iterator 
will be returned (not the old iterator). This is then handled in readRecord(), 
so it won't fail.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to