public async Task <byte[]> SerializeAsync(T data, SerializationContext context)
        {
            string subject = $"{context.Topic }-{context.Component}";
            int    id      = 0;
            var    schema  = AvroConvert.GenerateSchema(typeof(T));

            if (cache.ContainsKey(subject))
            {
                id = cache[subject];
            }
            else
            {
                var existingSchema = await RegistryClient.GetLatestSchemaAsync(subject);

                if (existingSchema.SchemaString == schema)
                {
                    cache.AddOrUpdate(subject, existingSchema.Id, (key, oldValue) => existingSchema.Id);
                }
                else
                {
                    Confluent.SchemaRegistry.Schema confluentSchema = new Confluent.SchemaRegistry.Schema(
                        subject,
                        existingSchema.Version + 1,
                        0,
                        schema
                        );

                    id = await RegistryClient.RegisterSchemaAsync(subject, confluentSchema.ToString());

                    cache.AddOrUpdate(subject, id, (key, oldValue) => id);
                }
            }

            using (var stream = new MemoryStream())
            {
                //Confluent Kafka format:
                //https://docs.confluent.io/current/schema-registry/docs/serializer-formatter.html#wire-format

                //Magic number
                stream.WriteByte(0x00);

                //Id
                var idAsBytes = BitConverter.GetBytes(id);
                stream.Write(idAsBytes, 0, idAsBytes.Length);

                //Data
                var serializedData = AvroConvert.SerializeHeadless(data, schema);
                stream.Write(serializedData, 0, serializedData.Length);

                return(stream.ToArray());
            }
        }
示例#2
0
 /// <inheritdoc/>
 public async Task <bool> IsCompatibleAsync(string subject, Schema schema)
 => await restService.TestLatestCompatibilityAsync(subject, schema).ConfigureAwait(continueOnCapturedContext: false);
 public async Task<bool> TestCompatibilityAsync(string subject, int versionId, Schema schema)
     => schema.SchemaType == SchemaType.Avro
         // In the avro case, just send the schema string to maintain backards compatibility.
         ? (await RequestAsync<CompatibilityCheck>($"compatibility/subjects/{subject}/versions/{versionId}", HttpMethod.Post, new SchemaString(schema.SchemaString))
                 .ConfigureAwait(continueOnCapturedContext: false)).IsCompatible
         : (await RequestAsync<CompatibilityCheck>($"compatibility/subjects/{subject}/versions/{versionId}", HttpMethod.Post, schema)
                 .ConfigureAwait(continueOnCapturedContext: false)).IsCompatible;
示例#4
0
 /// <inheritdoc/>
 public Task <RegisteredSchema> LookupSchemaAsync(string subject, Schema schema, bool ignoreDeletedSchemas)
 => restService.LookupSchemaAsync(subject, schema, ignoreDeletedSchemas);