KEY - Type of message keyPAYLOAD - Type of message valueOFFSET - Type of offset objectpublic abstract class KafkaSimpleApiConsumer<KEY,PAYLOAD,OFFSET> extends Object
Wrapper class for Apache Kafka consumer using the simple APIs to manage the last offset for a particular
partition of a topic.
Most of the code are borrowed from the cdap-kafka-pack project and modified to be suit the stream source.
| Modifier and Type | Field and Description |
|---|---|
protected KafkaSource |
kafkaSource |
protected static int |
SO_TIMEOUT |
| Modifier | Constructor and Description |
|---|---|
protected |
KafkaSimpleApiConsumer(KafkaSource kafkaSource) |
| Modifier and Type | Method and Description |
|---|---|
protected co.cask.cdap.api.data.format.StructuredRecord |
byteBufferToStructuredRecord(String key,
ByteBuffer payload) |
protected abstract void |
configureKafka(KafkaConfigurer configurer)
Configure Kafka consumer.
|
protected KEY |
decodeKey(ByteBuffer buffer)
Override this method to provide custom decoding of a message key.
|
protected PAYLOAD |
decodePayload(ByteBuffer buffer)
Override this method to provide custom decoding of a message payload.
|
void |
destroy()
Should be called for clean up.
|
protected abstract OFFSET |
getBeginOffset(org.apache.twill.kafka.client.TopicPartition topicPartition)
Returns the read offsets to start with for the given
TopicPartition. |
protected abstract long |
getDefaultOffset(org.apache.twill.kafka.client.TopicPartition topicPartition)
Returns the default value of offset to start with when encounter a new broker for a given topic partition.
|
protected KafkaConfig |
getKafkaConfig()
Returns a Kafka configuration.
|
String |
getName() |
protected Map<String,byte[]> |
getOffsetStore() |
Map<String,byte[]> |
getSavedState() |
protected String |
getStoreKey(org.apache.twill.kafka.client.TopicPartition topicPartition)
Returns the key to be used when persisting offsets into the
SourceState. |
void |
initialize(co.cask.cdap.etl.api.realtime.RealtimeContext context)
The method should be called to initialized the consumer when being used by the
RealtimeSource |
void |
pollMessages(co.cask.cdap.etl.api.Emitter<co.cask.cdap.api.data.format.StructuredRecord> emitter)
Will be called by external source to start poll the Kafka messages one at the time.
|
protected void |
processMessage(KafkaMessage<OFFSET> message,
co.cask.cdap.etl.api.Emitter<co.cask.cdap.api.data.format.StructuredRecord> emitter)
Overrides this method if interested in the raw Kafka message.
|
protected void |
processMessage(KEY key,
PAYLOAD payload,
co.cask.cdap.etl.api.Emitter<co.cask.cdap.api.data.format.StructuredRecord> emitter)
Override this method if interested in both the key and payload of a message read from Kafka.
|
protected abstract void |
processMessage(PAYLOAD payload,
co.cask.cdap.etl.api.Emitter<co.cask.cdap.api.data.format.StructuredRecord> emitter)
Override this method if only interested in the payload of a message read from Kafka.
|
protected abstract Iterator<KafkaMessage<OFFSET>> |
readMessages(KafkaConsumerInfo<OFFSET> consumerInfo)
Read messages from Kafka.
|
protected abstract void |
saveReadOffsets(Map<org.apache.twill.kafka.client.TopicPartition,OFFSET> offsets)
Persists read offsets for all topic-partition that this Flowlet consumes from Kafka.
|
void |
saveState(co.cask.cdap.etl.api.realtime.SourceState currentState)
Set the internal state for the Topic partition.
|
protected void |
stopService(com.google.common.util.concurrent.Service service)
Stops a
Service and waits for the completion. |
protected static final int SO_TIMEOUT
protected final KafkaSource kafkaSource
protected KafkaSimpleApiConsumer(KafkaSource kafkaSource)
public void initialize(co.cask.cdap.etl.api.realtime.RealtimeContext context)
throws Exception
The method should be called to initialized the consumer when being used by the RealtimeSource
context - Exceptionpublic String getName()
public Map<String,byte[]> getSavedState()
public void saveState(co.cask.cdap.etl.api.realtime.SourceState currentState)
public void pollMessages(co.cask.cdap.etl.api.Emitter<co.cask.cdap.api.data.format.StructuredRecord> emitter)
throws Exception
emitter - instance of Emitter to emit the messages.Exceptionprotected abstract void configureKafka(KafkaConfigurer configurer)
configurer - for configuring consuming from Kafkaprotected abstract Iterator<KafkaMessage<OFFSET>> readMessages(KafkaConsumerInfo<OFFSET> consumerInfo) throws Exception
consumerInfo - Contains information about where to fetch messages fromIterator containing sequence of messages read from Kafka. The first message must
has offset no earlier than the KafkaConsumerInfo.getReadOffset() as given in the parameter.Exceptionprotected abstract OFFSET getBeginOffset(org.apache.twill.kafka.client.TopicPartition topicPartition)
TopicPartition.protected abstract void saveReadOffsets(Map<org.apache.twill.kafka.client.TopicPartition,OFFSET> offsets)
public void destroy()
protected final KafkaConfig getKafkaConfig()
protected void processMessage(KafkaMessage<OFFSET> message, co.cask.cdap.etl.api.Emitter<co.cask.cdap.api.data.format.StructuredRecord> emitter)
message - The message fetched from Kafka.emitter - The emitter to push the messageprotected void processMessage(KEY key, PAYLOAD payload, co.cask.cdap.etl.api.Emitter<co.cask.cdap.api.data.format.StructuredRecord> emitter)
key - Key decoded from the messagepayload - Payload decoded from the messageemitter - The emitter to push the messageprotected abstract void processMessage(PAYLOAD payload, co.cask.cdap.etl.api.Emitter<co.cask.cdap.api.data.format.StructuredRecord> emitter)
payload - Payload decoded from the messageemitter - The emitter to push the messageprotected abstract long getDefaultOffset(org.apache.twill.kafka.client.TopicPartition topicPartition)
OffsetRequest.EarliestTime(). Sub-classes can override this to return
different value (for example OffsetRequest.LatestTime()).@Nullable protected KEY decodeKey(ByteBuffer buffer)
buffer - The bytes representing the key in the Kafka message@Nullable protected PAYLOAD decodePayload(ByteBuffer buffer)
buffer - The bytes representing the payload in the Kafka messageprotected final void stopService(com.google.common.util.concurrent.Service service)
Service and waits for the completion. If there is exception during stop, it will get logged.protected String getStoreKey(org.apache.twill.kafka.client.TopicPartition topicPartition)
SourceState.protected co.cask.cdap.api.data.format.StructuredRecord byteBufferToStructuredRecord(@Nullable String key, ByteBuffer payload)
Copyright © 2016 Cask Data, Inc. Licensed under the Apache License, Version 2.0.