riferrei / srclient

Golang Client for Schema Registry
Apache License 2.0
236 stars 70 forks source link

How to use the schema string #13

Closed alok87 closed 4 years ago

alok87 commented 4 years ago

Output:

$ go run schema_get.go
{"type":"record","name":"Envelope","namespace":"datapipe.inventory.customers","fields":[{"name":"before","type":["null",{"type":"record","name":"Value","fields":[{"name":"id","type":"int"},{"name":"first_name","type":"string"},{"name":"last_name","type":"string"},{"name":"email","type":"string"}],"connect.name":"datapipe.inventory.customers.Value"}],"default":null},{"name":"after","type":["null","Value"],"default":null},{"name":"source","type":{"type":"record","name":"Source","namespace":"io.debezium.connector.mysql","fields":[{"name":"version","type":"string"},{"name":"connector","type":"string"},{"name":"name","type":"string"},{"name":"ts_ms","type":"long"},{"name":"snapshot","type":[{"type":"string","connect.version":1,"connect.parameters":{"allowed":"true,last,false"},"connect.default":"false","connect.name":"io.debezium.data.Enum"},"null"],"default":"false"},{"name":"db","type":"string"},{"name":"table","type":["null","string"],"default":null},{"name":"server_id","type":"long"},{"name":"gtid","type":["null","string"],"default":null},{"name":"file","type":"string"},{"name":"pos","type":"long"},{"name":"row","type":"int"},{"name":"thread","type":["null","long"],"default":null},{"name":"query","type":["null","string"],"default":null}],"connect.name":"io.debezium.connector.mysql.Source"}},{"name":"op","type":"string"},{"name":"ts_ms","type":["null","long"],"default":null},{"name":"transaction","type":["null",{"type":"record","name":"ConnectDefault","namespace":"io.confluent.connect.avro","fields":[{"name":"id","type":"string"},{"name":"total_order","type":"long"},{"name":"data_collection_order","type":"long"}]}],"default":null}],"connect.name":"datapipe.inventory.customers.Envelope"}

Program:

package main

import (
    "fmt"
    "github.com/riferrei/srclient"
)

func main() {
    schemaRegistryClient := srclient.CreateSchemaRegistryClient("https://schema-XX.XX.com")
    s, err := schemaRegistryClient.GetSchema(6)
     if err != nil {
        panic(err)
     }
     fmt.Printf("schema:\n%+v\n\n", s.Schema())
}

Schema in this is a string (i mean s.Schema()). I need to construct a postgres statement from this schema. Parsing this scheme is a big pain. Do we have some golang Debezium structs for the debezium schema which i can unmarshal to...

riferrei commented 4 years ago

I don't think there is a way to generically create structs that will represent any type of schema defined. Schemas will always be per-project, each one of their own characteristics.

Unless you know any method I could follow. Otherwise I think we can close this issue.

-- Ricardo

alok87 commented 4 years ago

I was finally solve it the way u are suggesting by parsing. https://stackoverflow.com/questions/63564543/decode-a-debeuzium-event-schema-into-a-meaningful-datastructure-in-golang thank you.