Last active
April 7, 2018 14:02
-
-
Save pradp/dfa9121f3ff2558df1692ceb6f32f58b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.kafka.clients.CommonClientConfigs; | |
import org.apache.kafka.clients.consumer.ConsumerRecord; | |
import org.apache.kafka.clients.consumer.ConsumerRecords; | |
import org.apache.kafka.clients.consumer.KafkaConsumer; | |
import java.util.Arrays; | |
import java.util.Properties; | |
public class SampleConsumer { | |
public static void main(String[] args) throws Exception { | |
String topic = "sample-topic"; | |
Properties props = new Properties(); | |
props.put("bootstrap.servers", "kafka-a.company.com:9092,kafka-b.company.com:9092,kafka-c.company.com:9092"); | |
props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_SSL"); | |
props.put("sasl.mechanism", "PLAIN"); | |
props.put("group.id", "sample-group"); | |
props.put("enable.auto.commit", "true"); | |
props.put("auto.commit.interval.ms", "1000"); | |
props.put("session.timeout.ms", "30000"); | |
props.put("key.deserializer", | |
"org.apache.kafka.common.serialization.StringDeserializer"); | |
props.put("value.deserializer", | |
"org.apache.kafka.common.serialization.StringDeserializer"); | |
props.put("auto.offset.reset", "earliest"); | |
KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props); | |
consumer.subscribe(Arrays.asList(topic)); | |
boolean flag = true; | |
while (true) { | |
try { | |
ConsumerRecords<String, String> records = consumer.poll(100); | |
if (flag) { | |
consumer.seekToBeginning(consumer.assignment()); | |
flag = false; | |
} | |
for (ConsumerRecord<String, String> record : records) { | |
System.out.printf("offset = %d, key = %s, value = %s\n", | |
record.offset(), record.key(), record.value()); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment