Use Avro schema with Kafka
30 minutes
Use command line or Kafka Manager to create a customer
topic
This is using confluent kafka
# be in confluent directory
$ cd ~/apps/confluent
$ ./bin/kafka-topics --bootstrap-server localhost:9092 \
--create --topic customer --replication-factor 1 --partitions 5
Avro schema file : src/main/java/x/lab08_avro/customer_v1.avsc
{"namespace": "x.lab08_avro",
"type": "record",
"name": "Customer",
"fields": [
{"name": "id", "type": "int"},
{"name": "name", "type": "string"},
{"name": "email", "type": "string"}
]
}
If you don't have Avro tools, download them as follows:
$ mkdir -p ~/apps/avro
$ wget -O ~/apps/avro/avro-tools.jar \
https://dlcdn.apache.org/avro/stable/java/avro-tools-1.12.0.jar
$ wget -O ~/apps/avro/avro.jar \
https://dlcdn.apache.org/avro/stable/java/avro-1.12.0.jar
Verify Avro tools directory:
$ ls -lh ~/apps/avro
You will see downloaded jar files
-rw-rw-r-- 1 sujee sujee 596K Jul 26 13:08 avro.jar
-rw-rw-r-- 1 sujee sujee 53M Jul 26 13:08 avro-tools.jar
We are going to compile the avro schema into a Java file
$ cd ~/dev/kafka-labs # be in project root directory
$ java -jar ~/apps/avro/avro-tools.jar \
compile schema \
src/main/java/x/lab08_avro/customer_v1.avsc \
src/main/java/
This will generate Customer.java
file.
Note: To force compile, try deleting the Customer.java
file and run the compile again
==> Inspect the file here : src/main/java/x/lab08_avro/Customer.java
In Eclipse you can force compile the project like this : Project --> Clean --> kafka-api
Make sure the compile is clean with no errors.
Also do a maven compile as follows
$ cd ~/dev/kafka-labs # be in project root directory
$ mvn clean package -DskipTests
Make sure the compile is clean with no errors.
Inspect Consumer: src/main/java/x/lab08_avro/AvroConsumer.java
Run the consumer.
Note: You will need schema registry running to make this work
Inspect producer: src/main/java/x/lab08_avro/AvroProducer.java
Run the producer.
Note: You will need schema registry running to make this work
==> Observe events going from producer to consumer
Query Schema Registry as follows
$ curl --silent -X GET http://localhost:8081/schemas/
Better formatting with jq
$ curl --silent -X GET http://localhost:8081/schemas/ | jq
Note: if jq
is mising, you can install it as follows:
$ sudo apt install -y jq
Once you are done, you can shutdown the Confluent stack