英文:
Serializing A Class To Place On Confluent Schema Registry
问题
我正在尝试手动注册Confluent Schema注册表中的模式,调用RegisterSchemaAsync。
我已尝试多种序列化我的类的方法,包括newtonsoft json schema和confluent的json序列化器,但它们似乎不是完全正确的格式。
newtonsoft的方法似乎最接近我想要的内容:
JSchemaGenerator generator = new JSchemaGenerator();
JSchema schema = generator.Generate(typeof(TotallyCoolCustomClass));
var subject = "TotallyCoolCustomClass"; //假设我们想使用Record策略
var stringSchema = schema.ToString();
var schemaId = await schemaRegistry.RegisterSchemaAsync(subject, schema.ToString());
提供的结果:
'Invalid schema {subject=TotallyCoolCustomClass,version=0,id=-1,schemaType=AVRO,references=[],metadata=null,ruleSet=null,schema={
"type": "object",
"properties": {
"FavouriteQuote": {
"type": [
"string",
"null"
]
},
"FavouriteNumber": {
"type": "integer"
}
},
"required": [
"FavouriteQuote",
"FavouriteNumber"
]
}} with refs [] of type AVRO, details: Type not supported: object; error code: 42201'
有关如何将类定义序列化为模式的正确格式的建议吗?
英文:
I'm attempting to manually register a schema with Confluents Schema registry calling RegisterSchemaAsync.
I've attempted multiple methods of serializing my class, including newton soft json schema and confluents json serializer, but they don't seem to be in quite the right format.
The newtonsoft one seems to give the closest to what I'm after:
JSchemaGenerator generator = new JSchemaGenerator();
JSchema schema = generator.Generate(typeof(TotallyCoolCustomClass));
var subject = "TotallyCoolCustomClass"; //Assuming we want to use the Record strategy
var stringSchema = schema.ToString();
var schemaId = await schemaRegistry.RegisterSchemaAsync(subject, schema.ToString());
Provides:
'Invalid schema {subject=TotallyCoolCustomClass,version=0,id=-1,schemaType=AVRO,references=[],metadata=null,ruleSet=null,schema={
"type": "object",
"properties": {
"FavouriteQuote": {
"type": [
"string",
"null"
]
},
"FavouriteNumber": {
"type": "integer"
}
},
"required": [
"FavouriteQuote",
"FavouriteNumber"
]
}} with refs [] of type AVRO, details: Type not supported: object; error code: 42201'
Any suggestions on how I can serialize a class definition into the right format for the schema?
答案1
得分: 1
使用了 NJsonSchema。
var schemaRegistry = new CachedSchemaRegistryClient(schemaRegistryConfig);
var schema = JsonSchema.FromType<TotallyCoolCustomClass>();
var schemaData = schema.ToJson();
var subject = "TotallyCoolCustomClass"; // 假设我们想要使用记录策略
var schemaId = await schemaRegistry.RegisterSchemaAsync(subject, new Schema(schemaData, new List<SchemaReference>(), Confluent.SchemaRegistry.SchemaType.Json));
英文:
Ended up using NJsonSchema.
var schemaRegistry = new CachedSchemaRegistryClient(schemaRegistryConfig);
var schema = JsonSchema.FromType<TotallyCoolCustomClass>();
var schemaData = schema.ToJson();
var subject = "TotallyCoolCustomClass"; //Assuming we want to use the Record strategy
var schemaId = await schemaRegistry.RegisterSchemaAsync(subject, new Schema(schemaData, new List<SchemaReference>(), Confluent.SchemaRegistry.SchemaType.Json));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论