Protobuf Schema
Since Camel 4.24
The Protobuf data format reads and writes the classes that protoc generates from a .proto file. With its schema option, it takes the .proto text instead, and reads and writes a com.google.protobuf.DynamicMessage, so no generated class and no protoc is needed. This suits low-code routes, such as YAML routes and Kamelets.
protobuf-java cannot parse .proto text, so the parser is in this module, camel-protobuf-schema, which adds the parser of Square Wire and its dependencies (okio, Guava and the Kotlin standard library) to the classpath. Add it next to camel-protobuf when you use the schema option:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-protobuf-schema</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> In a Kamelet, declare camel:protobuf-schema. Without it, a protobuf data format with schema fails at startup with a message that says to add camel-protobuf-schema.
Example
A YAML route that turns JSON text into a Protobuf message of the type Order, and writes it to Kafka through a schema registry:
- route:
from:
uri: direct:orders
steps:
- unmarshal:
protobuf:
contentTypeFormat: json
schema: classpath:order.proto
messageName: Order
- to:
uri: kafka:orders
parameters:
schemaRegistryVendor: confluent
schemaRegistryURL: http://localhost:8081
schemaFormat: protobuf schema is the .proto text, or a resource such as classpath:order.proto, file:order.proto or an http: URL. messageName is the message type, such as Order, shop.Order or the nested Order.Line; by default the first message type of the schema.
Supported schemas
proto2 and proto3 files with nested messages, enums, maps, oneof and repeated fields. A schema may import these well-known types of google/protobuf, which are read in their JSON form (a Timestamp as RFC 3339 text): any.proto, descriptor.proto, duration.proto, empty.proto, struct.proto, timestamp.proto and wrappers.proto. Other imports are not supported, including api.proto, source_context.proto and type.proto.