Integrating Schema Registry with Kafka clients

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 deployed and have access to an instance of Cloudera Streams Messaging, if not see Deploying Cloudera Streams Messaging.

  • 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:

    <repositories>
      <repository>
        <id>cloudera</id>
        <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
      </repository>
    </repositories>
    repositories {
      maven {
        url "https://repository.cloudera.com/artifactory/cloudera-repos/"
      }
    }
  1. 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.

    <dependency>
      <groupId>com.hortonworks.registries</groupId>
      <artifactId>schema-registry-serdes</artifactId>
      <version>0.10.0.7.3.2.20000-258</version>
    </dependency>
    implementation 'com.hortonworks.registries:schema-registry-serdes:0.10.0.7.3.2.20000-258'
  2. Gather the values required for credential provisioning.
    1. Log in to Anywhere Cloud Console.
    2. 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.
    3. 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.
    4. 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.

      printf "Domain: " && read -r DOMAIN; \
        printf "Hadoop JWT cookie: " && read -rs HADOOP_JWT && echo; \
        export DOMAIN HADOOP_JWT
  3. Obtain client credentials.

    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.

    1. Call the credential provisioning endpoint and save the response to a file.
      curl -sk \
        -H "Cookie: hadoop-jwt=${HADOOP_JWT}" \
        -H "Content-Type: application/json" \
        -X POST "https://console.${DOMAIN}/api/v1/auth/access-keys/credentials" \
        -d '{"description": "my automation keys"}' > credentials.json
    2. Record the client_id and client_secret values from credentials.json.

      You need these values in a later step. The response in credentials.json resembles the following example:

      {
        "client_id": "123e4567-e89b-12d3-a456-426614174000",
        "client_secret": "dGhpcy1pcy1hLXNhbXBsZS1zZWNyZXQtZm9yLWRvY3M=",
        "principal": "admin",
        "issued_at": "2026-04-13T22:24:40.330429188Z"
      }

      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.

    3. 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.
  4. Access Cloudera Surveyor.
    1. In Anywhere Cloud Console, go to Streams Messaging
    2. Find and click the entry for your Cloudera Surveyor instance.
  5. Download the client configuration bundle from Cloudera Surveyor.
    1. In Cloudera Surveyor, go to Clusters and select your Kafka cluster.
    2. Go to the Client Configurations tab.
    3. Click Download All.
    4. 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.
  6. Get the Schema Registry URL from Anywhere Cloud Console.
    1. In Anywhere Cloud Console, go to Admin Center > Apps > Deployed Engines.
    2. Find and click the entry for your Schema Registry instance.
    3. 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.

  7. 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:

    keytool -importcert -alias kafka-ca -file kafka-ca.crt \
      -keystore sr-truststore.jks -storepass [***STORE PASSWORD***] -noprompt
    keytool -importcert -alias oauth-ca -file oauth-ca.crt \
      -keystore sr-truststore.jks -storepass [***STORE PASSWORD***] -noprompt
    keytool -importcert -alias ca-bundle -file ca-bundle.crt \
      -keystore sr-truststore.jks -storepass [***STORE PASSWORD***] -noprompt
  8. Configure Kafka broker connection properties and set the serializer or deserializer classes.
    1. Copy the contents of external.properties from the Cloudera Surveyor bundle into your application's client configuration.
    2. Replace the <CLIENT_ID> and <CLIENT_SECRET> placeholders with the client_id and client_secret you recorded earlier.
    3. Set value.serializer and value.deserializer to the Schema Registry SerDes classes for your data format.

    Your configuration file should resemble the following example, with your specific values for the highlighted properties:

    # Kafka broker connection
    bootstrap.servers=my-csm-kafka.test-cluster.my-awc.cloudera.com:8443
    security.protocol=SASL_SSL
    sasl.mechanism=OAUTHBEARER
    sasl.login.callback.handler.class=org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginCallbackHandler
    sasl.oauthbearer.token.endpoint.url=https://console.my-awc.cloudera.com/api/v1/auth/access-keys/token
    sasl.jaas.config=org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required ssl.truststore.location=oauth-ca.crt ssl.truststore.type=PEM;
    sasl.oauthbearer.client.credentials.client.id=123e4567-e89b-12d3-a456-426614174000
    sasl.oauthbearer.client.credentials.client.secret=dGhpcy1pcy1hLXNhbXBsZS1zZWNyZXQtZm9yLWRvY3M=
    ssl.truststore.location=kafka-ca.crt
    ssl.truststore.type=PEM
    
    # Serializer and deserializer
    value.serializer=com.hortonworks.registries.schemaregistry.serdes.avro.kafka.KafkaAvroSerializer
    value.deserializer=com.hortonworks.registries.schemaregistry.serdes.avro.kafka.KafkaAvroDeserializer
    • 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:

      value.serializer=com.hortonworks.registries.schemaregistry.serdes.json.kafka.KafkaJsonSerializer
      value.deserializer=com.hortonworks.registries.schemaregistry.serdes.json.kafka.KafkaJsonDeserializer
  9. Add the Schema Registry properties to your client configuration.

    The example below lists all required Schema Registry properties with example values. Replace the highlighted properties with your specific values:

    # Schema Registry connection
    schema.registry.url=https://schema-registry.my-csm.my-awc.cloudera.com/api/v1
    schema.registry.auth.type=oauth2
    schema.registry.oauth.server.url=https://console.my-awc.cloudera.com/api/v1/auth/access-keys/token
    schema.registry.oauth.client.id=123e4567-e89b-12d3-a456-426614174000
    schema.registry.oauth.secret=dGhpcy1pcy1hLXNhbXBsZS1zZWNyZXQtZm9yLWRvY3M=
    
    # TLS — Schema Registry HTTPS
    schema.registry.client.ssl.protocol=SSL
    schema.registry.client.ssl.trustStoreType=JKS
    schema.registry.client.ssl.trustStorePath=/path/to/sr-truststore.jks
    schema.registry.client.ssl.trustStorePassword=[***STORE PASSWORD***]
    
    # TLS — OAuth2 token endpoint
    trustStoreType=JKS
    trustStorePath=/path/to/sr-truststore.jks
    trustStorePassword=[***STORE PASSWORD***]
    • 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.*.

  10. 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.
      export KAFKA_OPTS="-Dorg.apache.kafka.sasl.oauthbearer.allowed.urls=[***OAUTH TOKEN ENDPOINT***]"
    • Custom Java Kafka client applications: Set the property as a JVM flag at launch time or in code before instantiating your Kafka client.
      • As a JVM flag:
        java -Dorg.apache.kafka.sasl.oauthbearer.allowed.urls=[***OAUTH TOKEN ENDPOINT***] -jar [***YOUR APPLICATION JAR***]
      • In Java source code:
        System.setProperty(
            "org.apache.kafka.sasl.oauthbearer.allowed.urls",
            "[***OAUTH TOKEN ENDPOINT***]"
        );
  11. Start your Kafka client application.

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.