https://github.com/picadoh/kafka-avro
An example integration of Kafka with Avro schemas
https://github.com/picadoh/kafka-avro
avro example java kafka
Last synced: 4 months ago
JSON representation
An example integration of Kafka with Avro schemas
- Host: GitHub
- URL: https://github.com/picadoh/kafka-avro
- Owner: picadoh
- Created: 2016-08-21T16:22:18.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-21T16:23:09.000Z (almost 10 years ago)
- Last Synced: 2025-08-24T17:31:09.166Z (10 months ago)
- Topics: avro, example, java, kafka
- Language: Java
- Size: 10.7 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Kafka Producer/Consumer Example
This example implements a Kafka producer and consumer that use a Avro schema mapper to communicate data in the topic.
#### Pre-requisites
- Kafka 0.9.0
- Maven 3
- Java 7/8
#### Setup environment
###### Start Kafka Server
kafka$ ./bin/zookeeper-server-start.sh config/zookeeper.properties
###### Start Zookeeper Server
kafka$ ./bin/kafka-server-start.sh config/server.properties
###### Create Kafka Topic
kafka$ ./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic example.test.topic
#### Build application
project$ mvn clean install
#### Example usage
String topicName = "example.test.topic";
// setup schema
Schema.Parser parser = new Schema.Parser();
Schema schema = parser.parse(AvroSchemaDefinitionLoader.fromFile("schema/tweet.avro").get());
// setup the mapper
DataMapper mapper = new AvroDataMapper(schema);
// setup consumer
DataProcessor processor = new AvroDataProcessor();
KafkaLibConsumer consumer = new KafkaLibConsumer<>(
topicName,
PropertiesLoader.fromFile("consumer.properties"),
mapper,
processor);
// set up the producer
KafkaLibProducer producer = new KafkaLibProducer<>(
topicName,
PropertiesLoader.fromFile("producer.properties"),
mapper);
GenericData.Record avroRecord = new GenericData.Record(schema);
avroRecord.put("username", "hugopicado");
avroRecord.put("tweet", "my #awesome tweet");
producer.produce(avroRecord);
consumer.consume();