Skip to content

Added Protobuf serialization #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions src/documentation/articles/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,159 @@ using (context = new BloggingContext()
// execute stuff here
}
```

## **Protobuf** serialization

With package [MASES.EntityFrameworkCore.KNet.Serialization.Protobuf](https://www.nuget.org/packages/MASES.EntityFrameworkCore.KNet.Serialization.Protobuf/) an user can choose the Protobuf serializer.

### Protobuf schema

The following schema is the default used from the engine and can be registered in Apache Schema registry so other applications can use it to extract the data stored in the topics:

- Common multitype value:
```protobuf
// [START declaration]
syntax = "proto3";
package storage;

import "google/protobuf/struct.proto";
import "google/protobuf/timestamp.proto";
// [END declaration]

// [START java_declaration]
option java_multiple_files = true;
option java_package = "mases.entityframeworkcore.knet.serialization.protobuf";
option java_outer_classname = "GenericValue";
// [END java_declaration]

// [START csharp_declaration]
option csharp_namespace = "MASES.EntityFrameworkCore.KNet.Serialization.Protobuf.Storage";
// [END csharp_declaration]

// [START messages]
// Our address book file is just one of these.
message GenericValue {
// The kind of value.
oneof kind {
// Represents a null value.
google.protobuf.NullValue null_value = 1;
// Represents a boolean value.
bool bool_value = 2;
// Represents a int value.
int32 byte_value = 3;
// Represents a int value.
int32 short_value = 4;
// Represents a int value.
int32 int_value = 5;
// Represents a long value.
int64 long_value = 6;
// Represents a float value.
float float_value = 7;
// Represents a double value.
double double_value = 8;
// Represents a string value.
string string_value = 9;
// Represents a Guid value.
bytes guid_value = 10;
// Represents a Timestamp value.
google.protobuf.Timestamp datetime_value = 11;
// Represents a Timestamp value.
google.protobuf.Timestamp datetimeoffset_value = 12;
}
}
// [END messages]
```

- Complex Primary Key schema:
```protobuf
// [START declaration]
syntax = "proto3";
package storage;

import "GenericValue.proto";
// [END declaration]

// [START java_declaration]
option java_multiple_files = true;
option java_package = "mases.entityframeworkcore.knet.serialization.protobuf";
option java_outer_classname = "KeyContainer";
// [END java_declaration]

// [START csharp_declaration]
option csharp_namespace = "MASES.EntityFrameworkCore.KNet.Serialization.Protobuf.Storage";
// [END csharp_declaration]

// [START messages]
message PrimaryKeyType {
repeated GenericValue values = 1;
}

// Our address book file is just one of these.
message KeyContainer {
PrimaryKeyType PrimaryKey = 1;
}
// [END messages]
```


- ValueContainer schema:
```protobuf
// [START declaration]
syntax = "proto3";
package storage;

import "GenericValue.proto";
// [END declaration]

// [START java_declaration]
option java_multiple_files = true;
option java_package = "mases.entityframeworkcore.knet.serialization.protobuf";
option java_outer_classname = "ValueContainer";
// [END java_declaration]

// [START csharp_declaration]
option csharp_namespace = "MASES.EntityFrameworkCore.KNet.Serialization.Protobuf.Storage";
// [END csharp_declaration]

// [START messages]
message PropertyDataRecord {
int32 PropertyIndex = 1;
string PropertyName = 2;
string ClrType = 3;
GenericValue Value = 4;
}

// Our address book file is just one of these.
message ValueContainer {
string EntityName = 1;
string ClrType = 2;
repeated PropertyDataRecord Data = 3;
}
// [END messages]
```

The extension converted this schema into code to speedup the exection of serialization/deserialization operations.

### How to use Protobuf

`KafkaDbContext` contains three properties can be used to override the default types:
- **KeySerializationType**: set this value to `ProtobufKEFCoreSerDes.Key<>`, the type automatically manages simple or complex Primary Key
- **ValueSerializationType**: set this value to `ProtobufKEFCoreSerDes.ValueContainer<>`
- **ValueContainerType**: set this value to `ProtobufValueContainer<>`

An example is:

```C#
using (context = new BloggingContext()
{
BootstrapServers = "KAFKA-SERVER:9092",
ApplicationId = "MyAppid",
DbName = "MyDBName",
KeySerializationType = typeof(ProtobufKEFCoreSerDes.Key<>),
ValueContainerType = typeof(ProtobufValueContainer<>),
ValueSerializationType = typeof(ProtobufKEFCoreSerDes.ValueContainer<>),
})
{
// execute stuff here
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class AvroSerializationHelper
{
public static void BuildDefaultSchema(string outputFolder)
{
AvroSerDes.CompilerSupport.BuildSchemaClassesFromFiles(outputFolder, "AvroValueContainer.avsc");
AvroSerDes.CompilerSupport.BuildSchemaClassesFromFiles(outputFolder, "AvroValueContainer.avsc");
AvroSerDes.CompilerSupport.BuildSchemaClassesFromFiles(outputFolder, "AvroKeyContainer.avsc");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>MASES.EntityFrameworkCore.KNet.Serialization.Avro</RootNamespace>
<Title>EntityFrameworkCore KNet - Avro Serialization support for EntityFrameworkCore provider for Apache Kafka</Title>
<Description>EntityFrameworkCore KNet - Avro Serialization support for EntityFrameworkCore provider for Apache Kafka</Description>
<Product>MASES.EntityFrameworkCore.KNet.Serialization.Avro</Product>
<Product>MASES.EntityFrameworkCore.KNet.Serialization.Avro.Compiler</Product>
<OutputPath>..\..\..\bin\</OutputPath>
<PackageTags>Entity Framework Core;entity-framework-core;ef;efcore;EntityFrameworkCore;orm;O/RM;sql kafka apache-kafka dotnet clr netcore net6 apachekafka connect streams producer consumer</PackageTags>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion src/net/KEFCore.SerDes.Avro/KEFCore.SerDes.Avro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Description>EntityFrameworkCore KNet - Avro Serialization support for EntityFrameworkCore provider for Apache Kafka</Description>
<Product>MASES.EntityFrameworkCore.KNet.Serialization.Avro</Product>
<OutputPath>..\..\..\bin\</OutputPath>
<PackageTags>Entity Framework Core;entity-framework-core;ef;efcore;EntityFrameworkCore;orm;O/RM;sql kafka apache-kafka dotnet clr netcore net6 apachekafka connect streams producer consumer</PackageTags>
<PackageTags>Avro;Entity Framework Core;entity-framework-core;ef;efcore;EntityFrameworkCore;orm;O/RM;sql kafka apache-kafka dotnet clr netcore net6 apachekafka connect streams producer consumer</PackageTags>
<PackageId>MASES.EntityFrameworkCore.KNet.Serialization.Avro</PackageId>
<PackageReadmeFile>serialization.md</PackageReadmeFile>
<Nullable>enable</Nullable>
Expand Down
Loading