jsun98 commented on a change in pull request #6431: Add Kinesis Indexing 
Service to core Druid
URL: https://github.com/apache/incubator-druid/pull/6431#discussion_r230883161
 
 

 ##########
 File path: 
extensions-core/kafka-indexing-service/src/main/java/org/apache/druid/indexing/kafka/KafkaRecordSupplier.java
 ##########
 @@ -0,0 +1,207 @@
+/*
+ * 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.druid.indexing.kafka;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import 
org.apache.druid.indexing.seekablestream.common.OrderedPartitionableRecord;
+import org.apache.druid.indexing.seekablestream.common.RecordSupplier;
+import org.apache.druid.indexing.seekablestream.common.StreamPartition;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.emitter.EmittingLogger;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.common.PartitionInfo;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.serialization.ByteArrayDeserializer;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
+
+public class KafkaRecordSupplier implements RecordSupplier<Integer, Long>
+{
+  private static final EmittingLogger log = new 
EmittingLogger(KafkaRecordSupplier.class);
+  private static final Random RANDOM = ThreadLocalRandom.current();
+
+  private final KafkaConsumer<byte[], byte[]> consumer;
+  private final Map<String, Object> consumerProperties;
+  private final ObjectMapper sortingMapper;
+  private boolean closed;
+  private final BlockingQueue<OrderedPartitionableRecord<Integer, Long>> 
records;
+
+
+  public KafkaRecordSupplier(
+      Map<String, Object> consumerProperties,
+      ObjectMapper sortingMapper
+  )
+  {
+    this.consumerProperties = consumerProperties;
+    this.sortingMapper = sortingMapper;
+    this.consumer = getKafkaConsumer();
+    this.closed = false;
+    this.records = new LinkedBlockingQueue<>();
+  }
+
+  @Override
+  public void assign(Set<StreamPartition<Integer>> streamPartitions)
+  {
+    consumer.assign(streamPartitions
+                        .stream()
+                        .map(x -> new TopicPartition(x.getStream(), 
x.getPartitionId()))
+                        .collect(Collectors.toSet()));
+  }
+
+  @Override
+  public void seek(StreamPartition<Integer> partition, Long sequenceNumber)
+  {
+    consumer.seek(new TopicPartition(partition.getStream(), 
partition.getPartitionId()), sequenceNumber);
+  }
+
+  @Override
+  public void seekAfter(StreamPartition<Integer> partition, Long 
sequenceNumber)
+  {
+    seek(partition, sequenceNumber + 1);
+  }
+
+  @Override
+  public void seekToEarliest(Set<StreamPartition<Integer>> partitions)
+  {
+    consumer.seekToBeginning(partitions
+                                 .stream()
+                                 .map(e -> new TopicPartition(e.getStream(), 
e.getPartitionId()))
+                                 .collect(Collectors.toList()));
+  }
+
+  @Override
+  public void seekToLatest(Set<StreamPartition<Integer>> partitions)
+  {
+    consumer.seekToEnd(partitions
+                           .stream()
+                           .map(e -> new TopicPartition(e.getStream(), 
e.getPartitionId()))
+                           .collect(Collectors.toList()));
+  }
+
+  @Override
+  public Set<StreamPartition<Integer>> getAssignment()
+  {
+    Set<TopicPartition> topicPartitions = consumer.assignment();
+    return topicPartitions
+        .stream()
+        .map((TopicPartition e) -> new StreamPartition<>(e.topic(), 
e.partition()))
+        .collect(Collectors.toSet());
+  }
+
+  @Override
+  public OrderedPartitionableRecord<Integer, Long> poll(long timeout)
+  {
+    ConsumerRecords<byte[], byte[]> polledRecords = consumer.poll(timeout);
+    if (!polledRecords.isEmpty()) {
+      ConsumerRecord<byte[], byte[]> record = polledRecords.iterator().next();
+      return new OrderedPartitionableRecord<>(
+          record.topic(),
+          record.partition(),
+          record.offset(),
+          record.value() == null ? null : ImmutableList.of(record.value())
+      );
+    }
+    return null;
+  }
+
+  @Override
+  public Long getLatestSequenceNumber(StreamPartition<Integer> partition)
 
 Review comment:
   from the API docs it seems like calling consumer.position() doesn't change 
the position of the pointer
   ```
   public long position(TopicPartition partition)
   Get the offset of the next record that will be fetched (if a record with 
that offset exists).
   ```
   

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to