Package com.github.charithe.kafka
Class KafkaHelper
java.lang.Object
com.github.charithe.kafka.KafkaHelper
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescription<K,V> com.google.common.util.concurrent.ListenableFuture<List<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>> consume(String topic, org.apache.kafka.clients.consumer.KafkaConsumer<K, V> consumer, int numMessagesToConsume) Attempt to consume the specified number of messagesGet the consumer configuration (with auto-commit enabled)consumerConfig(boolean enableAutoCommit) Get the consumer configurationconsumeStrings(String topic, int numMessagesToConsume) Consume specified number of string messagesorg.apache.kafka.clients.consumer.KafkaConsumer<byte[],byte[]> Create a consumer that reads bytesorg.apache.kafka.clients.consumer.KafkaConsumer<byte[],byte[]> createByteConsumer(Properties overrideConfig) Create a consumer that reads bytesorg.apache.kafka.clients.producer.KafkaProducer<byte[],byte[]> Create a producer that writes byte keys and valuesorg.apache.kafka.clients.producer.KafkaProducer<byte[],byte[]> createByteProducer(Properties overrideConfig) Create a producer that writes byte keys and values<K,V> org.apache.kafka.clients.consumer.KafkaConsumer<K, V> createConsumer(org.apache.kafka.common.serialization.Deserializer<K> keyDeserializer, org.apache.kafka.common.serialization.Deserializer<V> valueDeserializer, Properties overrideConfig) Create a consumer that can read from this brokerstatic KafkaHelpercreateFor(EphemeralKafkaBroker broker) <K,V> org.apache.kafka.clients.producer.KafkaProducer<K, V> createProducer(org.apache.kafka.common.serialization.Serializer<K> keySerializer, org.apache.kafka.common.serialization.Serializer<V> valueSerializer, Properties overrideConfig) Create a producer that can write to this brokerCreate a consumer that reads stringscreateStringConsumer(Properties overrideConfig) Create a consumer that reads stringsCreate a producer that writes String keys and valuescreateStringProducer(Properties overrideConfig) Create a producer that writes String keys and valuesintGet the broker listener port<K,V> void Produce data to the specified topicGet the producer configurationvoidproduceStrings(String topic, String... values) Convenience method to produce a set of strings to the specified topicGet the zookeeper connection stringintGet the zookeeper port
-
Method Details
-
createFor
-
producerConfig
Get the producer configuration- Returns:
- Properties
-
consumerConfig
Get the consumer configuration (with auto-commit enabled)- Returns:
- Properties
-
consumerConfig
Get the consumer configuration- Parameters:
enableAutoCommit- Enable auto-commit- Returns:
- Properties
-
zookeeperConnectionString
Get the zookeeper connection string- Returns:
- zookeeper connection string or
IllegalStateExceptionif the broker is not running
-
zookeeperPort
public int zookeeperPort()Get the zookeeper port- Returns:
- zookeeper port of or
IllegalStateExceptionif the broker is not running
-
kafkaPort
public int kafkaPort()Get the broker listener port- Returns:
- broker listener port or
IllegalStateExceptionif the broker is not running
-
createProducer
public <K,V> org.apache.kafka.clients.producer.KafkaProducer<K,V> createProducer(org.apache.kafka.common.serialization.Serializer<K> keySerializer, org.apache.kafka.common.serialization.Serializer<V> valueSerializer, Properties overrideConfig) Create a producer that can write to this broker- Type Parameters:
K- Type of KeyV- Type of Value- Parameters:
keySerializer- Key serializer classvalueSerializer- Valuer serializer classoverrideConfig- Producer config to override. Pass null if there aren't any.- Returns:
- KafkaProducer
-
createStringProducer
Create a producer that writes String keys and values- Returns:
- KafkaProducer
-
createStringProducer
public org.apache.kafka.clients.producer.KafkaProducer<String,String> createStringProducer(Properties overrideConfig) Create a producer that writes String keys and values- Parameters:
overrideConfig- Producer config to override- Returns:
- KafkaProducer
-
createByteProducer
public org.apache.kafka.clients.producer.KafkaProducer<byte[],byte[]> createByteProducer()Create a producer that writes byte keys and values- Returns:
- KafkaProducer
-
createByteProducer
public org.apache.kafka.clients.producer.KafkaProducer<byte[],byte[]> createByteProducer(Properties overrideConfig) Create a producer that writes byte keys and values- Parameters:
overrideConfig- Producer config to override- Returns:
- KafkaProducer
-
produce
public <K,V> void produce(String topic, org.apache.kafka.clients.producer.KafkaProducer<K, V> producer, Map<K, V> data) Produce data to the specified topic- Type Parameters:
K- Type of keyV- Type of value- Parameters:
topic- Topic to produce toproducer- Producer to usedata- Data to produce
-
produceStrings
Convenience method to produce a set of strings to the specified topic- Parameters:
topic- Topic to produce tovalues- Values produce
-
createConsumer
public <K,V> org.apache.kafka.clients.consumer.KafkaConsumer<K,V> createConsumer(org.apache.kafka.common.serialization.Deserializer<K> keyDeserializer, org.apache.kafka.common.serialization.Deserializer<V> valueDeserializer, Properties overrideConfig) Create a consumer that can read from this broker- Type Parameters:
K- Type of KeyV- Type of Value- Parameters:
keyDeserializer- Key deserializervalueDeserializer- Value deserializeroverrideConfig- Consumer config to override. Pass null if there aren't any- Returns:
- KafkaConsumer
-
createStringConsumer
Create a consumer that reads strings- Returns:
- KafkaConsumer
-
createStringConsumer
public org.apache.kafka.clients.consumer.KafkaConsumer<String,String> createStringConsumer(Properties overrideConfig) Create a consumer that reads strings- Parameters:
overrideConfig- Consumer config to override- Returns:
- KafkaConsumer
-
createByteConsumer
public org.apache.kafka.clients.consumer.KafkaConsumer<byte[],byte[]> createByteConsumer()Create a consumer that reads bytes- Returns:
- KafkaConsumer
-
createByteConsumer
public org.apache.kafka.clients.consumer.KafkaConsumer<byte[],byte[]> createByteConsumer(Properties overrideConfig) Create a consumer that reads bytes- Parameters:
overrideConfig- Consumer config to override- Returns:
- KafkaConsumer
-
consume
public <K,V> com.google.common.util.concurrent.ListenableFuture<List<org.apache.kafka.clients.consumer.ConsumerRecord<K,V>>> consume(String topic, org.apache.kafka.clients.consumer.KafkaConsumer<K, V> consumer, int numMessagesToConsume) Attempt to consume the specified number of messages- Type Parameters:
K- Type of KeyV- Type of Value- Parameters:
topic- Topic to consumeconsumer- Consumer to usenumMessagesToConsume- Number of messages to consume- Returns:
- ListenableFuture
-
consumeStrings
public com.google.common.util.concurrent.ListenableFuture<List<String>> consumeStrings(String topic, int numMessagesToConsume) Consume specified number of string messages- Parameters:
topic- Topic to consume fromnumMessagesToConsume- Number of messages to consume- Returns:
- ListenableFuture
-