|
| 1 | +// Copyright 2023 Confluent Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// Refer to LICENSE for more information. |
| 16 | + |
| 17 | +using Confluent.Kafka.SyncOverAsync; |
| 18 | +using Confluent.SchemaRegistry; |
| 19 | +using Confluent.SchemaRegistry.Serdes; |
| 20 | +using System; |
| 21 | +using System.IO; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.Threading; |
| 24 | +using System.Threading.Tasks; |
| 25 | +using NJsonSchema.Generation; |
| 26 | +using Newtonsoft.Json; |
| 27 | +using Newtonsoft.Json.Serialization; |
| 28 | + |
| 29 | + |
| 30 | +/// <summary> |
| 31 | +/// An example of working with JSON schemas with external, |
| 32 | +/// references and Json data, Apache Kafka and |
| 33 | +/// Confluent Schema Registry (v5.5 or later required for |
| 34 | +/// JSON schema support). |
| 35 | +/// </summary> |
| 36 | +namespace Confluent.Kafka.Examples.JsonWithReferences |
| 37 | +{ |
| 38 | + |
| 39 | + /// <remarks> |
| 40 | + /// The deserializer allows multiple ways to consume data. |
| 41 | + /// |
| 42 | + /// If the consumer is aware of the entire schema details, |
| 43 | + /// they can create a class corresponding to it and use the |
| 44 | + /// deserializer in these ways: |
| 45 | + /// - without passing a schema, the deserializer will convert |
| 46 | + /// the serialized string to the object of this class. |
| 47 | + /// - pass a schema and allow validating against it. |
| 48 | + /// |
| 49 | + /// Note: The user can also pass JObject to the |
| 50 | + /// ConsumerBuilder<string, JObject> and JsonDeserializer<JObject> |
| 51 | + /// in order to get JObject instead in consumer, this is possible |
| 52 | + /// in the producer too. |
| 53 | + /// </remarks> |
| 54 | + public class Product |
| 55 | + { |
| 56 | + public long ProductId { get; set; } |
| 57 | + |
| 58 | + public string ProductName { get; set; } |
| 59 | + |
| 60 | + public decimal Price { get; set; } |
| 61 | + |
| 62 | + public List<string> Tags { get; set; } |
| 63 | + |
| 64 | + public Dimensions Dimensions { get; set; } |
| 65 | + |
| 66 | + public GeographicalLocation WarehouseLocation { get; set; } |
| 67 | + } |
| 68 | + |
| 69 | + public class Dimensions |
| 70 | + { |
| 71 | + public decimal Length { get; set; } |
| 72 | + |
| 73 | + public decimal Width { get; set; } |
| 74 | + |
| 75 | + public decimal Height { get; set; } |
| 76 | + } |
| 77 | + |
| 78 | + public class GeographicalLocation |
| 79 | + { |
| 80 | + public decimal Latitude { get; set; } |
| 81 | + |
| 82 | + public decimal Longitude { get; set; } |
| 83 | + } |
| 84 | + |
| 85 | + /// <remarks> |
| 86 | + /// Internally, the JSON serializer uses Newtonsoft.Json for |
| 87 | + /// serialization and NJsonSchema for schema creation and |
| 88 | + /// validation. |
| 89 | + /// </remarks> |
| 90 | + class Program |
| 91 | + { |
| 92 | + // from: https://json-schema.org/learn/getting-started-step-by-step.html |
| 93 | + private static string S1; |
| 94 | + private static string S2; |
| 95 | + static async Task Main(string[] args) |
| 96 | + { |
| 97 | + if (args.Length != 3) |
| 98 | + { |
| 99 | + Console.WriteLine("Usage: .. bootstrapServers schemaRegistryUrl topicName"); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + S1 = File.ReadAllText("geographical-location.json"); |
| 104 | + S2 = File.ReadAllText("product.json"); |
| 105 | + string bootstrapServers = args[0]; |
| 106 | + string schemaRegistryUrl = args[1]; |
| 107 | + string topicName = args[2]; |
| 108 | + |
| 109 | + var consumerConfig = new ConsumerConfig |
| 110 | + { |
| 111 | + BootstrapServers = bootstrapServers, |
| 112 | + GroupId = "json-example-consumer-group" |
| 113 | + }; |
| 114 | + |
| 115 | + var producerConfig = new ProducerConfig |
| 116 | + { |
| 117 | + BootstrapServers = bootstrapServers |
| 118 | + }; |
| 119 | + |
| 120 | + var schemaRegistryConfig = new SchemaRegistryConfig |
| 121 | + { |
| 122 | + Url = schemaRegistryUrl |
| 123 | + }; |
| 124 | + |
| 125 | + var sr = new CachedSchemaRegistryClient(schemaRegistryConfig); |
| 126 | + |
| 127 | + var subject1 = $"{topicName}-CoordinatesOnMap"; |
| 128 | + var subject2 = $"{topicName}-Product"; |
| 129 | + |
| 130 | + // Test there are no errors (exceptions) registering a schema that references another. |
| 131 | + var id1 = sr.RegisterSchemaAsync(subject1, new Schema(S1, SchemaType.Json)).Result; |
| 132 | + var s1 = sr.GetLatestSchemaAsync(subject1).Result; |
| 133 | + var refs = new List<SchemaReference> { new SchemaReference("geographical-location.json", subject1, s1.Version) }; |
| 134 | + var id2 = sr.RegisterSchemaAsync(subject2, new Schema(S2, refs, SchemaType.Json)).Result; |
| 135 | + |
| 136 | + // In fact, it seems references are not checked server side. |
| 137 | + var latestSchema2 = sr.GetLatestSchemaAsync(subject2).Result; |
| 138 | + var latestSchema2Unreg = latestSchema2.Schema; |
| 139 | + var latestSchema1 = sr.GetLatestSchemaAsync(subject1).Result; |
| 140 | + |
| 141 | + var jsonSerializerConfig = new JsonSerializerConfig |
| 142 | + { |
| 143 | + BufferBytes = 100, |
| 144 | + UseLatestVersion = true, |
| 145 | + AutoRegisterSchemas = false, |
| 146 | + SubjectNameStrategy = SubjectNameStrategy.TopicRecord |
| 147 | + }; |
| 148 | + |
| 149 | + // This is needed only if you want to change attribute naming strategy |
| 150 | + // from default one to camelCase. |
| 151 | + // It's also possible to add JsonProperty attributes to customize |
| 152 | + // serialization mapping and all available NJson attributes. |
| 153 | + var jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings |
| 154 | + { |
| 155 | + SerializerSettings = new JsonSerializerSettings |
| 156 | + { |
| 157 | + ContractResolver = new DefaultContractResolver |
| 158 | + { |
| 159 | + NamingStrategy = new CamelCaseNamingStrategy() |
| 160 | + } |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + CancellationTokenSource cts = new CancellationTokenSource(); |
| 165 | + |
| 166 | + var consumeTask = Task.Run(() => |
| 167 | + { |
| 168 | + using (var consumer = |
| 169 | + new ConsumerBuilder<long, Product>(consumerConfig) |
| 170 | + .SetValueDeserializer(new JsonDeserializer<Product>(sr, latestSchema2Unreg, null, jsonSchemaGeneratorSettings).AsSyncOverAsync()) |
| 171 | + .SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}")) |
| 172 | + .Build()) |
| 173 | + { |
| 174 | + consumer.Subscribe(topicName); |
| 175 | + |
| 176 | + try |
| 177 | + { |
| 178 | + while (true) |
| 179 | + { |
| 180 | + try |
| 181 | + { |
| 182 | + var cr = consumer.Consume(cts.Token); |
| 183 | + var product = cr.Message.Value; |
| 184 | + |
| 185 | + Console.WriteLine("CONSUMER: product name " + product.ProductName + |
| 186 | + $" Product id {product.ProductId} " + |
| 187 | + $"Price: {product.Price} " + |
| 188 | + $"Latitude: {product.WarehouseLocation.Latitude} " + |
| 189 | + $"Longitude: {product.WarehouseLocation.Longitude}"); |
| 190 | + } |
| 191 | + catch (ConsumeException e) |
| 192 | + { |
| 193 | + Console.WriteLine($"Consume error: {e.Error.Reason}"); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + catch (OperationCanceledException) |
| 198 | + { |
| 199 | + consumer.Close(); |
| 200 | + } |
| 201 | + } |
| 202 | + }); |
| 203 | + |
| 204 | + using (var schemaRegistry = new CachedSchemaRegistryClient(schemaRegistryConfig)) |
| 205 | + using (var producer = |
| 206 | + new ProducerBuilder<long, Product>(producerConfig) |
| 207 | + .SetValueSerializer(new JsonSerializer<Product>(schemaRegistry, latestSchema2Unreg, |
| 208 | + jsonSerializerConfig, jsonSchemaGeneratorSettings)) |
| 209 | + .Build()) |
| 210 | + { |
| 211 | + Console.WriteLine($"PRODUCER: {producer.Name} producing on {topicName}. Enter product name, q to exit."); |
| 212 | + |
| 213 | + long i = 1; |
| 214 | + string text; |
| 215 | + while ((text = Console.ReadLine()) != "q") |
| 216 | + { |
| 217 | + var product = new Product |
| 218 | + { |
| 219 | + ProductId = i++, |
| 220 | + ProductName = text, |
| 221 | + Price = 9.99M, |
| 222 | + Tags = new List<string> { "tag1", "tag2" }, |
| 223 | + Dimensions = new Dimensions |
| 224 | + { |
| 225 | + Length = 10.0M, |
| 226 | + Width = 5.0M, |
| 227 | + Height = 2.0M |
| 228 | + }, |
| 229 | + WarehouseLocation = new GeographicalLocation |
| 230 | + { |
| 231 | + Latitude = 37.7749M, |
| 232 | + Longitude = -122.4194M |
| 233 | + } |
| 234 | + }; |
| 235 | + try |
| 236 | + { |
| 237 | + await producer.ProduceAsync(topicName, new Message<long, Product> |
| 238 | + { |
| 239 | + Key = product.ProductId, |
| 240 | + Value = product |
| 241 | + }); |
| 242 | + } |
| 243 | + catch (Exception e) |
| 244 | + { |
| 245 | + Console.WriteLine($"error producing message: {e.Message}"); |
| 246 | + } |
| 247 | + Console.WriteLine($"{producer.Name} producing on {topicName}. Enter product name, q to exit."); |
| 248 | + } |
| 249 | + } |
| 250 | + cts.Cancel(); |
| 251 | + } |
| 252 | + } |
| 253 | +} |
0 commit comments