-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathConsumer.java
More file actions
56 lines (50 loc) · 2.31 KB
/
Consumer.java
File metadata and controls
56 lines (50 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.microsoft.example;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.CommonClientConfigs;
import java.util.Properties;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Consumer {
private static final Logger logger = LoggerFactory.getLogger(Consumer.class);
public static int consume(String brokers, String groupId, String topicName) {
// Create a consumer
KafkaConsumer<String, String> consumer;
// Configure the consumer
Properties properties = new Properties();
// Point it to the brokers
properties.setProperty("bootstrap.servers", brokers);
// Set the consumer group (all consumers must belong to a group).
properties.setProperty("group.id", groupId);
// Set how to serialize key/value pairs
properties.setProperty("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
properties.setProperty("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
// When a group is first created, it has no offset stored to start reading from. This tells it to start
// with the earliest record in the stream.
properties.setProperty("auto.offset.reset","earliest");
// specify the protocol for Domain Joined clusters
properties.setProperty(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
consumer = new KafkaConsumer<>(properties);
// Subscribe to the 'test' topic
consumer.subscribe(Arrays.asList(topicName));
// Loop until ctrl + c
int count = 0;
while(true) {
// Poll for records
ConsumerRecords<String, String> records = consumer.poll(200);
// Did we get any?
if (records.count() == 0) {
// timeout/nothing to read
} else {
// Yes, loop over records
for(ConsumerRecord<String, String> record: records) {
// Display record and count
count += 1;
logger.info(count + ": " + record.value());
}
}
}
}
}