|
| 1 | +// Copyright 2024 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 Avro; |
| 18 | +using Avro.Generic; |
| 19 | +using Confluent.Kafka.SyncOverAsync; |
| 20 | +using Confluent.SchemaRegistry.Serdes; |
| 21 | +using Confluent.SchemaRegistry; |
| 22 | +using Schema = Confluent.SchemaRegistry.Schema; |
| 23 | +using System; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.Threading; |
| 26 | +using System.Threading.Tasks; |
| 27 | +using Confluent.SchemaRegistry.Rules; |
| 28 | + |
| 29 | + |
| 30 | +namespace Confluent.Kafka.Examples.AvroGenericMigration |
| 31 | +{ |
| 32 | + class Program |
| 33 | + { |
| 34 | + static async Task Main(string[] args) |
| 35 | + { |
| 36 | + if (args.Length != 3) |
| 37 | + { |
| 38 | + Console.WriteLine("Usage: .. bootstrapServers schemaRegistryUrl topicName"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Register the KMS drivers and the field encryption executor |
| 43 | + JsonataExecutor.Register(); |
| 44 | + |
| 45 | + string bootstrapServers = args[0]; |
| 46 | + string schemaRegistryUrl = args[1]; |
| 47 | + string topicName = args[2]; |
| 48 | + string subjectName = topicName + "-value"; |
| 49 | + string groupName = "avro-generic-example-group"; |
| 50 | + |
| 51 | + var avroSerializerConfig = new AvroSerializerConfig |
| 52 | + { |
| 53 | + AutoRegisterSchemas = false, |
| 54 | + UseLatestWithMetadata = new Dictionary<string, string>() |
| 55 | + { |
| 56 | + ["application.major.version"] = "1" |
| 57 | + }, |
| 58 | + // optional Avro serializer properties: |
| 59 | + BufferBytes = 100 |
| 60 | + }; |
| 61 | + |
| 62 | + var avroDeserializerConfig = new AvroDeserializerConfig |
| 63 | + { |
| 64 | + UseLatestWithMetadata = new Dictionary<string, string>() |
| 65 | + { |
| 66 | + ["application.major.version"] = "2" |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + var s = (RecordSchema)RecordSchema.Parse( |
| 71 | + @"{ |
| 72 | + ""type"": ""record"", |
| 73 | + ""name"": ""User"", |
| 74 | + ""fields"": [ |
| 75 | + {""name"": ""name"", ""type"": ""string"", ""confluent:tags"": [""PII""]}, |
| 76 | + {""name"": ""favorite_number"", ""type"": ""long""}, |
| 77 | + {""name"": ""favorite_color"", ""type"": ""string""} |
| 78 | + ] |
| 79 | + }" |
| 80 | + ); |
| 81 | + |
| 82 | + Confluent.SchemaRegistry.Metadata metadata = new Confluent.SchemaRegistry.Metadata( |
| 83 | + null, |
| 84 | + new Dictionary<string, string> |
| 85 | + { |
| 86 | + ["application.major.version"] = "1", |
| 87 | + }, |
| 88 | + null |
| 89 | + ); |
| 90 | + Schema schema = new Schema(s.ToString(), null, SchemaType.Avro, metadata, null); |
| 91 | + |
| 92 | + var s2 = (RecordSchema)RecordSchema.Parse( |
| 93 | + @"{ |
| 94 | + ""type"": ""record"", |
| 95 | + ""name"": ""User"", |
| 96 | + ""fields"": [ |
| 97 | + {""name"": ""name"", ""type"": ""string"", ""confluent:tags"": [""PII""]}, |
| 98 | + {""name"": ""fave_num"", ""type"": ""long""}, |
| 99 | + {""name"": ""favorite_color"", ""type"": ""string""} |
| 100 | + ] |
| 101 | + }" |
| 102 | + ); |
| 103 | + |
| 104 | + Confluent.SchemaRegistry.Metadata metadata2 = new Confluent.SchemaRegistry.Metadata( |
| 105 | + null, |
| 106 | + new Dictionary<string, string> |
| 107 | + { |
| 108 | + ["application.major.version"] = "2", |
| 109 | + }, |
| 110 | + null |
| 111 | + ); |
| 112 | + String expr = "$merge([$sift($, function($v, $k) {$k != 'favorite_number'}), {'fave_num': $.'favorite_number'}])"; |
| 113 | + RuleSet ruleSet = new RuleSet(new List<Rule> |
| 114 | + { |
| 115 | + new Rule("upgrade", RuleKind.Transform, RuleMode.Upgrade, "JSONATA", null, null, |
| 116 | + expr, null, null, false) |
| 117 | + }, new List<Rule>() |
| 118 | + ); |
| 119 | + Schema schema2 = new Schema(s2.ToString(), null, SchemaType.Avro, metadata2, ruleSet); |
| 120 | + |
| 121 | + CancellationTokenSource cts = new CancellationTokenSource(); |
| 122 | + var consumeTask = Task.Run(() => |
| 123 | + { |
| 124 | + using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = schemaRegistryUrl })) |
| 125 | + using (var consumer = |
| 126 | + new ConsumerBuilder<string, GenericRecord>(new ConsumerConfig { BootstrapServers = bootstrapServers, GroupId = groupName }) |
| 127 | + .SetValueDeserializer(new AvroDeserializer<GenericRecord>(schemaRegistry, avroDeserializerConfig).AsSyncOverAsync()) |
| 128 | + .SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}")) |
| 129 | + .Build()) |
| 130 | + { |
| 131 | + consumer.Subscribe(topicName); |
| 132 | + |
| 133 | + try |
| 134 | + { |
| 135 | + while (true) |
| 136 | + { |
| 137 | + try |
| 138 | + { |
| 139 | + var consumeResult = consumer.Consume(cts.Token); |
| 140 | + |
| 141 | + Console.WriteLine($"Key: {consumeResult.Message.Key}\nValue: {consumeResult.Message.Value}"); |
| 142 | + } |
| 143 | + catch (ConsumeException e) |
| 144 | + { |
| 145 | + Console.WriteLine($"Consume error: {e.Error.Reason}"); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + catch (OperationCanceledException) |
| 150 | + { |
| 151 | + // commit final offsets and leave the group. |
| 152 | + consumer.Close(); |
| 153 | + } |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = schemaRegistryUrl })) |
| 158 | + using (var producer = |
| 159 | + new ProducerBuilder<string, GenericRecord>(new ProducerConfig { BootstrapServers = bootstrapServers }) |
| 160 | + .SetValueSerializer(new AvroSerializer<GenericRecord>(schemaRegistry, avroSerializerConfig)) |
| 161 | + .Build()) |
| 162 | + { |
| 163 | + var c = schemaRegistry.UpdateCompatibilityAsync(Compatibility.None, null).Result; |
| 164 | + var id = schemaRegistry.RegisterSchemaAsync(subjectName, schema, true).Result; |
| 165 | + var id2 = schemaRegistry.RegisterSchemaAsync(subjectName, schema2, true).Result; |
| 166 | + |
| 167 | + Console.WriteLine($"{producer.Name} producing on {topicName}. Enter user names, q to exit."); |
| 168 | + |
| 169 | + long i = 1; |
| 170 | + string text; |
| 171 | + while ((text = Console.ReadLine()) != "q") |
| 172 | + { |
| 173 | + var record = new GenericRecord(s); |
| 174 | + record.Add("name", text); |
| 175 | + record.Add("favorite_number", i++); |
| 176 | + record.Add("favorite_color", "blue"); |
| 177 | + |
| 178 | + try |
| 179 | + { |
| 180 | + var dr = await producer.ProduceAsync(topicName, new Message<string, GenericRecord> { Key = text, Value = record }); |
| 181 | + Console.WriteLine($"produced to: {dr.TopicPartitionOffset}"); |
| 182 | + } |
| 183 | + catch (ProduceException<string, GenericRecord> ex) |
| 184 | + { |
| 185 | + // In some cases (notably Schema Registry connectivity issues), the InnerException |
| 186 | + // of the ProduceException contains additional informatiom pertaining to the root |
| 187 | + // cause of the problem. This information is automatically included in the output |
| 188 | + // of the ToString() method of the ProduceException, called implicitly in the below. |
| 189 | + Console.WriteLine($"error producing message: {ex}"); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + cts.Cancel(); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments