protobuf

Handles conversions between JSON documents and protobuf messages using reflection, which allows you to make conversions from or to the target .proto files.

For more information about JSON mapping of protobuf messages, see ProtoJSON Format and Examples.

# Configuration fields, showing default values
label: ""
protobuf:
  operator: "" # No default (required)
  message: "" # No default (required)
  discard_unknown: false
  use_proto_names: false
  import_paths: []
  use_enum_numbers: false

Performance considerations

Processing protobuf messages using reflection is less performant than using generated native code. For scenarios where performance is critical, consider using Redpanda Connect plugins.

Operators

to_json

Converts protobuf messages into a generic JSON structure, which makes it easier to manipulate the contents of the JSON document within Redpanda Connect.

from_json

Attempts to create a target protobuf message from a generic JSON structure.

Fields

operator

The operator to execute.

Type: string

Options: to_json , from_json

message

The fully-qualified name of the protobuf message to convert from or to JSON.

Type: string

discard_unknown

When set to true, the from_json operator discards fields that are unknown to the schema.

Type: bool

Default: false

use_proto_names

When set to true, the to_json operator deserializes fields exactly as named in schema file.

Type: bool

Default: false

import_paths

A list of directories that contain .proto files, including all definitions required for parsing the target message. If left empty, the current directory is used. This processor imports all .proto files listed within specified or default directories.

Type: array

Default: []

use_enum_numbers

When set to true, the to_json operator deserializes enumeration fields as their numerical values instead of their string names. For example, an enum field with a value of ENUM_VALUE_ONE is represented as 1 in the JSON output.

Type: bool

Default: false

Examples

  • JSON to Protobuf conversion

  • Protobuf to JSON conversion

A protobuf definition is stored in the testing/schema directory:

syntax = "proto3";
package testing;

import "google/protobuf/timestamp.proto";

message Person {
  string first_name = 1;
  string last_name = 2;
  string full_name = 3;
  int32 age = 4;
  int32 id = 5; // Unique ID number for this person.
  string email = 6;

  google.protobuf.Timestamp last_updated = 7;
}

And you have a stream of JSON documents with the format:

{
	"firstName": "caleb",
	"lastName": "quaye",
	"email": "caleb@myspace.com"
}

The following configuration converts the JSON documents into protobuf messages:

pipeline:
  processors:
    - protobuf:
        operator: from_json
        message: testing.Person
        import_paths: [ testing/schema ]

A protobuf definition is stored in the testing/schema directory:

syntax = "proto3";
package testing;

import "google/protobuf/timestamp.proto";

message Person {
  string first_name = 1;
  string last_name = 2;
  string full_name = 3;
  int32 age = 4;
  int32 id = 5; // Unique ID number for this person.
  string email = 6;

  google.protobuf.Timestamp last_updated = 7;
}

And you have a stream of protobuf messages of the type Person:

{
	"firstName": "caleb",
	"lastName": "quaye",
	"email": "caleb@myspace.com"
}

The following configuration converts the messages into JSON documents:

pipeline:
  processors:
    - protobuf:
        operator: to_json
        message: testing.Person
        import_paths: [ testing/schema ]