Skip to content

Instantly share code, notes, and snippets.

@jwkidd3
Created February 7, 2022 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwkidd3/83c5565bb5a99c409f7d8ee0cb2f7b37 to your computer and use it in GitHub Desktop.
Save jwkidd3/83c5565bb5a99c409f7d8ee0cb2f7b37 to your computer and use it in GitHub Desktop.
package com.kiddcorp;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.avro.generic.GenericRecord;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Properties;
import java.util.Random;
public class AvroConsumer {
public static void main(String[] args) {
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "io.confluent.kafka.serializers.KafkaAvroDeserializer");
props.put("schema.registry.url", "http://localhost:8081");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
String topic = "topic1";
final Consumer < String, GenericRecord > consumer = new KafkaConsumer < String, GenericRecord > (props);
consumer.subscribe(Arrays.asList(topic));
try {
while (true) {
ConsumerRecords < String, GenericRecord > records = consumer.poll(100);
for (ConsumerRecord < String, GenericRecord > record: records) {
System.out.printf("offset = %d, key = %s, value = %s \n", record.offset(), record.key(), record.value());
}
}
} finally {
consumer.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment