1- using System . Diagnostics ;
21using Amazon . Lambda . Core ;
32using Amazon . Lambda . RuntimeSupport ;
43using AWS . Lambda . Powertools . Kafka ;
54using AWS . Lambda . Powertools . Kafka . Protobuf ;
65using AWS . Lambda . Powertools . Logging ;
7- using AWS . Lambda . Powertools . Metrics ;
8- using Com . Example ;
96using TestKafka ;
107
11- // string Handler(ConsumerRecords<ProtobufKey, ProtobufProduct> records, ILambdaContext context)
12- // {
13- // Metrics.SetNamespace("Proto");
14- // Metrics.AddMetric("NumberOfRequests", 1, MetricUnit.Count, MetricResolution.High);
15- //
16- // foreach (var record in records)
17- // {
18- // foreach (var header in record.Headers)
19- // {
20- // Console.WriteLine($"{header.Key}: {ToDecimalString(header.Value)}");
21- // }
22- //
23- // foreach (var header in record.Headers.DecodedValues())
24- // {
25- // Console.WriteLine($"{header.Key}: {header.Value}");
26- // }
27- //
28- // Logger.LogInformation("Record Key: {@key}", record.Key);
29- // Logger.LogInformation("Record Value: {@record}", record.Value);
30- // }
31- //
32- // return "Processed " + records.Count() + " records";
33- // }
34- //
35- // await LambdaBootstrapBuilder.Create((Func<ConsumerRecords<ProtobufKey, ProtobufProduct>, ILambdaContext, string>?)Handler,
36- // new PowertoolsKafkaProtobufSerializer()) // Use PowertoolsKafkaAvroSerializer for Avro serialization
37- // .Build()
38- // .RunAsync();
39-
40- var responseStream = new MemoryStream ( ) ;
41- var serializer = new PowertoolsKafkaProtobufSerializer ( ) ;
42-
43- Task < InvocationResponse > ToUpperAsync ( InvocationRequest invocation )
8+ string Handler ( ConsumerRecords < ProtobufKey , ProtobufProduct > records , ILambdaContext context )
449{
45- var stopwatch = Stopwatch . StartNew ( ) ;
46-
47- var records = serializer . Deserialize < ConsumerRecords < string , CustomerProfile > > ( invocation . InputStream ) ;
48-
4910 foreach ( var record in records )
5011 {
51- Console . WriteLine ( "Record Key: {0}" , record . Key ) ;
52- foreach ( var header in record . Headers )
53- {
54- Console . WriteLine ( $ "{ header . Key } : { ToDecimalString ( header . Value ) } ") ;
55- }
56-
5712 foreach ( var header in record . Headers . DecodedValues ( ) )
5813 {
5914 Console . WriteLine ( $ "{ header . Key } : { header . Value } ") ;
6015 }
6116
62- Console . WriteLine ( "Record UserId: {0}" , record . Value ) ;
17+ Logger . LogInformation ( "Record Key: {@key}" , record . Key ) ;
18+ Logger . LogInformation ( "Record Value: {@record}" , record . Value ) ;
6319 }
6420
65- stopwatch . Stop ( ) ;
66-
67- Metrics . PushSingleMetric ( "ProtoDeserialization-512" ,
68- stopwatch . ElapsedMilliseconds , MetricUnit . Milliseconds , "kafka-dotnet" , "service" , null ,
69- MetricResolution . High ) ;
70-
71- Console . WriteLine ( "Record Count: {0}" , records . Count ( ) ) ;
72- Console . WriteLine ( "JsonDeserialization: {0:F2}" , stopwatch . ElapsedMilliseconds ) ;
73-
74- responseStream . SetLength ( 0 ) ;
75- responseStream . Position = 0 ;
76-
77- return Task . FromResult ( new InvocationResponse ( responseStream , false ) ) ;
21+ return "Processed " + records . Count ( ) + " records" ;
7822}
7923
80- static string ToDecimalString ( byte [ ] bytes )
81- {
82- if ( bytes == null || bytes . Length == 0 )
83- {
84- return "[]" ;
85- }
86-
87- return "[" + string . Join ( ", " , bytes ) + "]" ;
88- }
24+ await LambdaBootstrapBuilder . Create ( ( Func < ConsumerRecords < ProtobufKey , ProtobufProduct > , ILambdaContext , string > ? ) Handler ,
25+ new PowertoolsKafkaProtobufSerializer ( ) ) // Use PowertoolsKafkaAvroSerializer for Avro serialization
26+ . Build ( )
27+ . RunAsync ( ) ;
8928
90- var bootstrap = new LambdaBootstrap ( ToUpperAsync ) ;
91- await bootstrap . RunAsync ( ) ;
0 commit comments