Skip to main content

Generate Java data classes from Protobuf schemas

Generate Java data classes from Protocol Buffers (.proto) schema files for use in Apache Kafka® producers and consumers. Use the protoc compiler to generate Java classes that match your schema structure.

Prerequisites

Generate the Java classes

Generate Java classes from your Protobuf schema file using the protoc CLI:

protoc -I=. --java_out=src/main/java/ src/main/resources/example.proto
  • Replace example.proto with your Protobuf schema file.
  • Replace src/main/java/ with your preferred output directory.

The generated class is placed inside a directory that matches the package declaration in your .proto file. Ensure the package in your .proto file matches your intended Java package structure.

note

To control the Java package of the generated classes, add the option java_package inside your .proto file. This cannot be set through the protoc compiler command-line options. Kafka serialization depends on the fully qualified class name, so ensure the java_package matches your project structure.

Example schema

syntax = "proto3";

package io.aiven.example.protobuf;
option java_package = "io.aiven.example.protobuf";

message User {
int32 id = 1;
string email = 2;
}

This schema produces the following file:

src/main/java/io/aiven/example/protobuf/User.java

Add Maven dependencies

Add these dependencies to your pom.xml to compile and use the generated classes for Protobuf serialization and deserialization in Kafka producers and consumers.

<dependencies>
<!-- Apache Kafka client dependency -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.8.1</version>
</dependency>

<!-- Confluent dependencies for Protobuf serialization and Schema Registry -->
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-protobuf-serializer</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-client</artifactId>
<version>8.0.0</version>
</dependency>

<!-- Protobuf core library -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>4.32.0</version>
</dependency>
</dependencies>

Optional dependencies

You can also use these optional dependencies depending on your use case:

<!-- Support for Protobuf well-known types (e.g., Timestamp, Struct) -->
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-protobuf-types</artifactId>
<version>8.0.0</version>
</dependency>

<!-- Provider for advanced Protobuf schema features -->
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-protobuf-provider</artifactId>
<version>8.0.0</version>
</dependency>

These dependencies are optional. Use them if your schema includes well-known Protobuf types or if you use advanced features in Confluent’s Protobuf support.

Related pages

Protobuf Compiler Installation