github.com/Jeffail/benthos/v3@v3.65.0/website/docs/components/processors/protobuf.md (about)

     1  ---
     2  title: protobuf
     3  type: processor
     4  status: beta
     5  categories: ["Parsing"]
     6  ---
     7  
     8  <!--
     9       THIS FILE IS AUTOGENERATED!
    10  
    11       To make changes please edit the contents of:
    12       lib/processor/protobuf.go
    13  -->
    14  
    15  import Tabs from '@theme/Tabs';
    16  import TabItem from '@theme/TabItem';
    17  
    18  :::caution BETA
    19  This component is mostly stable but breaking changes could still be made outside of major version releases if a fundamental problem with the component is found.
    20  :::
    21  
    22  Performs conversions to or from a protobuf message. This processor uses
    23  reflection, meaning conversions can be made directly from the target .proto
    24  files.
    25  
    26  
    27  <Tabs defaultValue="common" values={[
    28    { label: 'Common', value: 'common', },
    29    { label: 'Advanced', value: 'advanced', },
    30  ]}>
    31  
    32  <TabItem value="common">
    33  
    34  ```yaml
    35  # Common config fields, showing default values
    36  label: ""
    37  protobuf:
    38    operator: to_json
    39    message: ""
    40    import_paths: []
    41  ```
    42  
    43  </TabItem>
    44  <TabItem value="advanced">
    45  
    46  ```yaml
    47  # All config fields, showing default values
    48  label: ""
    49  protobuf:
    50    operator: to_json
    51    message: ""
    52    import_paths: []
    53    parts: []
    54  ```
    55  
    56  </TabItem>
    57  </Tabs>
    58  
    59  The main functionality of this processor is to map to and from JSON documents,
    60  you can read more about JSON mapping of protobuf messages here:
    61  [https://developers.google.com/protocol-buffers/docs/proto3#json](https://developers.google.com/protocol-buffers/docs/proto3#json)
    62  
    63  Using reflection for processing protobuf messages in this way is less performant
    64  than generating and using native code. Therefore when performance is critical it
    65  is recommended that you use Benthos plugins instead for processing protobuf
    66  messages natively, you can find an example of Benthos plugins at
    67  [https://github.com/benthosdev/benthos-plugin-example](https://github.com/benthosdev/benthos-plugin-example)
    68  
    69  ## Operators
    70  
    71  ### `to_json`
    72  
    73  Converts protobuf messages into a generic JSON structure. This makes it easier
    74  to manipulate the contents of the document within Benthos.
    75  
    76  ### `from_json`
    77  
    78  Attempts to create a target protobuf message from a generic JSON structure.
    79  
    80  ## Fields
    81  
    82  ### `operator`
    83  
    84  The [operator](#operators) to execute
    85  
    86  
    87  Type: `string`  
    88  Default: `"to_json"`  
    89  Options: `to_json`, `from_json`.
    90  
    91  ### `message`
    92  
    93  The fully qualified name of the protobuf message to convert to/from.
    94  
    95  
    96  Type: `string`  
    97  Default: `""`  
    98  
    99  ### `import_paths`
   100  
   101  A list of directories containing .proto files, including all definitions required for parsing the target message. If left empty the current directory is used. Each directory listed will be walked with all found .proto files imported.
   102  
   103  
   104  Type: `array`  
   105  Default: `[]`  
   106  
   107  ### `parts`
   108  
   109  An optional array of message indexes of a batch that the processor should apply to.
   110  If left empty all messages are processed. This field is only applicable when
   111  batching messages [at the input level](/docs/configuration/batching).
   112  
   113  Indexes can be negative, and if so the part will be selected from the end
   114  counting backwards starting from -1.
   115  
   116  
   117  Type: `array`  
   118  Default: `[]`  
   119  
   120  ## Examples
   121  
   122  <Tabs defaultValue="JSON to Protobuf" values={[
   123  { label: 'JSON to Protobuf', value: 'JSON to Protobuf', },
   124  { label: 'Protobuf to JSON', value: 'Protobuf to JSON', },
   125  ]}>
   126  
   127  <TabItem value="JSON to Protobuf">
   128  
   129  
   130  If we have the following protobuf definition within a directory called `testing/schema`:
   131  
   132  ```protobuf
   133  syntax = "proto3";
   134  package testing;
   135  
   136  import "google/protobuf/timestamp.proto";
   137  
   138  message Person {
   139    string first_name = 1;
   140    string last_name = 2;
   141    string full_name = 3;
   142    int32 age = 4;
   143    int32 id = 5; // Unique ID number for this person.
   144    string email = 6;
   145  
   146    google.protobuf.Timestamp last_updated = 7;
   147  }
   148  ```
   149  
   150  And a stream of JSON documents of the form:
   151  
   152  ```json
   153  {
   154  	"firstName": "caleb",
   155  	"lastName": "quaye",
   156  	"email": "caleb@myspace.com"
   157  }
   158  ```
   159  
   160  We can convert the documents into protobuf messages with the following config:
   161  
   162  ```yaml
   163  pipeline:
   164    processors:
   165      - protobuf:
   166          operator: from_json
   167          message: testing.Person
   168          import_paths: [ testing/schema ]
   169  ```
   170  
   171  </TabItem>
   172  <TabItem value="Protobuf to JSON">
   173  
   174  
   175  If we have the following protobuf definition within a directory called `testing/schema`:
   176  
   177  ```protobuf
   178  syntax = "proto3";
   179  package testing;
   180  
   181  import "google/protobuf/timestamp.proto";
   182  
   183  message Person {
   184    string first_name = 1;
   185    string last_name = 2;
   186    string full_name = 3;
   187    int32 age = 4;
   188    int32 id = 5; // Unique ID number for this person.
   189    string email = 6;
   190  
   191    google.protobuf.Timestamp last_updated = 7;
   192  }
   193  ```
   194  
   195  And a stream of protobuf messages of the type `Person`, we could convert them into JSON documents of the format:
   196  
   197  ```json
   198  {
   199  	"firstName": "caleb",
   200  	"lastName": "quaye",
   201  	"email": "caleb@myspace.com"
   202  }
   203  ```
   204  
   205  With the following config:
   206  
   207  ```yaml
   208  pipeline:
   209    processors:
   210      - protobuf:
   211          operator: to_json
   212          message: testing.Person
   213          import_paths: [ testing/schema ]
   214  ```
   215  
   216  </TabItem>
   217  </Tabs>
   218  
   219