|
1 |
| -# Powertools for AWS Lambda (.NET) - Kafka |
| 1 | +# Powertools for AWS Lambda (.NET) - Kafka Avro |
| 2 | + |
| 3 | +A specialized Lambda serializer for handling Kafka events with Avro-formatted data in .NET Lambda functions. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Automatic Avro Deserialization**: Seamlessly converts Avro binary data from Kafka records into strongly-typed .NET objects |
| 8 | +- **Base64 Decoding**: Handles base64-encoded Avro data from Kafka events automatically |
| 9 | +- **Type Safety**: Leverages compile-time type checking with Avro-generated classes |
| 10 | +- **Flexible Configuration**: Supports custom JSON serialization options and AOT-compatible contexts |
| 11 | +- **Error Handling**: Provides clear error messages for serialization failures |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +dotnet add package AWS.Lambda.Powertools.Kafka.Avro |
| 17 | +``` |
| 18 | + |
| 19 | +## Quick Start |
| 20 | + |
| 21 | +### 1. Configure the Serializer |
| 22 | + |
| 23 | +Add the serializer to your Lambda function assembly: |
| 24 | + |
| 25 | +```csharp |
| 26 | +[assembly: LambdaSerializer(typeof(PowertoolsKafkaAvroSerializer))] |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Define Your Avro Model |
| 30 | + |
| 31 | +Ensure your Avro-generated classes have the required `_SCHEMA` field: |
| 32 | + |
| 33 | +```csharp |
| 34 | +public partial class Customer : ISpecificRecord |
| 35 | +{ |
| 36 | + public static Schema _SCHEMA = Schema.Parse(@"{ |
| 37 | + ""type"": ""record"", |
| 38 | + ""name"": ""Customer"", |
| 39 | + ""fields"": [ |
| 40 | + {""name"": ""id"", ""type"": ""string""}, |
| 41 | + {""name"": ""name"", ""type"": ""string""}, |
| 42 | + {""name"": ""age"", ""type"": ""int""} |
| 43 | + ] |
| 44 | + }"); |
| 45 | + |
| 46 | + public string Id { get; set; } |
| 47 | + public string Name { get; set; } |
| 48 | + public int Age { get; set; } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### 3. Create Your Lambda Handler |
| 53 | + |
| 54 | +```csharp |
| 55 | +public class Function |
| 56 | +{ |
| 57 | + public void Handler(ConsumerRecords<string, Customer> records, ILambdaContext context) |
| 58 | + { |
| 59 | + foreach (var record in records) |
| 60 | + { |
| 61 | + Customer customer = record.Value; // Automatically deserialized from Avro |
| 62 | + context.Logger.LogInformation($"Processing customer: {customer.Name}, Age: {customer.Age}"); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Advanced Configuration |
| 69 | + |
| 70 | +### Custom JSON Options |
| 71 | + |
| 72 | +```csharp |
| 73 | +[assembly: LambdaSerializer(typeof(PowertoolsKafkaAvroSerializer))] |
| 74 | + |
| 75 | +// In your startup or configuration |
| 76 | +var jsonOptions = new JsonSerializerOptions |
| 77 | +{ |
| 78 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 79 | + WriteIndented = true |
| 80 | +}; |
| 81 | + |
| 82 | +var serializer = new PowertoolsKafkaAvroSerializer(jsonOptions); |
| 83 | +``` |
| 84 | + |
| 85 | +### AOT-Compatible Serialization |
| 86 | + |
| 87 | +```csharp |
| 88 | +[JsonSerializable(typeof(ConsumerRecords<string, Customer>))] |
| 89 | +public partial class MyJsonContext : JsonSerializerContext { } |
| 90 | + |
| 91 | +[assembly: LambdaSerializer(typeof(PowertoolsKafkaAvroSerializer))] |
| 92 | + |
| 93 | +// Configure with AOT context |
| 94 | +var serializer = new PowertoolsKafkaAvroSerializer(MyJsonContext.Default); |
| 95 | +``` |
| 96 | + |
| 97 | +## Requirements |
| 98 | + |
| 99 | +- **.NET 6.0+**: This library targets .NET 6.0 and later versions |
| 100 | +- **Avro.NET**: Requires the Apache Avro library for .NET |
| 101 | +- **Avro Schema**: Your data classes must include a public static `_SCHEMA` field |
| 102 | +- **AWS Lambda**: Designed specifically for AWS Lambda runtime environments |
| 103 | + |
| 104 | +## Error Handling |
| 105 | + |
| 106 | +The serializer provides detailed error messages for common issues: |
| 107 | + |
| 108 | +```csharp |
| 109 | +// Missing _SCHEMA field |
| 110 | +InvalidOperationException: "Unsupported type for Avro deserialization: MyClass. |
| 111 | +Avro deserialization requires a type with a static _SCHEMA field." |
| 112 | +
|
| 113 | +// Deserialization failures |
| 114 | +SerializationException: "Failed to deserialize value data: [specific error details]" |
| 115 | +``` |
| 116 | + |
| 117 | +## Compatibility Notes |
| 118 | + |
| 119 | +- **Reflection Requirements**: Uses reflection to access Avro schemas, which may impact AOT compilation |
| 120 | +- **Trimming**: May require additional configuration for self-contained deployments with trimming enabled |
| 121 | +- **Performance**: Optimized for typical Lambda cold start and execution patterns |
| 122 | + |
| 123 | +## Related Packages |
| 124 | + |
| 125 | +- [AWS.Lambda.Powertools.Logging](https://www.nuget.org/packages/AWS.Lambda.Powertools.Logging/) - Structured logging |
| 126 | +- [AWS.Lambda.Powertools.Tracing](https://www.nuget.org/packages/AWS.Lambda.Powertools.Tracing/) - Distributed tracing |
| 127 | + |
| 128 | +## Documentation |
| 129 | + |
| 130 | +For more detailed documentation and examples, visit the [official documentation](https://docs.powertools.aws.dev/lambda/dotnet/). |
| 131 | + |
| 132 | +## License |
| 133 | + |
| 134 | +This library is licensed under the Apache License 2.0. |
0 commit comments