Integrate Schema Registry with your Kafka producer or consumer so schemas register
automatically on produce and consumers deserialize topic data with the correct schema
version.
When you connect a Kafka producer or consumer to your Kafka brokers, you can use Schema
Registry to store and evolve versioned schemas for the data on your Kafka topics. You
configure Cloudera-provided Schema Registry serializer and deserializer (SerDes) classes as
your Kafka client's value serializer and deserializer. Each SerDes class uses an embedded
Schema Registry client to talk to Schema Registry: the serializer registers a schema on
first produce, and the deserializer resolves the correct schema version on consume, so
producers and consumers stay aligned as that data changes.
To configure your application, you set up two connections: OAuth and TLS to your Kafka
brokers for producing and consuming messages, and a separate connection to Schema Registry
for schema registration and lookup. You download Kafka broker settings and CA certificates
from the Kafka client configuration bundle that Cloudera Surveyor
provides on the Client Configurations tab. OAuth client credentials and
the Schema Registry URL come from Anywhere Cloud Console; you use the same
credentials for both connections.
You have configured Apache Ranger policies for Kafka and Schema Registry. See Configuring Ranger authorization for policy
setup details.
Perform the credential provisioning steps in this task while logged in to Anywhere Cloud Console as a user who already has the Ranger policies
required for the Kafka and Schema Registry operations your client will perform. The
credentials you create inherit that user's access.
You have a working Kafka producer or consumer application that reads or writes
structured data in either Avro or JSON format:
For Avro: your application already produces or consumes Avro records with defined
schemas. The schema can be defined in an .avsc file, written in
code using SchemaBuilder, or embedded in a generated class. The
serializer reads the schema from the Avro record at runtime. This task does not cover
how to define Avro schemas.
For JSON: your application data is structured as Java classes. You do not need
separate schema files. The JSON serializer infers the schema from your Java objects
and registers it automatically.
Your application uses version 4.1 or later of the kafka-clients
library.
You must have access to a Bash-compatible shell environment with curl
and keytool.
Your project is configured to resolve artifacts from the Cloudera Maven repository. If
you have not already added it, add the following repository configuration to your
project:
Add the schema-registry-serdes library dependency to your
application.
The schema-registry-serdes library contains everything you need to
integrate your Kafka client with Schema Registry, including the SerDes classes and
SchemaRegistryClient.
Gather the values required for credential provisioning.
Log in to Anywhere Cloud Console.
Look up and record your domain from your Anywhere Cloud Console console
URL.
Your domain is the portion of the URL that comes after console.. For
example, if your console URL is https://console.my-awc.cloudera.com, your
domain is my-awc.cloudera.com.
Obtain and record the hadoop-jwt session cookie from your browser's
developer tools.
To do this, open browser developer tools and navigate to the
Cookies section. Find the hadoop-jwt cookie and
record its value.
To open browser developer tools:
Chrome: Press F12 and go to Application > Cookies.
Firefox: Press F12 and go to Storage > Cookies.
Safari: Press Cmd+Option+I and go to Storage > Cookies.
Open your local terminal and set your domain and hadoop-jwt session
cookie as environment variables.
The following command prompts you to enter each value. The cookie value is not displayed
as you type.
Client credentials are used by applications to authenticate directly with the OAuth server.
You can request client credentials through a provisioning endpoint using your session
cookie.
Call the credential provisioning endpoint and save the response to a file.
The client_id and client_secret fields are your client
credentials. The principal field is the user principal those credentials
are bound to. It matches the user you were logged in as in Anywhere Cloud Console when you obtained the hadoop-jwt session
cookie used to call the provisioning endpoint. When your client connects, Apache Ranger
authorizes it as this principal. The example shows admin; in your
environment, the value is your Anywhere Cloud Console login username. The
issued_at field is informational only.
Optional: Save the client credentials from credentials.json to a password
manager or another secure location for future use.
Client credentials are long-lived and can be reused for other clients or programmatic
API calls.
Access Cloudera Surveyor.
In Anywhere Cloud Console, go to Streams
Messaging
Find and click the entry for your Cloudera Surveyor
instance.
Download the client configuration bundle from Cloudera Surveyor.
In Cloudera Surveyor, go to Clusters and
select your Kafka cluster.
Go to the Client Configurations tab.
Click Download All.
Extract the archive to a working directory and keep all files in the same
directory.
Some paths in the configuration files are relative, so all files must remain in the same
directory.
Get the Schema Registry URL from Anywhere Cloud Console.
In Anywhere Cloud Console, go to Admin Center > Apps > Deployed Engines.
Find and click the entry for your Schema Registry instance.
In the Endpoints table, get the URL from the
landingPage row.
You can copy the URL with the option. This is
the URL that you will use as the value of the schema.registry.url
property in your client configuration. The URL includes the /api/v1
suffix.
Create a truststore for the Schema Registry client.
The Cloudera Surveyor bundle supplies PEM CA certificate files
for the Kafka client in external.properties. However, the embedded
Schema Registry client requires a PKCS12 or JKS truststore instead. Import
ca-bundle.crt, kafka-ca.crt, and
oauth-ca.crt from the bundle into one JKS file. Run the following
commands from the directory where you extracted the bundle:
sasl.oauthbearer.token.endpoint.url — the OAuth token endpoint
where the Kafka client requests access tokens. The Schema Registry client uses the
same endpoint in the next step (schema.registry.oauth.server.url).
The broker client and the Schema Registry client are separate clients, so each
property set supplies the URL for its own authentication flow.
sasl.oauthbearer.client.credentials.client.id and
sasl.oauthbearer.client.credentials.client.secret — OAuth client
credentials for the Kafka client. Set the same values on
schema.registry.oauth.client.id and
schema.registry.oauth.secret in the next step so both clients
authenticate as the same identity.
value.serializer and value.deserializer — set the
serializer on a producer and the deserializer on a consumer. If your application
only produces, set only the serializer. If it only consumes, set only the
deserializer. The example above uses the Avro SerDes classes. For JSON, use:
schema.registry.url — the base URL of your Schema Registry
instance from Admin Center > Apps > Deployed Engines.
schema.registry.oauth.server.url — the OAuth token endpoint for
the Schema Registry client. Use the same URL as
sasl.oauthbearer.token.endpoint.url. The Schema Registry client
requests its own access token independently of the Kafka client.
schema.registry.oauth.client.id and
schema.registry.oauth.secret — OAuth client credentials for the
Schema Registry client. Use the same values as for the Kafka client in the previous
step.
schema.registry.client.ssl.* — TLS trust settings for HTTPS calls
to the Schema Registry REST API at schema.registry.url.
trustStoreType, trustStorePath, and
trustStorePassword (unprefixed) — TLS trust settings for the
separate HTTPS call to the OAuth token endpoint at
schema.registry.oauth.server.url. These apply to the OAuth HTTP
client inside the Schema Registry client, not to the Kafka broker connection. You
can use the same JKS file as for schema.registry.client.ssl.*.
Configure the OAuth token endpoint allowlist.
The Kafka client must be explicitly permitted to contact the OAuth token endpoint before it
can request tokens. This is a JVM system property and cannot be set in a properties file. Set
it to the value of sasl.oauthbearer.token.endpoint.url from
external.properties. Choose the option that matches your client type and
complete only that option.
Kafka console tools: Set the property using the
KAFKA_OPTS environment variable before running the console tool. This
passes the property as a JVM argument to the console tool at
startup.
When a producer sends the first message for a subject, the serializer registers the schema
in Schema Registry automatically. When a consumer reads a message, the deserializer fetches
the registered schema and deserializes the record.
To confirm that a schema was registered, view schemas in Cloudera Surveyor or use the Schema Registry REST API.
The SerDes classes and the embedded Schema Registry client support additional
configuration properties beyond those in this task. Review those properties when you
need to fine-tune serialization, deserialization, or Schema Registry client
behavior.