add schema support

This commit is contained in:
“nishant-sharma”
2021-05-12 15:26:13 +05:30
parent f680bc4017
commit 666089458f
3 changed files with 57 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package producer
import (
"log"
"fmt"
"github.com/riferrei/srclient"
)
@@ -29,5 +30,33 @@ func GetSchemaVersions() {
}
log.Println(SchemaVersionMap)
}
func AddSchema(topic string, schemaType string, schema string) error {
schemaRegistryClient := srclient.CreateSchemaRegistryClient(SchemaRegistryEndpoint)
schemaRegistryClient.CodecCreationEnabled(false)
existingSchema, _ := schemaRegistryClient.GetLatestSchema(topic, false)
fmt.Println("EXISTINGSCHEMA: ")
fmt.Println(existingSchema)
if existingSchema != nil {
compatible, errorCompatible := schemaRegistryClient.IsSchemaCompatible(topic, schema, "latest", srclient.SchemaType(schemaType), false)
if errorCompatible != nil {
return errorCompatible
} else if (compatible == false) {
return fmt.Errorf("given schema not compatible with the latest version")
}
}
_, errroCreate := schemaRegistryClient.CreateSchema(topic, schema, srclient.SchemaType(schemaType), false)
// fmt.Println("SCHEMA_ID: ")
// fmt.Println(schemaObj.ID())
if errroCreate != nil {
return errroCreate
} else {
return nil
}
}