Package com.github.charithe.kafka
Class KafkaHelper
- java.lang.Object
-
- com.github.charithe.kafka.KafkaHelper
-
public class KafkaHelper extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKafkaHelper.RecordConsumer<K,V>
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <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 messagesPropertiesconsumerConfig()Get the consumer configuration (with auto-commit enabled)PropertiesconsumerConfig(boolean enableAutoCommit)Get the consumer configurationcom.google.common.util.concurrent.ListenableFuture<List<String>>consumeStrings(String topic, int numMessagesToConsume)Consume specified number of string messagesorg.apache.kafka.clients.consumer.KafkaConsumer<byte[],byte[]>createByteConsumer()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[]>createByteProducer()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 brokerorg.apache.kafka.clients.consumer.KafkaConsumer<String,String>createStringConsumer()Create a consumer that reads stringsorg.apache.kafka.clients.consumer.KafkaConsumer<String,String>createStringConsumer(Properties overrideConfig)Create a consumer that reads stringsorg.apache.kafka.clients.producer.KafkaProducer<String,String>createStringProducer()Create a producer that writes String keys and valuesorg.apache.kafka.clients.producer.KafkaProducer<String,String>createStringProducer(Properties overrideConfig)Create a producer that writes String keys and valuesintkafkaPort()Get the broker listener port<K,V>
voidproduce(String topic, org.apache.kafka.clients.producer.KafkaProducer<K,V> producer, Map<K,V> data)Produce data to the specified topicPropertiesproducerConfig()Get the producer configurationvoidproduceStrings(String topic, String... values)Convenience method to produce a set of strings to the specified topicStringzookeeperConnectionString()Get the zookeeper connection stringintzookeeperPort()Get the zookeeper port
-
-
-
Method Detail
-
createFor
public static KafkaHelper createFor(EphemeralKafkaBroker broker)
-
producerConfig
public Properties producerConfig()
Get the producer configuration- Returns:
- Properties
-
consumerConfig
public Properties consumerConfig()
Get the consumer configuration (with auto-commit enabled)- Returns:
- Properties
-
consumerConfig
public Properties consumerConfig(boolean enableAutoCommit)
Get the consumer configuration- Parameters:
enableAutoCommit- Enable auto-commit- Returns:
- Properties
-
zookeeperConnectionString
public String 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
public org.apache.kafka.clients.producer.KafkaProducer<String,String> 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
public void produceStrings(String topic, String... values)
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
public org.apache.kafka.clients.consumer.KafkaConsumer<String,String> 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
-
-