将一个类序列化以放入Confluent模式注册表。

huangapple go评论72阅读模式
英文:

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&lt;TotallyCoolCustomClass&gt;();
            var schemaData = schema.ToJson();

            var subject = &quot;TotallyCoolCustomClass&quot;; //Assuming we want to use the Record strategy
            var schemaId = await schemaRegistry.RegisterSchemaAsync(subject, new Schema(schemaData, new List&lt;SchemaReference&gt;(), Confluent.SchemaRegistry.SchemaType.Json));

huangapple
  • 本文由 发表于 2023年7月3日 21:44:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定