Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rachel-barrett/5f4354fcb846a46605e42964b8b97aaf to your computer and use it in GitHub Desktop.
Save rachel-barrett/5f4354fcb846a46605e42964b8b97aaf to your computer and use it in GitHub Desktop.
// In build.sbt: libraryDependencies += "org.apache.avro" % "avro" % "1.11.0"
/*
In org.apache.avro library there are two different versions of checking schema compatibility:
(1) SchemaValidator.validate
(2) SchemaComatibility.checkReaderWriterCompatibility
The first is deprecated, and not aligned with the second (for example changes to enums are completely ignored in (1)).
Interestingly it was only very recently (Feb 2021) that the Confluent schema-registry switched over to using (2):
https://github.com/confluentinc/schema-registry/pull/1775
*/
import org.apache.avro.Schema
import org.apache.avro.SchemaCompatibility
object AvroSchemaCompatibilityOfEnumsWithDefault {
def main(args: Array[String]) {
println(
"Messages produced by schema_0 can be read by schema_1: " +
isForwardsCompatible(schema_0, schema_1)
) // true
println(
"Messages produced by schema_1 can be read by schema_0: " +
isForwardsCompatible(schema_1, schema_0)
) // true (but arguably should be false)
}
def isForwardsCompatible(writer: Schema, reader: Schema): Boolean =
SchemaCompatibility
.checkReaderWriterCompatibility(reader, writer)
.getResult
.getIncompatibilities
.isEmpty
val schema_0 = Schema.parse(
"""
|{
| "type":"record",
| "name":"Form",
| "fields":[
| {
| "name":"rating",
| "type":
| {
| "type":"enum",
| "name":"Rating",
| "symbols":[
| "Excellent",
| "Good",
| "Bad"
| ],
| "default": "Good"
| }
| }
| ]
|}
""".stripMargin
)
val schema_1 = Schema.parse(
"""
|{
| "type":"record",
| "name":"Form",
| "fields":[
| {
| "name":"rating",
| "type":
| {
| "type":"enum",
| "name":"Rating",
| "symbols":[
| "Perfect",
| "Excellent",
| "Good",
| "Bad"
| ],
| "default": "Good"
| }
| }
| ]
|}
""".stripMargin
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment