Avro messages with schema - apache-kafka

So we are planning to use Avro for communication over a confluent kafka-based ecosystem. My current understanding of Avro is that each message carries its schema. If that is the case, we need schema registry just for resolving version updates?
I ask since carrying the schema with each message prevents the need for something like a schema registry to map a message id to a schema. Or am I missing something here?

When you run the Confluent Schema Registry, the Kafka messages published with the Confluent Avro Serdes library do not contain the avro schema. They only contain a numeric Schema id that is used by the consumers deserializer to fetch the Schema from the Confluent Schema Registry. These schemas are cached by the serializer and deserializer as a further performance optimization.

Related

Where do Kafka consumers get the first version of Avro schema from?

Most Quarkus examples that illustrate the use of Avro and Service registry combine consumer and producer in the same project. In doing so, the schema and the generated classes are available to the producer and the consumer.
I understand that the role of the schema registry is to maintain various versions of a schema and make them available to consumers.
What I do not fully understand is how and when the consumer pulls the schema from the registry. For instance, how does the consumer's team get the initial version of the schema? Do they just go to the registry and download it manually? Do they use the maven plugin to download it and generate the sources?
In the case of Quarkus, the avro extension automatically generates the Java source from avro schema. I wonder if it also downloads the initial schema for consumers.
The consumers can optionally download the schema from the registry. Then the consumer should be configured to use a SpecificRecord deserialization.
For the Confluent serialization format, the Schema ID is embedded in each record. When the consumer deserializer processes the bytes, it'll always perform a lookup from the registry for that ID.
Avro requires both a writer schema (the one that's downloaded from that ID), and a reader schema (either the one that the consumer app developer has downloaded, or defaults to the writer schema), and then schema evolution is evaluated.
Quarkus/Kafka doesn't really matter, here. It's entirely done within the Avro library after HTTP calls to the Registry.

What is the use of confluent schema registry if Kafka can use Avro without it

The difference between vanilla apache Avro and Avro with confluent schema registry is that when using apache avro , we send schema+message in kafka topic whereas in confluent schema registry , we send schemaID+message in kafka topic ? So here , schema registry helps in performance improvement via schema look up in registry. Is there any other benefit of using confluent schema registry ? Also , does apache avro supports compatabilty rules of schema evolution like schema registry ?
Note: There are other implementations of a "Schema Registry" that can use used with Kafka.
Here are a list of reasons
Clients can discover schemas without interacting with Kafka. For example, Apache Hive / Presto / Spark can download schemas from the Registry to perform analytics.
The registry is centrally responsible for compatibility checks rather than pushing each client to operate that on their own (to answer your second question)
The same applies to any serialization format, as well, not only Avro

Sending Avro messages to Kafka

I have an app that produces an array of messages in raw JSON periodically. I was able to convert that to Avro using the avro-tools. I did that because I needed the messages to include schema due to the limitations of Kafka-Connect JDBC sink. I can open this file on notepad++ and see that it includes the schema and a few lines of data.
Now I would like to send this to my central Kafka Broker and then use Kafka Connect JDBC sink to put the data in a database. I am having a hard time understanding how I should be sending these Avro files I have to my Kafka Broker. Do I need a schema registry for my purposes? I believe Kafkacat does not support Avro so I suppose I will have to stick with the kafka-producer.sh that comes with the Kafka installation (please correct me if I am wrong).
Question is: Can someone please share the steps to produce my Avro file to a Kafka broker without getting Confluent getting involved.
Thanks,
To use the Kafka Connect JDBC Sink, your data needs an explicit schema. The converter that you specify in your connector configuration determines where the schema is held. This can either be embedded within the JSON message (org.apache.kafka.connect.json.JsonConverter with schemas.enabled=true) or held in the Schema Registry (one of io.confluent.connect.avro.AvroConverter, io.confluent.connect.protobuf.ProtobufConverter, or io.confluent.connect.json.JsonSchemaConverter).
To learn more about this see https://www.confluent.io/blog/kafka-connect-deep-dive-converters-serialization-explained
To write an Avro message to Kafka you should serialise it as Avro and store the schema in the Schema Registry. There is a Go client library to use with examples
without getting Confluent getting involved.
It's not entirely clear what you mean by this. The Kafka Connect JDBC Sink is written by Confluent. The best way to manage schemas is with the Schema Registry. If you don't want to use the Schema Registry then you can embed the schema in your JSON message but it's a suboptimal way of doing things.

How to auto-save Avro schema in Confluent Schema Registry from Apache NiFi flow?

How to auto-save Avro schema in Confluent Schema Registry from Apache NiFi flow?
That's basically the question.
I am not finding the way of automatically storing the Avro schema of the record in the Confluent Schema Registry from a NiFi flow. It is possible to flexibly read and populate message with the reference to the schema in the Confluent Schema-Registry, but there should be a way of auto-creating one in the registry instead of demanding Confluent Schema-Registry to be initialized upfront before NiFi flow starts.
Update
Here is my current Flow:
I'm reading from a Postgres table using QueryDatabaseTableRecord processor (version 1.10) and publishing [new] records to a Kafka topic using PublishKafkaRecord_2_0 (version 1.10.0).
I want to publish to Kafka in Avro format storing (and passing around) the Avro schema in the Confluent Schema Registry (that works well in other places of my NiFi setup).
For that, I am using AvroRecordSetWriter in the "Record Writer" property on the QueryDatabaseTableRecord processor with the following properties:
PublishKafkaRecord processor is configured to read Avro schema from the input message (using the Confluent schema registry, the schema is not embedded into each FlowFile) and uses same AvroRecordSetWriter as QueryDatabaseTableRecord processor to write to Kafka.
That's basically it.
Trying to replace the first AvroRecordSetWriter with one that embeds the schema with the hope that the second AvroRecordSetWriter could auto generate schema in the Confluent Schema Registry on publish, since I don't want to bloat each message with my embedded Avro schema.
Update
I've tried to follow the advice from the comment as follows
With that I was trying to make first access to the Confluent Schema Registry the last step in the chain. Unfortunately, my attempts were unsuccessful. The only option that worked was my initial described in this question that required a schema in the registry upfront/in advance to work.
Both other cases that I tried ended up with the exception:
org.apache.nifi.schema.access.SchemaNotFoundException: Cannot write Confluent Schema Registry Reference because the Schema Identifier is not known
Please note, that I cannot use "Inherit Schema from record" in the last writer's schema access, since I'm getting an invalid combination and the NiFi config validation doesn't pass such combination through.

How to publish and consume nifi json to avro and avro to json content to kafka using NiFi

I am new to both NiFi and Avro. So, according to my understanding if we use schema registry the schema won't be added to Avro content that is being published to Kafka, only schema ID will be sent is that correct??
How can I publish and consume through Kafka using Horton works Schema Registry, using Avro serialization and deserialization?
In Nifi ConvertJsonToAvro schema will be embedded while sending.SO, is there any other processor which will use schema registry and won't send schema while publishing.
On publishing side you would use PublishKafkaRecord (with the version corresponding to your Kafka broker) and you would configure it with a JsonTreeReader and an AvroRecordSetWriter. In the record writer you would configure the Schema Write Strategy as Hortonworks Content Encoded.
On consuming side your would ConsumeKafkaRecord (same version as publish) and you would configure it with an AvroRecordReader and a JsonRecordWriter. In the reader you would configure the Schema Access Strategy as Hortonworks Content Encoded.